짧은 링크를 작성하여 특정 사이트가 링크 클릭에 대한 새 창을 만들지 못하게했습니다.간단한 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"
}
]
}
는 내가있는 지역의 웹 사이트에 그 자체로 자바 스크립트 파일을 테스트, 그래서 나는 괜찮아요 확신하지만, 단지의 경우에있어 :
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,')
이 이상을 찾고 주셔서 감사합니다!
편집 :
나는 Teepeemm에 의해 코멘트에 링크 Rob W's response to another question에서 주입 방법 두 가지를 시도했다.여기에 방법 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);
당신 덕분에, 스콧 주석 모든 사람!
'$ ("문서"). 준비 (함수() {'아마'해야 $ (문서) .ready (함수() {'하거나'$ (함수() {'. –
불행히도, 확장 기능이 여전히 작동하지 않는 것 같아요. – Nick
관련이 없지만 이렇게하려면 "browser_action"또는 "permissions"가 필요하지 않습니다. 그리고 "document_end "이 기본값 일 때 누락 될 수 있습니다 .. – Teepeemm