How to get notified on window resize in chrome browser

有些话、适合烂在心里 提交于 2019-12-07 09:51:29

The background.js file cannot capture resize events in the browser. You would need to inject a content script for that.

NVI

Chrome-o-Tile is one example of an extension which listens resize in its content script.

In manifest.json:

"content_scripts": [
  {
    "js": ["contentscript.js"],
    "run_at": "document_start",
    "matches": [
      "<all_urls>"
    ]
  }
],

In contentscript.js:

'use strict';

var timeoutId = 0;

window.addEventListener('resize', function() {
  if (timeoutId) {
    clearTimeout(timeoutId);
  }
  timeoutId = setTimeout(function() {
    chrome.runtime.sendMessage({method: 'resize'});
    timeoutId = 0;
  }, 100);
}, false);

In background.js:

chrome.runtime.onMessage.addListener(function requested(request) {
  if (request.method === 'resize') {
    ...
  }
)};

There is also an open issue to implement chrome.windows.onResize event for Chrome extensions.

You can use jQuery's bind() function.. http://api.jquery.com/bind/

$(window).bind('resize', function () {
//do something here
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!