Firefox hiện hỗ trợ the beforescriptexecute event (tính Version 4, released on March 22, 2011) ‡.
Với sự kiện đó và the // @run-at document-start
directive, Firefox và Greasemonkey hiện có vẻ như đang làm tốt việc chặn các thẻ <script>
cụ thể.
Điều này vẫn không thể thực hiện được với Chrome + Tampermonkey. Đối với bất cứ điều gì, nhưng Firefox + Greasemonkey, bạn sẽ cần phải sử dụng các kỹ thuật như được hiển thị trong các câu trả lời khác, dưới đây, viết một phần mở rộng trình duyệt đầy đủ.
The checkForBadJavascripts
function đóng gói này. Ví dụ, giả trang có một thẻ <script>
như vậy:
<script>
alert ("Sorry, Sucka! You've got no money left.");
</script>
Bạn có thể sử dụng checkForBadJavascripts
như vậy:
checkForBadJavascripts ([
[ false,
/Sorry, Sucka/,
function() {
addJS_Node ('alert ("Hooray, you\'re a millionaire.");');
}
]
]);
để có được một thông điệp đẹp hơn nhiều. (^_^)
Xem tài liệu nội tuyến, trong checkForBadJavascripts để biết thêm.
Để xem trình diễn hoàn chỉnh, trước tiên hãy truy cập this page at jsBin. Bạn sẽ thấy 3 dòng văn bản, hai dòng văn bản được thêm bởi JS.
Bây giờ, install this script (View source; nó cũng ở bên dưới.) Và truy cập lại trang. Bạn sẽ thấy rằng tập lệnh GM đã xóa một thẻ không hợp lệ và thay thế một thẻ khác bằng JS "tốt" của chúng tôi.
‡ Lưu ý rằng chỉ Firefox hỗ trợ sự kiện beforescriptexecute
. Và nó đã bị xóa khỏi thông số HTML5 mà không có khả năng tương đương được chỉ định.
Toàn bộ kịch bản GM dụ (Giống như một ở GitHub và jsBin):
Với HTML này:
<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>
Sử dụng kịch bản Greasemonkey này:
// ==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);
}
Bạn có thể hiển thị mã và trỏ vào dòng bạn muốn tắt không? – hookedonwinter
Bạn chỉ có thể tải xuống JS, sửa đổi nó cục bộ và thực thi nó ở đó? – Rudu
Tôi đã đăng một giải pháp ở đây: http: // stackoverflow.com/a/9699686/6355 Vì nó ít hơn một liên kết ở đây, nó lại xuất hiện: http://userscripts.org/scripts/show/125936 – Sarien