-
-
Save knu/dc1b31fbc11a72f916b21cd631e557df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name canonical-link | |
// @namespace http://www.hatena.ne.jp/hitode909 | |
// @include * | |
// ==/UserScript== | |
(function() { | |
if (window != window.parent) { | |
return; | |
} | |
var getValue = function (query, attr) { | |
var element = document.querySelector(query); | |
if (element) { | |
return element[attr]; | |
} | |
}; | |
var url = | |
getValue('link[rel="canonical"]', 'href') || | |
getValue('meta[property="og:url"]', 'content'); | |
if (url == null) { | |
return; | |
} | |
url = url.replace(/^([^\/]+:\/\/[^/]+)$/, "$1/"); | |
if (url != location.href) { | |
var button = document.createElement('a'); | |
document.querySelector('body').appendChild(button); | |
button.appendChild(document.createTextNode('canonical')); | |
Object.assign(button.style, { | |
position: 'fixed', | |
width: '100%', | |
left: 0, | |
bottom: 0, | |
color: '#fff', | |
background: '#000', | |
fontSize: '24px', | |
textAlign: 'center', | |
textDecoration: 'none', | |
margin: 0, | |
padding: 0, | |
fontWeight: 'bold', | |
opacity: 0.5, | |
zIndex: 9999, | |
}); | |
button.href = url; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment