0

我想提出一個Chrome擴展開。點擊popup.html中的按鈕打開一個新窗口並加載feedback-panel.html檢查窗口已經從非父窗口(Chrome擴展)

這工作,但上點擊,我想檢查窗口已經打開,如果是集中於它,而不是創建一個新的。

JavaScript window.open only if the window does not already exist看上去很不錯,但它依賴打開的窗口作爲變量存儲在父頁面上,當打開窗口並在打開窗口之前檢查它們。這不會爲我工作,因爲父窗口(popup.html)經常會被關閉並重新打開,我會失去變量。

我試圖實現相同的想法,但窗口變量存儲與chrome.storage,因爲它可以讓你存儲對象。那麼,它讓你存儲的對象,但它首先序列化他們,所以窗口變量失去它的所有功能,我結束了

result.feedbackPanel.focus()不是一個函數

這裏是我的嘗試:

function openFeedbackPanel(){ 
    chrome.storage.local.get('feedbackPanel', function (result) { 
     console.log(result.feedbackPanel); // logs window object sans all functions 
     if(result.feedbackPanel && ! result.feedbackPanel.closed){ 
      try{ 
       result.feedbackPanel.focus(); 
      } 
      catch(error){ 
       chrome.storage.local.remove('feedbackPanel', function() { 
       }); 
       createFeedbackPanel(); 
      } 
     } 
     else{ 
      createFeedbackPanel(); 
     } 
    }); 
} 
function createFeedbackPanel(){ 
    var win = window.open('feedback-panel.html', 'Feedback', 'width=935, height=675'); 
    console.log(win); // logs full object as expected 
    chrome.storage.local.set({"feedbackPanel": win}); 
} 



$('#some-button').click(openFeedbackPanel()); 

所以,因爲這並不工作:

如何檢查彈出窗口是否已經從非父窗口(未打開彈出窗口)打開?

回答

2

不需要跟蹤窗口和存儲。
如果你知道你的擴展ID,最簡單的方法是測試所有選項卡的URL,看看它是否已經打開

chrome.tabs.query({}, function(tabs) { 
    var doFlag = true; 
    for (var i=tabs.length-1; i>=0; i--) { 
    if (tabs[i].url === "chrome-extension://EXTENSION_ID/feedback-panel.html") { 
     //your popup is alive 
     doFlag = false; 
     chrome.tabs.update(tabs[i].id, {active: true}); //focus it 
     break; 
    } 
    } 
    if (doFlag) { //it didn't found anything, so create it 
    window.open('feedback-panel.html', 'Feedback', 'width=935, height=675'); 
    } 
}); 

這裏已經回答瞭如何讓extension ID

+0

這看起來promissing但遺憾的是它不工作。當頁面未打開時,它會打開它,但打開時沒有任何反應。我設置了一個斷點,代碼被稱爲'chrome.tabs.update(tabs [i] .id,{active:true});'並且我有正確的標籤ID,但是窗口沒有獲得焦點。我認爲這是因爲彈出窗口本身仍然是開放的 – DelightedD0D

+0

@ DelightedD0D我在Opera瀏覽器的win10上測試了它,彈出窗口集中(從背景)。也許它是不同的平臺行爲? –

+0

嗯,沒有意識到你可以在opera中安裝chrome擴展,你可以在實際的chrome中測試嗎?編號愛,以確認它的工作原理,所以我可以專注於理由雷犯規 – DelightedD0D

1

您也可以使用消息系統。這是我用於擴展選項的示例。調用這樣的功能在onclick的按鈕:

// send message to the option tab to focus it. 
    // if not found, create it 
    showOptionsTab: function() { 
     chrome.runtime.sendMessage({window: 'highlight'}, null, function(response) { 
      if (!response) { 
       // no one listening 
       chrome.tabs.create({url: '../html/options.html'}); 
      } 
     }); 
    }, 

而在你的窗口/標籤聽這樣這種方法不需要的選項卡

// listen for request to make ourselves highlighted 
chrome.runtime.onMessage.addListener(t.onMessage); 

... 

// message: highlight ourselves and tell the sender we are here 
t.onMessage = function(request, sender, response) { 
    if (request.window === 'highlight') { 
     chrome.tabs.getCurrent(function(t) { 
      chrome.tabs.update(t.id, {'highlighted': true}); 
     }); 
     response(JSON.stringify({message: 'OK'})); 
    } 
}; 

一個優點消息允許。