只允许一个Chrome扩展程序的活动实例 [英] Only allow one active instance of a Chrome extension

查看:116
本文介绍了只允许一个Chrome扩展程序的活动实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Chrome扩展程序一次只能在每台机器的一个窗口中打开。
执行此条件的最佳方法是什么?例如,是否有一种机制将用户指向运行扩展名的现有选项卡(如果存在此类选项卡)?

我的清单文件的相关部分是如下所示:

manifest.json

  {

manifest_version:2,

browser_action:{
default_icon:/img/favicon.ico,
popup:main.html
},

background:{
scripts:[open.js]
}




$ b $ open.js 的内容如下:

open.js

  chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.create({'url':chrome.extension.getURL('test.html')},function (tab){
});
});


解决方案

更新包括专注于不同窗口中的标签



因为没有人在这里发布这个问题的答案,它是从Rob W的github中获取的, https:// github.com/Rob--W/stackexchange-notifications/blob/8947b9982cd7b9e04ccf0ef23510571f39d33c4e/Chrome/using-websocket.js#L66-L82 的。



这是一个更基本的版本,用基本命令替换原始代码中的openTab()函数,打开一个新标签。

  var options_url = chrome.extension.getURL('options.html'); 
chrome.tabs.query({
url:options_url
},函数(tabs){
if(tabs.length == 0){
chrome.tabs .create({url:main.html});
} else {
//如果有多于一个,关闭除第一个
之外的全部(var i = 1; i< ; tabs.length; i ++)
chrome.tabs.remove(tabs [i] .id);
//将选项页面集中到
chrome.tabs.update(tabs [0] .id,{active:true});
chrome.windows.update(tabs [0] .windowId,{focused:true})
}
});


I have a Chrome extension which needs to be open only in one window per machine at a time. What would be the best way to enforce this condition? For example, is there a mechanism to point the user to an existing tab running the extension, if there exists such a tab?

The relevant parts of my manifest file are as follows:

manifest.json

{

    "manifest_version": 2,

    "browser_action": {
        "default_icon": "/img/favicon.ico",
        "popup": "main.html"
    },

    "background": {
      "scripts": ["open.js"]
    }

}

And the open.js reads as follows:

open.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.create({'url': chrome.extension.getURL('test.html')}, function(tab) {
  });
});

解决方案

Update Updated to also include focusing on a tab that is in a different window

Since no one posted the answer to this question here it is, taken from Rob W's github as linked in the comments under the question https://github.com/Rob--W/stackexchange-notifications/blob/8947b9982cd7b9e04ccf0ef23510571f39d33c4e/Chrome/using-websocket.js#L66-L82.

This is a more basic version replacing the openTab() function found in the original code with the basic command to open a new tab.

var options_url = chrome.extension.getURL('options.html');
chrome.tabs.query({
    url: options_url
}, function(tabs) {
    if (tabs.length == 0) {
        chrome.tabs.create({ url: "main.html" });
    } else {
        // If there's more than one, close all but the first
        for (var i=1; i<tabs.length; i++)
            chrome.tabs.remove(tabs[i].id);
        // And focus the options page
        chrome.tabs.update(tabs[0].id, {active: true});
        chrome.windows.update(tabs[0].windowId, {focused:true})
    }
});

这篇关于只允许一个Chrome扩展程序的活动实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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