javascript – Background.js无效Chrome扩展程序

前端之家收集整理的这篇文章主要介绍了javascript – Background.js无效Chrome扩展程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Chrome扩展程序的新手,似乎无法弄清楚背景概念是如何工作的.我正在构建一个计数器扩展,即使用户关闭扩展(但不是浏览器),并且想要做一个简单的测试,看看我是否可以弄清楚如何使用后台文件.下面是我尝试创建一个功能,每当用户点击选项卡(我的扩展名之外)时激活该功能,当他们点击5个选项卡时,警报就会响起.我无法弄清楚为什么这不起作用.

background.js:

var counter = 0;
chrome.browserAction.onClicked.addListener(function(tab){
  counter++;
  if (counter == 5) {
    alert("Hi");
  }
});

manifest.json的:

{
  "name": "Hello World!","description": "My first packaged app.","version": "0.1","permissions": ["tabs","http://*/*"],"manifest_version":2,"content_scripts": [ {
    "js": [ "jquery-1.9.1.js","myscript.js" ],"matches": [ "http://*/*","https://*/*"]
  }],"background": {
    "scripts": [
       "background.js"
    ]
  },"browser_action": {
    "default_title": "10,000 Hours","default_icon": "icon16.png","default_popup": "index.html"
  },"icons": {
    "16": "icon16.png","48": "icon48.png","128": "icon128.png"
  }
}

解决方法

它通过以下代码为我工作.

的manifest.json

{
    "name": "Popping Alert","description": "https://stackoverflow.com/questions/15194198/background-js-not-working-chrome-extension","background": {
        "scripts": [
            "background.js"
        ]
    },"version": "1","manifest_version": 2,"browser_action": {
        "default_title": "Click Me"
    }
}

background.js

var counter = 0;
chrome.browserAction.onClicked.addListener(function (tab) {
    counter++;
    if (counter == 5) {
        alert("Hey !!! You have clicked five times");
    }
});

如果这不起作用,您可以分享您的相关代码或清楚地提出您的问题陈述吗?

原文链接:https://www.f2er.com/js/150391.html

猜你在找的JavaScript相关文章