2014-07-13 62 views
1

我寫了一個簡短的內容腳本,該腳本阻止特定站點爲鏈接點擊創建新窗口。簡單的Chrome內容腳本未運行

這是我第一次使用Chrome瀏覽器擴展程序,爲此我無法運行,但我找不到這個網站和互聯網。我可能在某個地方犯了一個基本的業餘失誤。

manifest.json的:

{ 
"manifest_version": 2, 

"name": "DHS Links", 
"description": "Stops the school's site from constantly opening new windows.", 
"version": "1.0", 

"browser_action": { 
"default_icon": "icon.png" 
}, 

"content_scripts":[ 
    { 
    "matches": ["*://www.darienps.org/dhs/*"], 
    "js": ["jquery.js", "makeNormalLinks.js"], 
    "run_at": "document_end" 

    } 
] 
} 

我本身對網站的本地版本測試的JavaScript文件,所以我敢肯定它的罰款,但以防萬一:

makeNormalLinks。 js:

$(document).ready(function() { 
    $("a").each(function(){ 
     $(this).removeAttr("onclick"); 
    }); 
}); 

jQuery的一個副本是在同一個目錄中,似乎沒有任何問題。

這裏的onclick代碼的網站上的許多鏈接:

onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,')

謝謝你看這個了!


編輯:

我嘗試了兩種從Rob W's response to another question由Teepeemm的評論鏈接到注射方法。

下面是方法1的新代碼:

manifest.json的:

{ 
"manifest_version": 2, 

"name": "DHS Links", 
"description": "Stops the school's site from constantly opening new windows.", 
"version": "1.0", 

"browser_action": { 
"default_icon": "icon.png" 
}, 

"content_scripts":[ 
    { 
    "matches": ["*://www.darienps.org/dhs/*"], 
    "js": ["jquery.js", "scriptLauncher.js"] 

    } 
], 
"web_accessible_resources": ["makeNormalLinks.js"] 
} 

scriptLauncher.js:

var s = document.createElement('script'); 
// TODO: add "script.js" to web_accessible_resources in manifest.json 
s.src = chrome.extension.getURL('makeNormalLinks.js'); 
s.onload = function() { 
this.parentNode.removeChild(this); 
}; 
(document.head||document.documentElement).appendChild(s); 

方法2a:

(使用舊清單。 js)

makeNormalLinks.js:

var actualCode = ['$(document).ready(function(){', 
        '$("a").each(function(){', 
        '$(this).removeAttr("onclick");', 
        '});', 
        '});'].join('\n'); 

var script = document.createElement('script'); 
script.textContent = actualCode; 
(document.head||document.documentElement).appendChild(script); 
script.parentNode.removeChild(script); 

不幸的是,這兩種方法似乎都不起作用。我非常感謝那些評論並認爲我們接近答案的人。


解決方案:

manifest.json的:

{ 
"manifest_version": 2, 

"name": "DHS Links", 
"description": "Stops the school's site from constantly opening new windows.", 
"version": "1.0", 

"browser_action": { 
"default_icon": "icon.png" 
}, 

"content_scripts":[ 
    { 
    "matches": ["*://www.darienps.org/dhs/*"], 
    "js": ["makeNormalLinks.js"] 

    } 
] 
} 

makeNormalLinks.js:

document.addEventListener("click", function(e) { 
    e.stopPropagation(); 
}, true); 

謝謝你,斯科特和所有誰評論!

+0

'$( 「文件」)。就緒(函數(){'大概應該是'$(文件)。就緒(函數(){'或'只是$(函數(){'。 –

+0

我不能相信我犯了這個錯誤,感謝你的支持!不幸的是,擴展仍然不起作用 – Nick

+0

無關,但你不需要「browser_action」或「permissions」來做到這一點,而「document_end 「是默認的,並且可以省略。 – Teepeemm

回答

1

使用onclick屬性有許多奇怪的副作用。更新的方法是將偵聽器添加到過濾不需要的事件的文檔。像:

document.addEventListener("click", function(e) { 
    e.stopPropagation(); 
}, true); 

true參數是很重要的(見事件捕獲)。這將阻止所有點擊事件偵聽器觸發,但默認點擊操作仍將被觸發。

+0

它的工作!該方法甚至不需要注入腳本或jQuery。非常感謝! – Nick

0

我有一個類似的問題,讓內容腳本在Google Mail中觸發。我偶然發現了一個推薦使用「hashchange」事件的頁面。

// Event listener 
window.addEventListener("hashchange", function() { 
    alert("hashchanged"); 
}, false); 
+0

你應該指出這是否會有助於OP和如何 – YakovL