![](/img/trans.png)
[英]Check if document already exists and if yes then update, otherwise create new - Mongoose
[英]Check if tab exists and make it active; otherwise create it
我正在嘗試查看標簽“ http://google.com ”是否存在。 如果是,那么我想將其設為活動頁面。 否則,標簽“ http://google.com ”不存在,我想創建它。
后台.js
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
chrome.tabs.create({'url': 'http://google.com'}, function(tab) {
alert('Tab Created ' + tab.id);
var oms = tab.id;
chrome.tabs.update(oms, {url:"http://en.wikipedia.org"});
});
});
這將創建網頁,獲取tabid
並設置tabid
作為變量。
您當前的嘗試似乎是嘗試使用 Google 創建一個選項卡,然后將其導航到 Wikipedia。 這與您的初始段落不一致。 相反,我認為您需要類似於以下內容的內容:
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
chrome.tabs.query({'url': 'http://google.com'}, function(tabs) {
if ( tabs.length > 0 ) {
chrome.tabs.update(tabs[0].id,{'active':true});
} else {
chrome.tabs.create({'url':'http://google.com'});
}
});
});
請注意,這不會找到http://www.google.com或http://google.com/otherstuff 。 您可能想要使用匹配模式。
您評論了更新要selected
的選項卡時遇到問題。 文檔說selected
已被棄用,而支持highlighted
。 這也暗示了其他可能發生的事情: highlighted
選項卡不一定處於活動狀態(特別是,您可以highlighted
多個選項卡)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.