Remove a javascript function with Greasemonkey
It's not clear that update
is a global function. If it isn't then that approach won't work.
But you can override the keypress handler with:
unsafeWindow.document.onkeypress = function(){};
For a general, high-powered way to selectively block, or replace, any JS (on Firefox), use @run-at document-start
and the checkForBadJavascripts function, like so:
// ==UserScript==
// @name _Replace select javascript on a page
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require https://gist.github.com/raw/2620135/checkForBadJavascripts.js
// @run-at document-start
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
checkForBadJavascripts ( [
[ false,
/document\.onkeypress\s*=\s*update/,
function () {
addJS_Node (myKeypressFunction.toString() );
addJS_Node ('document.onkeypress = myKeypressFunction;');
}
]
] );
function myKeypressFunction (evt) {
/* DO WHATEVER HERE BUT USE NO GREASEMONKEY FUNCTIONS INSIDE
THIS FUNCTION.
*/
console.log ("Keypress function fired.");
}
See this answer, for more information on checkForBadJavascripts
.
Disabling a page's focus-checking function using GM
In general, to surgically disable or alter a page's javascript, use checkForBadJavascripts
as shown in "How to alter this javascript with Greasemonkey?".
However, in this case, the page is using document.hasFocus()
, so it should be enough to just hijack that:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @run-at document-start
// @grant none
// ==/UserScript==
document.hasFocus = function () {return true;};
How to block url or .js file execution on a specific using greasemonkey script? (client side)
The second method below usually works but something seems to be interfering with it on your page for some reason. An ugly workaround is to put an empty hljs property on the window in advance, so that the page script, when run, thinks it already exists and does nothing:
window.hljsLoader = {};
You can add a MutationObserver to the document at the beginning of pageload and watch for the addition of a script tag which has that URL as a src, and remove it. Make sure you're using @run-at document-start
, and then do:
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.16/loader.min.js') {
addedNode.remove();
observer.disconnect();
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
Although the script tag will get temporarily added to the DOM, it will be removed in the observer microtask before it gets a chance to run.
Live snippet of the script working:
<script>
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
[...addedNodes].forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.16/loader.min.js') {
console.log('Script tag addition intercepted and removed');
addedNode.remove();
observer.disconnect();
console.log('hljsLoader has not been loaded:', typeof hljsLoader);
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
</script>
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="console.log(hljsLoader)" src="https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.16/loader.min.js"></script>
Replace Execution Of Javascript File Loaded From Website With My Own
Currently I've found only a solution to download and install Fiddler web debugging proxy and change Firefox proxy settings to those specified in Fiddler. To execute edited file I've used Fiddler's rule to autorespond with local file when there is a request to specified URL.
Related Topics
Webdriver Click() VS JavaScript Click()
Array.Fill(Array) Creates Copies by References Not by Value
Mongoose and Multiple Database in Single Node.Js Project
How to Render an Array of Objects in React
What Does "Options = Options || {}" Mean in JavaScript
Strip All Non-Numeric Characters from String in JavaScript
How to Strip HTML Tags from String in JavaScript
Difference Between 'Return Await Promise' and 'Return Promise'
Please Explain the Use of JavaScript Closures in Loops
How to Access Svg Elements with JavaScript
Why Are Callbacks from Promise '.Then' Methods an Anti-Pattern
Node.Js Plans to Support Import/Export Es6 (Ecmascript 2015) Modules
JavaScript Filter Array of Objects
How to Concatenate a String with a Variable
Chrome Desktop Notification Example