当前位置: 首页 > news >正文

chrome extension创建新的window

背景

  • 在接收一段代码的时候遇到问题,创建的新的window窗口怎么都出不来,而且background对应代码在创建window完成之后获取的tab报错,并且控制台明确窗口没展示出来(有的电脑可以,有的电脑不行)。
  • 在这里插入图片描述

解决方案

  • 必须写全width\height\left\top,否则在某些电脑上面正常,在某些电脑上面不正常,和分辨率强相关,目前没找到规律
chrome.windows.create({type: 'normal',url: chrome.runtime.getURL('content.html'),width: 1000,height: 800,left: 100,top: 100},(e) => {console.log(e)chrome.tabs.update(e.tabs[0].id, { pinned: true })// chrome.tabs.update(e.id, { pinned: true })})

最优解决方案

  • 采用用户屏幕大小自动计算的方式
  • 未采纳,因为需要增加system权限,会导致插件更新的时候用户的插件自动关闭,减少用户
chrome.system.display.getInfo((displays) => {// 获取主屏幕的宽度和高度const screenWidth = displays[0].workArea.width;const screenHeight = displays[0].workArea.height;// 确保窗口不会超出屏幕的50%const windowWidth = Math.min(1000, screenWidth - 100); // 让窗口宽度不超过屏幕宽度const windowHeight = Math.min(800, screenHeight - 100); // 让窗口高度不超过屏幕高度chrome.windows.create({type: 'normal',url: chrome.runtime.getURL('content.html'),width: windowWidth,height: windowHeight,left: Math.max(0, (screenWidth - windowWidth) / 2), // 居中或者不超出屏幕top: Math.max(0, (screenHeight - windowHeight) / 2), // 居中或者不超出屏幕},(e) => {console.log(e);if (e.tabs && e.tabs.length > 0) {chrome.tabs.update(e.tabs[0].id, { pinned: true });} else {console.error('No tabs found in the created window');}});
});

http://www.mrgr.cn/news/46077.html

相关文章:

  • 明达IO:赋能工业机器人新未来
  • 【机器学习】监督学习 vs 非监督学习——如何选择合适的方法
  • 提交gitlab
  • 懂球短视频系统小程序的设计
  • 【fastjson】json对象格式化打印
  • 在Linux上离线安装mysql 基于centos7
  • PCL ICP算法实现点云精配准原理及代码
  • 【开源免费】基于SpringBoot+Vue.JS渔具租赁系统(JAVA毕业设计)
  • 《webpack深入浅出系列》
  • 编译后为什么要链接?
  • 第3天:Android应用组件
  • 深入剖析递归算法:原理、特点、应用与优化策略
  • RDD的介绍、RDD的特点、创建RDD数据
  • 语音怎么识别转成文字?5个超实用方法了解一下(技巧)
  • 5.将扩散模型应用于具有特殊结构的数据
  • Spring系列 Bean的生命周期
  • 2024双十一买什么?双11好物清单来啦,速速码住这篇!
  • JSBSim脚本运行结果
  • 什么是超平面
  • PHP中的HTTP请求:获取taobao商品数据的艺术