Chrome扩展程序:browserAction.onClicked.addListener()未被调用 [英] Chrome Extension: browserAction.onClicked.addListener() not being called

查看:135
本文介绍了Chrome扩展程序:browserAction.onClicked.addListener()未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个非常简单的Chrome扩展程序。所有这一切都是在浏览器操作图标被点击时尝试显示警报的弹出html文件。我显然做错了,因为警报不会触发。



manifest.json

  {
name:Simple,
description:Simple,
version:1.0,
browser_action :{
default_title:简单,
default_icon:images / icon.png,
default_popup:popup.html
},
manifest_version:2
}

popup.html

 < html> 
< head>
< script>
//点击扩展的图标时执行
chrome.browserAction.onClicked.addListener(函数(标签)
{
alert(gah);
} );
< / script>
< / head>

< body>
Hello World!
< / body>
< / html>

我也试过:

 < HTML> 
< head>
< script>

function onPageLoad()
{
//单击扩展的图标时执行
chrome.browserAction.onClicked.addListener(function(tab)
{
alert(gah);
});
}

< / script>
< / head>

< body onload =onPageLoad()>
Hello World!
< / body>
< / html>

基于响应的更新感谢您的回应。我做了以下更改,但仍然没有被称为browser.Action.onClicked()(你可以看到,而不是一个警报我使用console.log()。(在全局范围内的日志显示,里面的一个

manifest.json

  {
name:Simple,
description:Simple,
version:1.0,
permissions:[tabs,http :// * / *,https:// * / *],
background:{
persistent:false,
scripts:[popup。 js]
},
browser_action:{
default_title:简单,
default_icon:images / icon.png,
default_popup:popup.html
},
manifest_version:2
}

popup.html

 < html> 
< head>
< script type =text / javascriptsrc =popup.js>< / script>
< / head>

< body>
< div style =white-space:nowrap>
你好,世界!
< / div>
< / body>
< / html>



popup.js

  console.log(在全局范围运行)

//单击扩展的图标时执行
chrome.browserAction.onClicked.addListener(function(tab)
{
console.log(运行addListener()的insode);
});


解决方案

chrome.browserAction.onClicked 不能在弹出窗口中使用。它没有多大意义 - 你开始监听浏览器动作图标,点击后它已被点击。它可以再次点击,但弹出窗口会关闭,监听器会在发生任何事情之前终止。
chrome.browserAction.onClicked 应该在后台页面上使用,并且只有在不使用浏览器动作的弹出窗口时:


如果浏览器操作弹出窗口,此事件不会触发。




您应该只是写:

 < script> 
alert(gah);
< / script>

但我不确定警报是否在弹出窗口中工作。最好尝试一下:

 < script> 
document.getElementsByTagName('body')[0] .innerHTML =gah;
< / script>

编辑

<在扩展中还有一件事需要解决。正如@Cody在他的回答中所建议的,您不应该使用内联脚本。它出于安全原因被阻止。把我上面建议的代码放到一个单独的JavaScript文件中,然后将它包含在 popup.html 头部:

 < script src ='script.js'type ='text / javascript'>< / script> 


I am trying to write a very simple Chrome extension. All it is at this point is a popup html file that tries to display an alert when the browser action icon is clicked. I am obviously doing something wrong because the alert doesn't fire.

manifest.json

{
  "name": "Simple",
  "description": "Simple",
  "version": "1.0",
  "browser_action": {
    "default_title": "Simple",
    "default_icon": "images/icon.png",
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html

<html>
<head>
    <script>
        //Executed when the extension's icon is clicked
        chrome.browserAction.onClicked.addListener(function(tab) 
        { 
            alert("gah");
        });
    </script>
</head>

<body>
    Hello World!
</body>
</html>

I have also tried:

<html>
<head>
    <script>

        function onPageLoad()
        {
            //Executed when the extension's icon is clicked
            chrome.browserAction.onClicked.addListener(function(tab) 
            { 
                alert("gah");
            });
        }

    </script>
</head>

<body onload="onPageLoad()">
    Hello World!
</body>
</html>

UPDATE BASED UPON RESPONSES Thank you for your response. I've made the following changes but is still not being called browser.Action.onClicked() (you can see that instead of an alert I am using console.log(). (The log at global scope is displayed, the one inside the callback is not).

manifest.json

{
  "name": "Simple",
  "description": "Simple",
  "version": "1.0",
  "permissions": ["tabs", "http://*/*", "https://*/*"],
  "background": {
    "persistent": false,
    "scripts": ["popup.js"]
  }, 
  "browser_action": {
    "default_title": "Simple",
    "default_icon": "images/icon.png",
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html

<html>
<head>
    <script type="text/javascript" src="popup.js"></script>
</head>

<body>
    <div style="white-space: nowrap">
        Hello World!
    </div>
</body>
</html>

popup.js

console.log("Running at global scope")

//Executed when the extension's icon is clicked
chrome.browserAction.onClicked.addListener(function(tab) 
{ 
    console.log("Running insode of addListener()");
});

解决方案

chrome.browserAction.onClicked can't be used inside a popup. It doesn't make much sense - you start listening for browser action icon being clicked after it was already clicked. It can be clicked again, but then popup will be closed and listener will be terminated before anything happens. chrome.browserAction.onClicked is supposed to be used on the background page and only when popup for browser action is not used:

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

source

You should probably just write:

<script>
        alert("gah");
</script>

But I'm not sure if alerts work in a popup. Better try something like:

<script>
        document.getElementsByTagName('body')[0].innerHTML = "gah";
</script>

EDIT

There is one more thing to fix in your extension. As @Cody suggested in his answer, you shouldn't have used inline script. It is blocked for security reasons. Take the code I suggested above and put it into a separate javascript file, then include it in popup.html head:

<script src='script.js' type='text/javascript'></script>

这篇关于Chrome扩展程序:browserAction.onClicked.addListener()未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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