我想建立一個Chrome擴展;這方面的經驗最少。我跟着Insert code into the page context using a content script使用第一種方法將js代碼注入頁面。覆蓋鉻擴展中的JavaScript警報
我想建立一個js框架而不是別人的代碼,它嚴重依賴於打破我的覆蓋層功能的警報,所以我只是想讓他們沉默 - 實際上,我寧願通過將消息發送到console.log中,但我會在現階段採取我所能得到的。所以我試着按照JavaScript: Overriding alert()設置我的最終js文件(nogo.js)進行注入。
nogo.js被注入,但它似乎沒有抑制警報的效果。可能是因爲另一個html文件本身是由一個不同的js文件啓動的,注入的速度太慢或者是亂序?
的manifest.json
"content_scripts": [
{
"matches": ["*://URL/*"],
"js": ["myscript.js"],
"run_at": "document_end",
"all_frames": true
},
{
"matches": ["*://URL/*"],
"js": ["noalerts.js"],
"run_at": "document_start",
"all_frames": true
}
],
"web_accessible_resources": ["script.js","nogo.js"]
}
myscript.js
var s = document.createElement('script');
// TODO: add "script.js" to web_accessible_resources in manifest.json
s.src = chrome.extension.getURL('script.js');
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
noalerts.js
var n = document.createElement('script');
// TODO: add "script.js" to web_accessible_resources in manifest.json
n.src = chrome.extension.getURL('nogo.js');
n.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(n);
nogo.js
window.alert = null;
感謝您的提示,我已經能夠得到nogo.js注入,但不幸的是代碼不能防止警報窗口。 – CursingLoudlyintheOffice
這裏:https://stackoverflow.com/q/12095924/632951 – Pacerier