简单的Chrome内容脚本未运行 [英] Simple Chrome Content Script Not Running

查看:180
本文介绍了简单的Chrome内容脚本未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简短的内容脚本,该脚本阻止了某个网站创建链接点击的新窗口。



这是我第一次使用Chrome扩展程序,这个网站和互联网为什么它不会运行的原因,但我找不到任何。我可能在某个地方犯了一个基本的业余犯错。

Manifest.json:

 manifest_version:2,

name:DHS链接,
description:阻止学校网站不断打开新窗口。,
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的副本位于同一目录中,似乎没有任何问题。 p>

以下是网站上许多链接的onclick代码:



onclick =窗口。打开(this.href,'targetWindow','toolbar = no,location = no,status = no,menubar = no,scrollbars = yes,resizable = yes,')


$ p
$ b

编辑:

感谢您阅读本文!

p>

我尝试了两种

以下是关于Teepeemm的评论方法1的新代码:

Manifest.json:

  { 
manifest_version:2,

name:DHS链接,
description:阻止学校网站不断打开新窗口。,
版本:1.0 ,

browser_action:{
default_icon:icon.png
},

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

$ b],
web_accessible_resources:[makeNormalLinks.js]
}

scriptLauncher.js:

  var s = document.createElement('script ); 
// TODO:在manifest.json中添加script.js到web_accessible_resources
s.src = chrome.extension.getURL('makeNormalLinks.js');
s.onload = function(){
this.parentNode.removeChild(this);
};
(document.head || document.documentElement).appendChild(s);

方法2a:

makeNormalLinks.js:

  var实际代码= ['$(document).ready(function(){',
'$(a)。each(function(){',
'$(this).removeAttr(onclick );',
'});',
'});']。join('\\\
');

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

不幸的是,这两种方法似乎都不起作用。我非常感谢那些评论并认为我们接近答案的人。






解决方案: p>

Manifest.json:

  {
manifest_version: 2,

姓名:DHS链接,
description:阻止学校网站不断打开新窗口。,
version:1.0 ,

browser_action:{
default_icon:icon.png
},

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

}
]
}

makeNormalLinks.js:



pre $ lt; code> document.addEventListener(click,function(e){
e.stopPropagation();
},true) ;

谢谢您,Scott和所有评论者!

解决方案

使用 onclick 属性有许多奇怪的副作用。更新的方法是将侦听器添加到过滤不需要的事件的文档。像:

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

true 参数很重要捕获)。这会阻止所有点击事件侦听器发起冲击,但默认的点击动作仍然会被触发。


I wrote a short content script, which stops a particular site from creating new windows for link clicks.

This is my first Chrome extension, and I've scored this website and the internet for a reason why it won't run, but I can't find any. I'm probably making a fundamental amateur mistake somewhere.

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"

  }
 ]
}

I tested the Javascript file by itself on a local version of the site, so I'm pretty sure it's fine, but just in case:

makeNormalLinks.js:

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

A copy of jQuery is in the same directory and doesn't seem to have any issues.

Here's the onclick code for many links on the website:

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

Thank you for looking this over!


Edit:

I tried two of the injection methods from Rob W's response to another question linked to in the comments by Teepeemm.

Here's the new code for Method 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);

Method 2a:

(Uses old Manifest.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);

Unfortunately, neither method seems to work. I'm extremely grateful to those who commented and think we're getting close to an answer.


Solution:

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);

Thanks you, Scott and all who commented!

解决方案

Using the onclick attribute has many weird side effects. A more up to date approach would be to add a listener to the document that filters unwanted events. Like:

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

The true argument is important (see event capture). This will block all click event listeners from firing, but the default click action will still be triggered.

这篇关于简单的Chrome内容脚本未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆