0

我的扩展应该打开一个弹出窗口(一个普通的弹出窗口,而不是默认的扩展弹出窗口),具有以下条件:

  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.
  });
});

有任何想法吗?

4

1 回答 1

0

我认为,您可以使用window.open().

1-此刻只能打开一个窗口,

如果你给相同name的, window.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"
};
于 2012-11-24T18:13:36.380 回答