Google Chrome扩展程序问题:popup.html干扰了background.html中的脚本执行 [英] Google chrome extension issue: popup.html interferes with script execution in background.html

查看:137
本文介绍了Google Chrome扩展程序问题:popup.html干扰了background.html中的脚本执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何扩展Google chrome,但遇到了以下问题:

I am learning how to extend Google chrome and I have run into the following problem:

我有以下清单文件:

{
    "name": "My First Extension",
    "version": "1.0",
    "description": "The first extension that I made.",
    "background_page": "background.html",
    "browser_action": {
        "default_icon": "icon.png", 
        "popup": "popup.html"
     },
    "permissions": [
    "tabs",
        "http://*/*",
        "https://*/*"
    ]
 }

我的background.html文件只是将一些简单的JavaScript注入页面:

My background.html file just injects some simple JavaScript into the page:

<script>
    // Called when the user clicks on the browser action.
    chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});
    });
</script>

我的popup.html文件只是简单的HTML:

My popup.html file is just simple HTML:

<body>
    Sup Playa
</body>

background.html对话框永远不会显示. popup.html可以正常运行.

The dialog box from background.html never gets displayed. popup.html functions as expected.

但是,当我从清单文件中注释掉popup.html时,background.html中的脚本可以工作.

However when I comment out popup.html from the manifest file, the script in background.html works.

我做错了什么?为什么没有同时显示对话框和弹出窗口?

What am I doing wrong? Why aren't both the dialog box and the popup showing up?

推荐答案

如果浏览器操作弹出,则不会触发

onClicked事件.

onClicked event will not fire if the browser action has a popup.

因此,如果您将任何html文件分配给弹出式窗口"popup": "popup.html"(而不是仅一个没有正文的按钮),则不会触发onClicked事件.

So if you assign any html file to your popup "popup": "popup.html" (rather than just a button without body), onClicked event isn't fired.

如果您希望每次打开代码时都可以执行某些操作,则可以直接将代码放入popup.html(它与后台页面具有相同的特权).

You can just put your code right into popup.html (it has same privileges as a background page) if you want something to be executed each time it is opened:

chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});

这篇关于Google Chrome扩展程序问题:popup.html干扰了background.html中的脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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