Background.js无法使用Chrome扩展程序 [英] Background.js not working Chrome Extension

查看:260
本文介绍了Background.js无法使用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:我的第一个打包应用程序。,
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
},
图标:{
16:icon16.png,
48:icon48.png,
128:icon128.png



$ div $解析方案

它是以下面的代码为我工作。



manifest.json



  { 
name:Popping Alert,
descrip和:http://stackoverflow.com/questions/15194198/background-js-not-working-chrome-extension,
background:{
scripts:[
background.js
]
},
version:1,
manifest_version:2,
browser_action:{
default_title:点击我
}
}



背景.js



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

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


I'm new to chrome extensions and cannot seem to figure out how the background concept works. 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. I cannot figure out why this doesn't work.

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"
  }
}

解决方案

It is working for me with following code.

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

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?

这篇关于Background.js无法使用Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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