Last active
November 21, 2016 12:52
-
-
Save nutrino/e9db415ec0c0022ac22b72c47707d231 to your computer and use it in GitHub Desktop.
url: Medium remove location hash TamperMonkey
workaround because Tampermonkey doesn't support # in include. forked from @iki https://gist.github.com/iki/03af4eda558efd56d038520889afb56a
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 url: Medium remove location hash TamperMonkey | |
// @namespace http://efcl.info/ | |
// @description Remove location hash from medium | |
//// @include https://voice.kadira.io/* | |
//// @include https://medium.com/* | |
// @include /^https://.+-[a-z0-9]{11,12}$/ | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
// TamperMonkey does not work well with hash '#' in @include urls: | |
// https://forum.tampermonkey.net/viewtopic.php?p=3094#p3094 | |
function removeLocationHash(event) { | |
var components = window.location.href.split('#'); | |
if (components.length > 1) { | |
var base = components[0]; | |
var hash = components.slice(1).join('#'); | |
if (hash) { | |
console.log('medium: remove hash' + (event && event.type ? ' on ' + event.type : '') + ' #' + hash, arguments); | |
window.history.replaceState('', document.title, base); | |
} | |
return hash; | |
} | |
} | |
function removeLocationHashAndStop(event) { | |
event.preventDefault(); | |
removeLocationHash(event); | |
} | |
function removeLocationHashAndScheduleNext(event) { | |
event.scheduleMs = removeLocationHash(event) ? event.scheduleMinMs || 500 : Math.min((event.scheduleMs || event.scheduleMinMs || 500) * 2, event.scheduleMaxMs || 60000); | |
event.type = 'schedule'; | |
console.log('medium: remove hash: schedule after ' + event.scheduleMs + 'ms'); | |
setTimeout(function() { removeLocationHashAndScheduleNext(event); }, event.scheduleMs); | |
} | |
function registerRemoveLocationHash() { | |
console.log('medium: remove hash: register'); | |
window.addEventListener('popstate', removeLocationHash); | |
window.addEventListener('hashchange', removeLocationHashAndStop); | |
window.addEventListener('load', removeLocationHash); | |
removeLocationHashAndScheduleNext({type: 'userscript load'}); | |
} | |
registerRemoveLocationHash(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment