2012-11-24 95 views
0

我的擴展應該打開一個彈出窗口(正常的彈出窗口,而不是默認擴展彈出式)具有以下條件:谷歌Chrome擴展:彈出窗口處理

  1. 只有一個窗口能在瞬間被打開,
  2. 我想在localStorage中保存窗口大小和位置,並在下次打開時恢復它們。

該怎麼辦?

我想:

chrome.browserAction.onClicked.addListener(function() { 

    // 1. In this case, variable win is a window object. But it's empty. There 
    // is no properties/methods to operate with. 
    var win = window.open('http://example.com','windowname'); 

    chrome.windows.create({ 
    'url': 'http://example.com', 
    'type': 'popup' 
    }, function(win) { 
    // 2. In this case, variable win has some properties, but they are static 
    // and won't change on window resize/close. 
    }); 
}); 

任何想法?

+0

什麼特別之處打開的窗口?該擴展程序如何知道「窗口」是否已打開?如果它基於URL,[這裏](https://github.com/Rob--W/stackexchange-notifications/blob/4f4aa9ce76bce8f9f8c4180a73c675b5d9651f86/using-websocket.js#L66-L82)就是一個例子。 –

回答

0

我想,你可以用window.open()來做到這一點。

1 - 只有一個窗口能在瞬間被打開,

如果你給同namewindow.open()

  • 如果沒有任何存在的窗口名稱相同,將創建一個新的
  • 否則如果有一個打開的窗口具有相同的名稱,它只會刷新它。

2-我想保存窗口的大小和位置在localStorage中,並在下一次打開時恢復它們。

是的,你可以通過將window.onresize事件做到這一點,

這是一個基本的例子,

chrome.browserAction.onClicked.addListener(function() { 
    var wOpt = getWindowDefaultOptions(); // options from localStorage 
    var win = window.open("http://www.example.com", "myscriptpopup", "width="+wOpt.width+", height="+wOpt.height+", top="+wOpt.top+", left="+wOpt.left); 
    win.onresize = function() { 
     var newOptions = {}; 
     newOptions.width = this.innerWidth; 
     newOptions.height = this.innerHeight; 
     newOptions.top = this.screenY; 
     newOptions.left = this.screenX; 
     setWindowDefaultOptions(newOptions); //save Options to localStorage 
    } 
}); 

//Default Options For Window 
var winOptions = { 
     "width" : "200", 
     "height" : "100", 
     "top" : "50", 
     "left" : "50" 
}; 
+0

出於某種原因,在窗口打開時,只調用了兩次win.onresize()。 – Leksat