【问题标题】:How I can execute JavaScript in popup.html [duplicate]如何在 popup.html 中执行 JavaScript [重复]
【发布时间】:2017-09-10 04:34:46
【问题描述】:

它在普通 HTML 页面上运行良好,但在我的 Chrome 扩展程序上不起作用。

我收到此错误:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

清单:

{
  "name": "script",
  "version": "1.3.2",
  "manifest_version": 2,
  "description": "script",
    "icons": {
    "16": "assets/immagine.png",
    "128": "assets/immagine.png"
  },
  "permissions":[
    "tabs", "storage"
   ],
  "browser_action": {
          "default_icon": {               
            "16": "assets/immagine.png",        
            "24": "assets/immagine.png",       
            "32": "assets/immagine.png"            
          },
         "default_popup": "popup.html",
         "scripts": ["calculator.js"]
   }

}

popup.html:

<html>
    <head>
        <script>

        var ammontare = 0 ;
        var npack = 0;
        var nprel = 0;
        var minprelievo = 0;

        function c(val)
        {
            document.getElementById("d").value=val;

        }

        function math(val)
        {
            document.getElementById("d").value+=val;
        }
        function e(pack)
        {   
            ammontare = document.getElementById("e").value;

            if (pack == 20) nprel = 5;
            if (pack == 30) nprel = 4;
            if (pack == 50) nprel = 6;
            if (pack == 70) nprel = 2;
            if (pack == 100) nprel = 3;
            var fut = (Number(ammontare)+Number(ammontare/4));

            var npack = (Number(ammontare)/Number(pack));
            nprel = (npack*nprel)+5;

            var minprelievo = (fut*nprel);
            var minprelievo = minprelievo.toFixed( 2 );

            try
            {
              c(minprelievo)
            }
            catch(e)
            {
              c('Error')
            }
        }


        </script>
    </head>

<body>
        INSERT Value:   
    <p><div class="display"><input type="number" size="15.75" id="e" > $</div></p>
        PRESS BUTTON: 
    <p>
        <input type="button" class="button gray" value="BUTTON1" onclick='e(20)'>
        <input type="button" class="button gray" value="BUTTON2" onclick='e(30)'>
        <input type="button" class="button gray" value="BUTTON3" onclick='e(50)'>
        <input type="button" class="button gray" value="BUTTON4" onclick='e(70)'>
        <input type="button" class="button gray" value="BUTTON5" onclick='e(100)'>  
     </p>


    <p>RESULTS:</p>
    <p><div class="display"><input type="number" readonly size="15.75" id="d"> $</div></p>

</body>
</html>

【问题讨论】:

    标签: javascript html google-chrome google-chrome-extension


    【解决方案1】:

    将 Javascript 代码放在文件代码 popup.js 上,并将其放在与 popup.html 相同的文件夹中

    然后加载它:&lt;script src="popup.js"&gt;&lt;/script&gt;

    这是因为内容安全政策: https://developer.chrome.com/extensions/contentSecurityPolicy

    【讨论】:

    • 嗨,我做对了,但是没有用
    • 还是同样的错误还是不同的错误?新代码怎么样?
    猜你喜欢
    • 2015-08-23
    • 2017-09-24
    • 2014-06-11
    • 2019-07-25
    • 2013-03-05
    • 2012-10-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多