[英]chrome extension remove script tags
我到處試圖找到這個問題的答案。 我希望我的擴展能夠禁用頁面上的所有javascript但允許插入一個有效的腳本。 (所以chrome.contentSettings.javascript現在不是一個有效的選項)或者我想要一種方法來刪除它們中的任何一個之前的所有腳本標簽(這是有點相同的事情)
我嘗試將內容腳本插入runat:document_start但當時dom並不完全存在。 itried在加載狀態時在tabs.onUpdate上添加了一個上傳但是為時已晚,以及document_end上的內容腳本(所有這些都嘗試刪除腳本標記),但現在還為時已晚。 在絕望的行為中,我試圖改變element.innerHTML的getter和setter的行為。 刪除標簽但不起作用
我試圖避免向location.href發送xhr請求並解析和重新設置內容,因為它太密集了。
有任何想法嗎?
看到您的評論后,我認為這可能滿足您的需求。 它的工作原理是獲取頁面源,將其渲染為dom,禁用所有j,然后將其放回頁面。 不完全是你想要的,但應該很好地適應你的情況......
mainfest.json
{
"name": "Reload and Kill JS - Using a content script",
"version": "1.0",
"permissions": [
"tabs", "<all_urls>" , "storage"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["injectedCode.js"],
"run_at" : "document_start"
}
],
"minimum_chrome_version" : "20",
"manifest_version" : 2
}
background.js
chrome.storage.local.set({"blockhttp://paez.kodingen.com/":true});
injectedCode.js
reloadAndKillJS = function() {
document.documentElement.innerHTML = 'Reloading Page...';
var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href, true);
xhr.onerror = function() {
document.documentElement.innerHTML = 'Error getting Page';
}
xhr.onload = function() {
var page = document.implementation.createHTMLDocument("");
page.documentElement.innerHTML = this.responseText;
var newPage = document.importNode(page.documentElement, true);
var nodeList = newPage.querySelectorAll('script');
for (var i = 0; i < nodeList.length; ++i) {
var node = nodeList[i];
if (node.src) {
node.setAttribute('original-src', node.src);
node.removeAttribute('src');
}
node.innerText = '';
}
document.replaceChild(newPage, document.documentElement);
delete page;
// Do your thing here
}
xhr.send();
}
chrome.storage.local.get("block"+window.location.href,function(items){
if (items["block"+window.location.href]){
window.stop();
reloadAndKillJS();
}
});
嗯,真正防止腳本的唯一方法是使用contentSettings。 因此,您需要將代碼放在其他域中的其他位置,因為contentSettings規則可以應用於特定的URL。
將內容腳本放在文檔啟動時運行。
contentScript.js:
window.stop();
document.all[0].innerHTML = "\
<html>\
<body>\
<iframe src=\"chrome-extension://ID/inject.html?url="+encodeURIComponent(location.href)+"\"></iframe>\
</body>\
</html>";
inject.html:
<html>
<head>
<script>
var frame = document.querySelector('iframe');
frame.src = location.search.replace('?url=', '');
frame.onload = function() {
//Your stuff here
}
</script>
</head>
<body>
<iframe></iframe>
</body>
<html>
現在您的代碼位於父框架和另一個域中,但它可能會導致一些CORS問題,您可以嘗試稍后找到一些解決方法。 試一試,然后告訴我是否有什么需要解決的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.