Replace Javascript using Greasemonkey / Tampermonkey / FiddlerScript / uBlock

Costas

Administrator
Staff member
replace an entire script
http://squakmt.com/replacing_javascript/

change parts of an exisitng script
https://userscripts-mirror.org/scripts/show/125936

The beforescriptexecute event is marked as deprecated, but is still supported by Firefox as of version 4.

beforescriptexecute on Chrome
https://gist.github.com/BrockA/2620135#gistcomment-2596040
https://github.com/jspenguin2017/Snippets/blob/master/onbeforescriptexecute.html



Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.


https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo




Bare minimum Chrome extension to inject a JS file into the given page when you click on the browser action icon. The script then inserts a new div into the DOM.

https://gist.github.com/danharper/8364399



use Telerik FiddlerScript


Redirect the request :
JavaScript:
if (oSession.url=="www.site.com/javascript.js")
{
  oSession.url = "www.newsite.com/differentjavascript.js";
}

or do a search & replace on the html content from the response...

JavaScript:
if (oSession.HostnameIs("www.site.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html"))
{
  oSession.utilDecodeResponse();
  oSession.utilReplaceInResponse('www.site.com/javascript.js','www.newsite.com/differentjavascript.js');
}



uBlock v1.33 on Chrome
https://github.com/gorhill/uBlock/releases/tag/1.33.0

I have setup a localhost


inject js code into specific domain

<div style="margin-left:30px">
On addon settings
DVaivHD.png


adding the JS to be injected
more -- https://github.com/gorhill/uBlock/wiki/Advanced-settings#userresourceslocation

B56Nhbr.png


source of test.js (give a handle to script ex twitch-videoad.js)
JavaScript:
twitch-videoad.js application/javascript
/* test */
alert("this is a test on localhost");

go back and
ADi4ulM.png


set the filter
5VJYmlH.png


JavaScript:
//src - https://github.com/pixeltris/TwitchAdSolutions
localhost##+js(twitch-videoad)

navigate to localhost
uqTmCtj.jpg

</div>


replace js call, by your js file


<div style="margin-left:30px">

at the time Im using the uBlock as unpacked extension, but even you have installed you can dig to userprofile\addons\addon dir\

no needed to use the userResourcesLocation, leave it unset.

on the tab My Filters add :
JavaScript:
//src - https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#redirect
||localhost/*.js$1p,script,redirect=myjsjs:100
//or for online site + specific js file
||test.net/js/postvalidation.en.js$1p,script,redirect=myjsjs:100

close the browser, go to addon directory \web_accessible_resources\

create a file myjs.js copy the code from the default noop.js exists on the same directory.

edit the myjs.js, add something to the code, to signalize that is loaded..

we have to declare the new webresource to js\redirect-engine.js, under noop.js add yours as

JavaScript:
    [ 'noop.js', {
        alias: [ 'noopjs', 'abp-resource:blank-js' ],
        data: 'text',
    } ],
    [ 'myjs.js', {
        alias: [ 'myjsjs', 'abp-resource:myjs-js' ],
        data: 'text',
    } ]

restart the browser, navigate to localhost, see your message you wrote to myjs.js

on the debug console, the source call will appear 307 - Internal Redirect

tested and working on online site as well.
</div>

Overview of uBlock's network filtering engine
Priorities and filtering

TwitchAdSolutions - Examples of use
 
Top