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'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
2 Answers
Reset to default 2Found 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);});
- VMware宣布与Yahoo收购电子邮件和协作软件提供商Zimbra的协议
- 拍照比“剪刀手”注意了!专家:会泄露指纹信息
- 2015年形成的计算机发展趋势 Windows10依然强势
- 虚拟现实技术2015年将迎来爆发
- 鲍尔默:未来5至10年微软将不再像一家软件公司
- 微软Surface平板电脑 是矛盾的存在吗?
- Azure web app High availability with DNS failover - Stack Overflow
- javascript - How can you use a React hook conditionally in a function component - Stack Overflow
- powerbi - Keep Date Filter Active After Hiding Matrix-Based Calendar in Report Viewer - Stack Overflow
- android - Unity Ads fail to initialize - Stack Overflow
- typescript - Declare object and internally refer to other values - Stack Overflow
- using Okta principal groups and metadata in Spring Boot with Okta Spring Boot - Stack Overflow
- netlogo - Indirect allocation of resources than intended - Stack Overflow
- pytorch - how to get custom column in the model's forward() function when training with Huggingface Trainer? - Stack Ove
- docker - Pulling AWS Parameter Store secrets into a deployed Dockerized app on EC2 - Stack Overflow
- Cannot open DocumentPicker window in React Native app, Android platform - Stack Overflow
- scipy - Problem with a simple script in which the Librosa Python library does not work well for me - Stack Overflow