简体   繁体   English

Background.js无法使用Chrome扩展程序

[英]Background.js not working Chrome Extension

I'm new to chrome extensions and cannot seem to figure out how the background concept works. 我是Chrome扩展程序的新手,似乎无法弄清楚背景概念是如何工作的。 I am building a counter extension that keeps counting even when the user closes the extension (but not the browser) and wanted to do a simple test to see if I could figure out how to use the background file. 我正在构建一个计数器扩展,即使用户关闭扩展(但不是浏览器),并且想要做一个简单的测试,看看我是否可以弄清楚如何使用后台文件。 Below is my attempt to create a function that activates everytime a user clicks on a tab (outside of my extension) and when they click on 5 tabs, the alert hits. 下面是我尝试创建一个功能,每当用户点击选项卡(我的扩展名之外)时激活该功能,当他们点击5个选项卡时,警报就会响起。 I cannot figure out why this doesn't work. 我无法弄清楚为什么这不起作用。

background.js: background.js:

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

manifest.json: 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"
  }
}

It is working for me with following code. 它通过以下代码为我工作。

manifest.json 的manifest.json

{
    "name": "Popping Alert",
    "description": "http://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 background.js

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

Can you share your related code or put your problem statement clearly if this does not work? 如果这不起作用,您可以分享您的相关代码或清楚地提出您的问题陈述吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 background.js 中的 Chrome 扩展中的 setInterval - setInterval in Chrome extension in background.js chrome 扩展中 background.js 的用途是什么? - What is the purpose of background.js on a chrome extension? Background.js中的Chrome扩展Keydown侦听器 - Chrome extension keydown listener in background.js Chrome扩展程序在Background.js中注册事件 - Chrome Extension Registering Events in Background.js chrome.notifications.onClosed不在background.js中为google chrome扩展程序工作 - chrome.notifications.onClosed not working in background.js for the google chrome extension 我的“background.js”代码用于获取选定的 chrome 标签 url 在我的 chrome 扩展中无法正常工作 - My “background.js” code to get the selected chrome tabs url is not working properly in my chrome extension Chrome 扩展消息传递不起作用(background.js 到 content.js) - Chrome extension message passing not working (background.js to content.js) background.html与background.js-chrome扩展 - background.html vs. background.js - chrome extension 不会调用background.js中的Chrome扩展onMessage.addListener - Chrome Extension onMessage.addListener in background.js not being called 每次页面重新加载时,Chrome扩展程序都会加载background.js - Chrome extension load a background.js each time the page reloads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM