OnClick或Inline脚本不在扩展中工作
这似乎是最简单的事情,但这只是不起作用。在普通浏览器中,.html和.js文件可完美地工作,但是在Chrome/Firefox扩展程序中,OnClick
函数无法执行该功能。
.js文件:
function hellYeah(text) {
document.getElementById("text-holder").innerHTML = text;
}
.html文件:
<!doctype html>
<html>
<head>
<title>
Getting Started Extension's Popup
</title>
<script src="popup.js"></script>
</head>
<body>
<div id="text-holder">
ha
</div>
<br />
<a onClick=hellYeah("xxx")>
hyhy
</a>
</body>
</html>
因此,基本上,一旦用户单击“ hyhy”,“ ha”应该更改为“ xxx”。再说一次 - 它在浏览器中完美工作,但在扩展方面不起作用。你知道为什么吗?以防万一我还附加了清单。
清单:json:
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"http://api.flickr.com/"
]
}
This seems to be the easiest thing to do, but it's just not working. In a normal browser the .html and .js files works perfectly, but in the Chrome/Firefox extension the onClick
function is not performing what it's supposed to do.
.js file:
function hellYeah(text) {
document.getElementById("text-holder").innerHTML = text;
}
.html file:
<!doctype html>
<html>
<head>
<title>
Getting Started Extension's Popup
</title>
<script src="popup.js"></script>
</head>
<body>
<div id="text-holder">
ha
</div>
<br />
<a onClick=hellYeah("xxx")>
hyhy
</a>
</body>
</html>
So basically once the user clicks "hyhy", "ha" should change into "xxx". And again - it works perfectly in the browser but does not work in the extension. Do you know why? Just in case I'm attaching the manifest.json below as well.
manifest.json:
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"http://api.flickr.com/"
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Chrome Extensions不允许您具有内联JavaScript(
Firefox Webextensions()。
您将必须做类似的事情:
将ID分配给链接(
&lt; a onClick = hellyeah(“ xxx”)&gt;
sust&lt; a ID =“ link “&gt;
),并使用 绑定事件。将以下内容放入您的popup.js
文件:popup.js
应加载为单独的脚本文件:Chrome Extensions don't allow you to have inline JavaScript (documentation).
The same goes for Firefox WebExtensions (documentation).
You are going to have to do something similar to this:
Assign an ID to the link (
<a onClick=hellYeah("xxx")>
becomes<a id="link">
), and useaddEventListener
to bind the event. Put the following in yourpopup.js
file:popup.js
should be loaded as a separate script file:Reason
This does not work, because Chrome forbids any kind of inline code in extensions via Content Security政策。
如何检测
这是否确实是问题,Chrome会在控制台中产生以下错误:
要访问弹出窗口的JavaScript控制台(一般对调试很有用),请右键单击扩展程序的按钮,然后从上下文菜单中选择“检查弹出式”。
有关调试弹出的更多信息,请在这里。
如何修复
需要删除所有内联JavaScript。有 Chrome文档中的指南。
假设原始看起来像:
一个人需要删除
onClick
属性,并给元素一个唯一的ID:然后从脚本附加侦听器(必须在
.js 文件,假设
popup.js
):注意
domcontentloaded
事件中的包装。这样可以确保该元素在执行时存在。现在,添加脚本标签,例如,在&lt; head&gt;
中添加文档的:替代方案,如果您使用jQuery:
放松策略
q:该错误提到了方法允许内联代码。我不想 /不能更改我的代码,如何启用内联脚本?
a:尽管错误说的是 不能启用内联脚本:
更新:自Chrome 46以来
但是,我没有很容易看到使用此功能的理由,也不会启用诸如
onclick =“ code”
之类的内联属性。Reason
This does not work, because Chrome forbids any kind of inline code in extensions via Content Security Policy.
How to detect
If this is indeed the problem, Chrome would produce the following error in the console:
To access a popup's JavaScript console (which is useful for debug in general), right-click your extension's button and select "Inspect popup" from the context menu.
More information on debugging a popup is available here.
How to fix
One needs to remove all inline JavaScript. There is a guide in Chrome documentation.
Suppose the original looks like:
One needs to remove the
onclick
attribute and give the element a unique id:And then attach the listener from a script (which must be in a
.js
file, supposepopup.js
):Note the wrapping in a
DOMContentLoaded
event. This ensures that the element exists at the time of execution. Now add the script tag, for instance in the<head>
of the document:Alternative if you're using jQuery:
Relaxing the policy
Q: The error mentions ways to allow inline code. I don't want to / can't change my code, how do I enable inline scripts?
A: Despite what the error says, you cannot enable inline script:
Update: Since Chrome 46, it's possible to whitelist specific inline code blocks:
However, I do not readily see a reason to use this, and it will not enable inline attributes like
onclick="code"
.我遇到了同样的问题,并且不想重写代码,因此我编写了一个函数来修改代码并创建内联声明的事件:
这应该从所选节点中删除所有内联事件,然后用jQuery重新创建它们。
I had the same problem, and didn´t want to rewrite the code, so I wrote a function to modify the code and create the inline declarated events:
This should remove all inline events from the selected node, and recreate them with jquery instead.
我决定发布我在情况下使用的示例。我尝试使用脚本替换DIV中的内容。我的问题是,Chrome没有识别 /没有运行该脚本。
更详细地说,我想做的是:单击链接,以及指向“读取”外部HTML文件的链接,将其加载到DIV部分中。
被称为,脚本没有用。
必须使用document.addeventlistener('domcontentloaded',function',function(),
I decide to publish my example that I used in my case. I tried to replace content in div using a script. My problem was that Chrome did not recognized / did not run that script.
In more detail What I wanted to do: To click on a link, and that link to "read" an external html file, that it will be loaded in a div section.
was called, the script did not work.
The script must be coded using document.addEventListener('DOMContentLoaded', function() as it was told
如前所述,由于安全原因,Chrome Extensions不允许使用内联JavaScript,因此您也可以尝试此解决方法。
html文件
popup.js
或如果您包含jQuery文件,则
As already mentioned, Chrome Extensions don't allow to have inline JavaScript due to security reasons so you can try this workaround as well.
HTML file
popup.js
Or if you are having a Jquery file included then