运行JavaScript并点击Chrome扩展程序中的popup.html图标 [英] Run javascript with click on popup.html icon in Chrome extension

查看:1008
本文介绍了运行JavaScript并点击Chrome扩展程序中的popup.html图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是简单的。用户点击扩展的图标,并执行一个JS代码,显示一个提示框询问两个值。但我无法弄清楚如何正确链接JS与popup.html。到目前为止,只需点击图标,弹出窗口就会打开,不会运行JS代码。



popup.html

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascriptsrc =prompt.js>< / script>
< / head>
< body onload =promptBox()>
< / body>
< / html>

prompt.js

  function promptBox(){
prompt('Choose File 1',A14nH);
R1Gh7 =提示('选择文件2',L3f7);
if(L3f7&& R1Gh7){
Fr4Q ='< frameset cols = \'*,* \\''> \\\
Fr4Q + ='< frame src = \''+ R1Gh7 +'\'/> \\\
';
Fr4Q + ='< / frameset>';
with(document){
write(Fr4Q);
void(close())
}
}
else {
void(null)
};


解决方案

Chrome扩展程序。

右键单击扩展程序图标,然后单击检查弹出窗口。您将看到以下内容:
$ b


拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:script-src你需要删除 onload =promptBox()。 ,然后将监听器添加到 prompt.js 中,而不是:

  function promptBox(){
// ...
}

document.addEventListener('DOMContentLoaded',function(){
promptBox();
});


What I want is simple. The user clicks on the icon of the extension and a JS code is executed showing a prompt box asking for two values. But I cannot figure out how to link the JS with the popup.html correctly. So far with the click on the icon only the popup opens without running the JS code.

popup.html

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="prompt.js"></script>
    </head>
    <body onload="promptBox()">
    </body>
</html>

prompt.js

function promptBox() {
    prompt('Choose File 1',A14nH);
    R1Gh7=prompt('Choose File 2',L3f7);
    if(L3f7&&R1Gh7) {
        Fr4Q='<frameset cols=\'*,*\'>\n<frame src=\''+L3f7+'\'/>';
        Fr4Q+='<frame src=\''+R1Gh7+'\'/>\n';
        Fr4Q+='</frameset>';
        with(document) {
            write(Fr4Q);
            void(close())
        }
    }
    else{
       void(null)
    };
}

解决方案

You cannot run inline handlers within chrome extensions.
Do a right click on your extensions icon and click "Inspect Popup". You'll see the following:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

You need to remove onload="promptBox()" from your body and add an listener to your prompt.js instead:

function promptBox() {
  // ...
}

document.addEventListener('DOMContentLoaded', function() {
  promptBox();
});

这篇关于运行JavaScript并点击Chrome扩展程序中的popup.html图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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