Override javascript file in chrome

I would like to override a javascript file with my own version of a similar javascript file in chrome.

Let me explain:

1) Let's say the site http://example.com 'calls http://example.com/script/somescript.js .

2) What I would like to do is override the http://example.com/script/somescript.js file with my own version located at ' http: //localhost/script/somescript.js '.

3) I need to effectively modify the function in the Javascript source file.

Thank: -)

+4
source share
5 answers

Chrome :

, .

+4

. , , yoman chrome.

yo chrome-extension

. Page Action Content Scripts. - , , .

? What would you like to call this extension? insert-script
? How would you like to describe this extension? replace a function with another function
? Would you like to use UI Action? Page Action
? Would you like more UI Features? Content Scripts
? Would you like to set permissions? 

.. ..

app
  bower_components
  images
  _locales
  scripts.babel
      background.js
      chromereload.js
      contentscript.js

script script, script DOM. script , , . - , , , , , , . contentscript.js:

'use strict';

console.log('\'Allo \'Allo! Content script');

:

'use strict';

var code = `
    function foo() {
        alert('foo');
    }
`;

var script = document.createElement('script');
script.textContent = code;
document.body.appendChild(script);

. , . foo() , .

. ,

  • chrome://extensions
  • ,
  • manifest.json
  • , contentscript.js.
+3

Chrome 65 .

- Chrome 65

? , /css , . , , .

?

  • "".
  • "".
  • " ".
  • , .
  • "", DevTools .
  • . " ". scripts.js, " ".
+3

, ( ) script.

window.onload = function () {
  var script = document.createElement('script');
  script.src = '//localhost/your/script';
  script.onload = function() {
    console.log('your script have been loaded');
  }
  document.body.appendChild(script);
}

, , /, , , , (, My.namespace.method = myNewMethod)

+2

chrome, Requestly. :

:

enter image description here

You can also select the Contains / Matches operator, depending on your use case.

+1
source

Source: https://habr.com/ru/post/1630076/


All Articles