Yes, there are existing extensions and userscripts that can help you modify JavaScript on web pages. Below are some popular options:
Extensions
-
Tampermonkey:
- Description: Tampermonkey is a popular userscript manager that allows you to run custom scripts on web pages.
- Features: It supports a wide range of userscripts and provides a user-friendly interface for managing them.
- Link: Tampermonkey
-
Greasemonkey:
- Description: Greasemonkey is another well-known userscript manager for Firefox.
- Features: It allows you to customize the way web pages look and function by running custom scripts.
- Link: Greasemonkey
-
Violentmonkey:
- Description: Violentmonkey is a userscript manager that aims to provide a better user experience with a simple and intuitive interface.
- Features: It supports a wide range of userscripts and is available for multiple browsers.
- Link: Violentmonkey
Userscripts
-
AdGuard Scriptlets:
- Description: AdGuard provides a collection of scriptlets that can be used to modify web pages, including blocking or modifying JavaScript.
- Features: These scriptlets can be used with AdGuard's browser extension or other userscript managers.
- Link: AdGuard Scriptlets
-
Custom JavaScript for Websites 2:
- Description: This userscript allows you to run custom JavaScript on any website.
- Features: You can write and execute custom scripts to modify the behavior of web pages.
- Link: Custom JavaScript for Websites 2
-
Resource Override:
- Description: Resource Override is a browser extension that allows you to intercept and modify network requests, including JavaScript files.
- Features: You can replace, block, or modify resources before they are loaded by the browser.
- Link: Resource Override
Example Userscripts
Here are some example userscripts that you can use with Tampermonkey, Greasemonkey, or Violentmonkey to modify JavaScript on web pages:
Example 1: Modify Inline Scripts
// ==UserScript==
// @name Modify Inline Scripts
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Modify inline JavaScript on web pages
// @author You
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Function to modify the script content
function modifyScriptContent(script) {
let originalContent = script.textContent;
let modifiedContent = originalContent.replace('originalString', 'modifiedString');
script.textContent = modifiedContent;
}
// Observe the DOM for added script elements
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'SCRIPT' && node.type === 'text/javascript') {
modifyScriptContent(node);
}
});
});
});
// Start observing the document body
observer.observe(document.documentElement, { childList: true, subtree: true });
// Modify existing script elements
document.querySelectorAll('script[type="text/javascript"]').forEach(modifyScriptContent);
})();
Example 2: Modify External Scripts
// ==UserScript==
// @name Modify External Scripts
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Modify external JavaScript files on web pages
// @author You
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Function to modify the script content
function modifyScriptContent(url, callback) {
fetch(url)
.then(response => response.text())
.then(text => {
let modifiedContent = text.replace('originalString', 'modifiedString');
callback(modifiedContent);
});
}
// Observe the DOM for added script elements
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'SCRIPT' && node.src) {
let scriptUrl = node.src;
modifyScriptContent(scriptUrl, (modifiedContent) => {
let blob = new Blob([modifiedContent], { type: 'application/javascript' });
let newUrl = URL.createObjectURL(blob);
node.src = newUrl;
});
}
});
});
});
// Start observing the document body
observer.observe(document.documentElement, { childList: true, subtree: true });
})();
Conclusion
While there are existing extensions and userscripts that can help you modify JavaScript on web pages, the specific functionality you need might require custom scripting. The examples provided above should give you a good starting point for creating your own userscripts to modify JavaScript content on web pages.