你可以在页面动作弹出窗口中使用JavaScript吗? [英] Can you use javascript in a page action popup?

查看:96
本文介绍了你可以在页面动作弹出窗口中使用JavaScript吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试构建一个使用页面动作弹出功能的Chrome扩展,但是我的JavaScript似乎都没有工作。这是源代码:

 <!DOCTYPE html> 
< html>
< body>
< input type =buttonid =button1value =first button>
< input type =buttonid =button2value =second button>
< script>
console.log(做任何事!!);
< / script>
< / body>
< / html>

我无法在任何地方看到记录器语句。我似乎无法获得任何运行。这是弹出如何工作?文档中没有提到这些弹出窗口只是静态HTML。

首先, Chrome扩展程序是不允许的运行内嵌Javascript代码(也就是任何不包含在.js文件内但在DOM中的代码段)。



内容安全政策页面引用


没有任何机制可以放宽对执行内嵌JavaScript的限制。特别是,设置包含'不安全内联'的脚本策略将无效。


因此,您需要将您的代码在另一个文件中并导入它,如下所示:

 < sctipt src =popup.js>< /脚本> 

其次,运行到弹出窗口的代码记录在自己的控制台中,所以你必须右键点击你的分机的图标,然后点击检查弹出。然后,会出现一个开发人员工具窗口,您将可以浏览到控制台选项卡并查看日志。或者,您可以打开弹出窗口并在其内​​部的任意位置单击鼠标右键,然后选择检查元素,就像在任何常规页面上一样。 $ b


I'm trying to build a chrome extension that uses the page action popup feature, but none of my javascript seems to be working. Here is the source:

<!DOCTYPE html>
<html>
    <body>
        <input type="button" id="button1" value="first button">
        <input type="button" id="button2" value="second button">
        <script>
           console.log("do anything!!");
        </script>
    </body>
</html>

I can't see the logger statement anywhere. I can't seem to get anything to run. Is this how popups are supposed to work? The docs make no mention of these popups being static HTML only.

解决方案

First of all, Chrome extensions are not allowed to run inline Javascript Code (aka any piece of code that is not included inside a .js file, but in the DOM).

Quoting from the Content Security Policy page:

There is no mechanism for relaxing the restriction against executing inline JavaScript. In particular, setting a script policy that includes 'unsafe-inline' will have no effect.

Therefore, you'll need to put your code inside another file and import it, like this:

<sctipt src="popup.js"></script>

Secondly, the code you run into the popup is logged in its own console, so you have to right-click on your extension's icon, and hit "Inspect popup". Then, a developer tools window will appear, and you'll be able to browse to the "Console" tab and see the log. Alternatively, you can open the popup and right click anywhere inside it, then choose "Inspect element", like on any normal page.

这篇关于你可以在页面动作弹出窗口中使用JavaScript吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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