简体   繁体   English

在内容脚本中打开弹出窗口(Chrome 扩展)

[英]Open popup in content scripts(Chrome extension)

I have a js file that should open the popup of the Chrome extension, so how can I open the popup when the user clicks on some input element with jquery.我有一个 js 文件应该打开 Chrome 扩展程序的弹出窗口,所以当用户单击带有 jquery 的某些输入元素时,如何打开弹出窗口。

the js: js:

$(document).ready(function(){
$("input").focus(function(){ 
        //open popup
});

}); });

If you really want to automatically open a "popup", you should rather convert chromium's "Extension Popup" to just a "Extension Window" that looks like a popup.如果您真的想自动打开一个“弹出窗口”,您应该将 chromium 的“扩展弹出窗口”转换为一个看起来像弹出窗口的“扩展窗口”。

For example (assuming Manifest v3):例如(假设清单 v3):

In your manifest.json , don't specify the popup.在您的manifest.json中,不要指定弹出窗口。

  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_icon": {
      "128": "icon.png"
    }
  }

Then in your background.js , listen when the user clicks on that popup, remember, in Manifest v3, background pages are just event pages, so all logic should run as if the closure is being run at that moment:然后在你的background.js中,当用户点击那个弹出窗口时,请记住,在 Manifest v3 中,背景页面只是事件页面,所以所有逻辑都应该像当时正在运行的闭包一样运行:

chrome.action.onClicked.addListener(() => {
  chrome.windows.create({
    focused: true,
    width: 400,
    height: 600,
    type: 'popup',
    url: 'popup.html',
    top: 0,
    left: 0
  },
  () => {})
})

With some math, you can position that popup in the area you want by calling window.screen通过一些数学运算,您可以通过调用 window.screen 在您想要的区域弹出window.screen

Additionally, if you just want a single instance of that popup, you can query all the extension windows that are open to see if that popup is opened.此外,如果您只想要该弹出窗口的单个实例,您可以查询所有打开的扩展 windows 以查看该弹出窗口是否打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM