简体   繁体   English

Google Chrome扩展程序的选项页中的Javascript不起作用

[英]Javascript is not working in the options page of the Google Chrome extension

I am developing an extension for google chrome. 我正在为Google Chrome开发扩展程序。 But I'm having trouble using localStorage and also is not working javascript. 但是我在使用localStorage时遇到了麻烦,并且无法正常运行javascript。

See the code below: 请参见下面的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="pt-br">
    <head>
        <script type="text/javascript">
            function ShowAlert() {
                alert("This is a alert!");
            }
        </script>
    </head><body>
        <script>
            ShowAlert();
        </script>
    </body>
</html>

This is a simple code that displays a message, it works, I've tested. 经过测试,这是一个显示消息的简单代码,它可以正常工作。 But when I access as an options page of my extension, the message will not appear. 但是,当我以扩展程序的选项页访问时,该消息将不会出现。

Have any permission that should be missing in manifest for this to work? 清单中应该缺少任何许可才能起作用吗?

You cannot execute inline scripting, this is a Content Security Policy 您无法执行内联脚本,这是内容安全策略

Inline JavaScript will not be executed. 内联JavaScript将不会执行。
This restriction bans both inline blocks and inline event handlers (eg < button onclick = "..." > ). 此限制禁止内联块和内联事件处理程序(例如<button onclick =“ ...”>)。

The restriction wipes out a huge class of cross-site scripting attacks by making it impossible for you to accidentally execute script provided by a malicious third-party. 该限制使您无法意外执行恶意第三方提供的脚本,从而消除了一大堆跨站点脚本攻击。 It does, however, require you to write your code with a clean separation between content and behavior. 但是,它确实要求您编写代码时必须将内容和行为区分开。

Change your code as follows: 更改您的代码,如下所示:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="pt-br">
    <head>
        <script src="alerter.js"></script>
    </head>
    <body>
    </body>
</html>

and separate code in alerter.js 和alerter.js中的单独代码

function ShowAlert() {
    alert("This is a alert!");
}
ShowAlert();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM