Firefox obsługuje obecnie the beforescriptexecute event (od Version 4, released on March 22, 2011) ‡.
Z tym wydarzeniem i the // @run-at document-start
directive, Firefox i Greasemonkey wydają się teraz robić dobrą robotę przechwytującą konkretne tagi <script>
.
To nadal nie jest możliwe w przypadku Chrome + Tampermonkey. Aby uzyskać wszystko oprócz Firefox + Greasemonkey, będziesz musiał użyć technik, jak pokazano w innych odpowiedziach, poniżej, napisać pełne rozszerzenie przeglądarki.
The checkForBadJavascripts
function to hermetyzuje. Na przykład załóżmy, że strona miała <script>
tag tak:
<script>
alert ("Sorry, Sucka! You've got no money left.");
</script>
Można użyć checkForBadJavascripts
tak:
checkForBadJavascripts ([
[ false,
/Sorry, Sucka/,
function() {
addJS_Node ('alert ("Hooray, you\'re a millionaire.");');
}
]
]);
dostać dużo ładniejszy wiadomość. (^_^)
Zobacz dokumentację w wersji checkForBadJavascripts, aby uzyskać więcej informacji.
Aby zobaczyć demonstrację w kompletnym skrypcie, należy najpierw odwiedzić stronę this page at jsBin. Zobaczysz 3 wiersze tekstu, dwa z nich dodane przez JS.
Teraz, install this script (View source; jest również poniżej) i ponownie odwiedzić stronę. Zobaczysz, że skrypt GM usunął jeden zły znacznik i zastąpił go innym naszym "dobrym" JS.
‡ Należy pamiętać, że tylko Firefox obsługuje zdarzenie beforescriptexecute
. Został on usunięty ze specyfikacji HTML5 bez określonej równoważnej możliwości.
Kompletny przykład skrypt GM (Taki sam jak ten na GitHub i jsBin):
Biorąc pod uwagę to HTML:
<body onload="init()">
<script type="text/javascript" src="http://jsbin.com/evilExternalJS/js"></script>
<script type="text/javascript" language="javascript">
function init() {
var newParagraph = document.createElement ('p');
newParagraph.textContent = "I was added by the old, evil init() function!";
document.body.appendChild (newParagraph);
}
</script>
<p>I'm some initial text.</p>
</body>
Użyj tego Greasemonkey skrypt:
// ==UserScript==
// @name _Replace evil Javascript
// @include http://jsbin.com/ogudon*
// @run-at document-start
// ==/UserScript==
/****** New "init" function that we will use
instead of the old, bad "init" function.
*/
function init() {
var newParagraph = document.createElement ('p');
newParagraph.textContent = "I was added by the new, good init() function!";
document.body.appendChild (newParagraph);
}
/*--- Check for bad scripts to intercept and specify any actions to take.
*/
checkForBadJavascripts ([
[false, /old, evil init()/, function() {addJS_Node (init);} ],
[true, /evilExternalJS/i, null ]
]);
function checkForBadJavascripts (controlArray) {
/*--- Note that this is a self-initializing function. The controlArray
parameter is only active for the FIRST call. After that, it is an
event listener.
The control array row is defines like so:
[bSearchSrcAttr, identifyingRegex, callbackFunction]
Where:
bSearchSrcAttr True to search the SRC attribute of a script tag
false to search the TEXT content of a script tag.
identifyingRegex A valid regular expression that should be unique
to that particular script tag.
callbackFunction An optional function to execute when the script is
found. Use null if not needed.
*/
if (! controlArray.length) return null;
checkForBadJavascripts = function (zEvent) {
for (var J = controlArray.length - 1; J >= 0; --J) {
var bSearchSrcAttr = controlArray[J][0];
var identifyingRegex = controlArray[J][1];
if (bSearchSrcAttr) {
if (identifyingRegex.test (zEvent.target.src)) {
stopBadJavascript (J);
return false;
}
}
else {
if (identifyingRegex.test (zEvent.target.textContent)) {
stopBadJavascript (J);
return false;
}
}
}
function stopBadJavascript (controlIndex) {
zEvent.stopPropagation();
zEvent.preventDefault();
var callbackFunction = controlArray[J][2];
if (typeof callbackFunction == "function")
callbackFunction();
//--- Remove the node just to clear clutter from Firebug inspection.
zEvent.target.parentNode.removeChild (zEvent.target);
//--- Script is intercepted, remove it from the list.
controlArray.splice (J, 1);
if (! controlArray.length) {
//--- All done, remove the listener.
window.removeEventListener (
'beforescriptexecute', checkForBadJavascripts, true
);
}
}
}
/*--- Use the "beforescriptexecute" event to monitor scipts as they are loaded.
See https://developer.mozilla.org/en/DOM/element.onbeforescriptexecute
Note that it does not work on acripts that are dynamically created.
*/
window.addEventListener ('beforescriptexecute', checkForBadJavascripts, true);
return checkForBadJavascripts;
}
function addJS_Node (text, s_URL, funcToRun) {
var D = document;
var scriptNode = D.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
//--- Don't error check here. if DOM not available, should throw error.
targ.appendChild (scriptNode);
}
Czy możesz pokazać nam kod i punkt w linii, którą chcesz wyłączyć? – hookedonwinter
Możesz po prostu pobrać JS, zmodyfikować go lokalnie i uruchomić tam? – Rudu
Wysłałem rozwiązanie tutaj: http: // stackoverflow.com/a/9699686/6355 Ponieważ jest to niewiele więcej niż link tutaj, to znowu: http://userscripts.org/scripts/show/125936 – Sarien