问题
I know with Chrome I can use snippets to manually run some js code on a webpage. But This required manually executing the snippet. is there any way to have some JS code that my browser (chrome or firefox) would every time I visited a new page?
回答1:
You may be looking for userscripts. These allow you to write Javascript code which will then be automatically executed whenever you visit a page matching a particular pattern.
For example, the following userscript:
// ==UserScript==
// @name example name
// @include /^https://example\.com/
// @grant none
// ==/UserScript==
console.log('hello world');
will result in hello world
being logged to the console every time you visit https://example.com/.
To run userscripts, you need a userscript manager. Tampermonkey is the most popular choice - it works for all modern browsers.
Another option is a bookmarklet, which allow you to store code in a browser bookmark, but they require you to click on the bookmark link in order to execute the code - it won't run automatically. For example, a bookmark with the following URL:
javascript: (() => { console.log('hello world') })();
will result in hello world
being logged to the console whenever the bookmarklet is clicked. For any code that isn't trivially short, a userscript is probably the more maintainable choice.
回答2:
Requestly chrome and firefox extension can solve your use case. You can use the Insert Script feature to inject any JS or CSS code before or after page load.
You can either enter a custom URL for the javascript file or write the custom js code in the inbuilt editor. You can also create a JS/CSS file in Requestly library too and use that file.
回答3:
Actually the best thing to do is to create a chrome extension
来源:https://stackoverflow.com/questions/60292446/run-my-javascript-code-on-every-page-on-my-browser-similar-to-how-a-chrome-exte