为Chrome创建扩展程序,只需运行后台脚本而无需用户干预 [英] Create an extension for Chrome to just run a background script without user intervention

查看:307
本文介绍了为Chrome创建扩展程序,只需运行后台脚本而无需用户干预的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的扩展,它在没有用户干预的情况下运行脚本。脚本会自动创建通知,用户必须不做任何事情,只能安装扩展。



我的 background.js 看起来像,

  chrome.alarms.create(myAlarm,{
periodInMinutes:1
});

chrome.alarms.onAlarm.addListener(function(alarm){
if(alarm.name ===myAlarm){
alert(Tick ... );
}
});

所以在这种情况下,没有使用浏览器操作按钮 EM> 即可。无论如何,如果我删除了下面的部分,扩展名将不起作用( alert'Tick'不会弹出)。

 browser_action:{
default_icon:icon.png}

如果我只是像下面那样,那么仍然会有一个没有图像的图标((无论如何,'Tick'的提示会弹出)))。

 browser_action:{
}

现在我所拥有的是( alert'Tick'会弹出),

  {
name:myExt,
version:1.0,
manifest_version: 2,
background:{
scripts:[background.js]
},
browser_action:{
default_icon: icon.png

权限:[
https://www.google.com/,
通知,
警报

}

我需要做的是 background.js 文件。它将使用 chrome.alarms 来创建定期通知。

所有我想要的只是在后台运行脚本,有关如何从菜单栏中删除浏览器操作图标的任何想法?

解决方案

必须知道,由于Google本身的一些政策,现在只有用户有责任隐藏图标。安装扩展程序的用户可以通过右键单击并选择在Chrome浏览器菜单中隐藏来完成此操作。

所以开发人员无法隐藏菜单图标。


I'm trying to create a simple extension which run a script without user intervention. Script will automatically create notifications and user has to do nothing but install the extension.

What my background.js looks like,

chrome.alarms.create("myAlarm", {
  periodInMinutes : 1
});

chrome.alarms.onAlarm.addListener(function(alarm) {
  if (alarm.name === "myAlarm") {
    alert("Tick...");
  }
});

So in this case there is no use of a browser action button. Anyway if I remove the below part the extension will not work (alert with 'Tick' will NOT popup).

"browser_action": {
"default_icon": "icon.png"}

If I just have like below then there will still be an icon without an image ( (Anyway alert with 'Tick' will popup)).

"browser_action": {
}

For now what I have is this (alert with 'Tick' will popup),

{
  "name": "myExt",
  "version": "1.0",
  "manifest_version": 2,
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
   "default_icon": "icon.png"
  },
  "permissions": [
    "https://www.google.com/",
    "notifications",
    "alarms" 
  ]  
}

All the thing I need to do is in the background.js file. It will use the chrome.alarms to create periodic notifications.

So as all I want is to just run a script in background, Any idea on how to remove that browser action icon from the menu bar ?

解决方案

Got to know that due to some policies by the Google itself, now only the user is responsible to hide the icon. User who installed the extension can do this by right-clicking and selecting "Hide in Chrome Menu".

So there is no way to hide the menu icon by the developer.

这篇关于为Chrome创建扩展程序,只需运行后台脚本而无需用户干预的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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