javascript - How to resize Chrome browser window with an extension - Stack Overflow

I am making a chrome extension and wanted to add an option to resize the browser window.I know I can&#

I am making a chrome extension and wanted to add an option to resize the browser window. I know I can't resize the window with normal JS.(window.resizeTo(600, 600); etc. won't work)

But with extension it's possible. For example with this tool you can resize the window. Problem is that I don't know how.

Also seems possible to open a new tab with desired sizes but it won't open a normal window but a tab.(Like ads)

Does anyone know how to achieve this?

I am making a chrome extension and wanted to add an option to resize the browser window. I know I can't resize the window with normal JS.(window.resizeTo(600, 600); etc. won't work)

But with extension it's possible. For example with this tool you can resize the window. Problem is that I don't know how.

Also seems possible to open a new tab with desired sizes but it won't open a normal window but a tab.(Like ads)

Does anyone know how to achieve this?

Share Improve this question asked Feb 18, 2021 at 12:03 sunflower seedsunflower seed 3037 silver badges19 bronze badges 1
  • Here's a repo for that. Tested ✅ – Unknown Commented Apr 24, 2023 at 13:56
Add a ment  | 

2 Answers 2

Reset to default 2

Found an answer:

We need 2 js files. background.js and main.js(content js file, name can be anything).

main.js doesn't have access to extension apis, tabs, background stuff etc. So we will send a message to background.js from our main.js file and get that message in background.js file and execute what we want.

main.js:

chrome.runtime.sendMessage(
  {
    action: "resizeWindow",
  },
  function (createdWindow) {
    console.log("Window Resize");
  }
);

background.js:

  //Windows resize
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if (request && request.action === "resizeWindow") {
    chrome.windows.getCurrent(function (window) {
      var updateInfo = {
        width: 1200,
        height: 710,
      };
      (updateInfo.state = "normal"), chrome.windows.update(window.id, updateInfo);
    });
  }
});

Note that we need updateInfo.state = "normal".

You can try the window API for chrome extensions


chrome.windows.getCurrent(function(wind) {
alert(wind.id);
var maxWidth = window.screen.availWidth;
var maxHeight = window.screen.availHeight;
var updateInfo = {
    left: 0, //change those to whatever you like 
    top: 0,
    width: maxWidth,
    height: maxHeight
};
chrome.windows.update(wind.id, updateInfo);});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745670129a4639353.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信