Created
August 26, 2020 13:09
-
-
Save mistificator/97f0d4649210f6cf11511c58782e135f to your computer and use it in GitHub Desktop.
Plurk jQuery patch
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 Plurk jQuery patch | |
// @namespace http://www.plurk.com/ | |
// @include http://www.plurk.com/* | |
// @include https://www.plurk.com/* | |
// @version 0.1 | |
// @description Reconnect jQuery for Plurk.com | |
// @author Mist Poryvaev | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
// based on https://github.com/jspenguin2017/Snippets/blob/master/onbeforescriptexecute.html | |
// Library code, licensed under MIT | |
(() => { | |
"use strict"; | |
const Event = class { | |
constructor(script, target) { | |
this.script = script; | |
this.target = target; | |
this._cancel = false; | |
this._replace = null; | |
this._stop = false; | |
} | |
preventDefault() { | |
this._cancel = true; | |
} | |
stopPropagation() { | |
this._stop = true; | |
} | |
replacePayload(payload) { | |
this._replace = payload; | |
} | |
}; | |
let callbacks = []; | |
window.addBeforeScriptExecuteListener = (f) => { | |
if (typeof f !== "function") { | |
throw new Error("Event handler must be a function."); | |
} | |
callbacks.push(f); | |
}; | |
window.removeBeforeScriptExecuteListener = (f) => { | |
let i = callbacks.length; | |
while (i--) { | |
if (callbacks[i] === f) { | |
callbacks.splice(i, 1); | |
} | |
} | |
}; | |
const dispatch = (script, target) => { | |
if (script.tagName !== "SCRIPT") { | |
return; | |
} | |
const e = new Event(script, target); | |
if (typeof window.onbeforescriptexecute === "function") { | |
try { | |
window.onbeforescriptexecute(e); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
for (const func of callbacks) { | |
if (e._stop) { | |
break; | |
} | |
try { | |
func(e); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
if (e._cancel) { | |
script.textContent = ""; | |
script.remove(); | |
} else if (typeof e._replace === "string") { | |
script.textContent = e._replace; | |
} | |
}; | |
const observer = new MutationObserver((mutations) => { | |
for (const m of mutations) { | |
for (const n of m.addedNodes) { | |
dispatch(n, m.target); | |
} | |
} | |
}); | |
observer.observe(document, { | |
childList: true, | |
subtree: true, | |
}); | |
})(); | |
// mixed with https://stackoverflow.com/questions/36605989/how-to-replace-head-script-using-greasemonkey-code | |
// and https://stackoverflow.com/questions/18120809/equivalent-of-onbeforescriptexecute-in-chrome | |
(() => { | |
"use strict"; | |
window.onbeforescriptexecute = (e) => { | |
if (e.script.outerHTML.includes("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js")) { | |
var dst_script = document.createElement('script'); | |
dst_script.src = "https://code.jquery.com/jquery-3.3.1.min.js"; | |
e.target.replaceChild(dst_script, e.script); | |
e.preventDefault(); | |
} | |
}; | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment