简体   繁体   English

如何从点击按钮运行 chrome 扩展程序?

[英]How to run a chrome extension from a click button?

After a lot of times spend to find the solution, i'm here to ask your help.在花了很多时间寻找解决方案之后,我在这里寻求您的帮助。

I have a simple chrome extension, and what I try to do is simple: Detect on the page when a button is clicked, then, open the extension popup.html .我有一个简单的 chrome 扩展程序,我尝试做的很简单:单击按钮时在页面上检测,然后打开扩展程序popup.html

A good example is MetaMask, the html page can open the popup to connect the wallet, send transaction...一个很好的例子是 MetaMask,html 页面可以打开弹出窗口连接钱包,发送交易......

Thank you !谢谢 !

  1. Run a content script on the page and send a message to the background when the button is clicked.在页面上运行一个内容脚本,并在点击按钮时向后台发送一条消息。
 // Content Script button.addEventListener("click", () => { chrome.runtime.sendMessage("OpenPopup") })
  1. Catch the message in the background and create a window with your popup URL. Provide a "top" and a "left" value to place it at the top right of the users' screen.在后台捕获消息并使用弹出窗口 URL 创建一个 window。提供“top”和“left”值以将其放置在用户屏幕的右上角。 Use the type "popup" to get rid of the stuff that is usually at the top of a window. Read more about the chrome.windows.create method here .使用类型“popup”去除通常位于 window 顶部的内容。在此处阅读有关 chrome.windows.create 方法的更多信息。
 // Background Script chrome.runtime.onMessage.addListener(request => { if (request == "OpenPopup") { chrome.windows.create({ url: "popup.html", type: "popup", focused: true, width: 400, height: 600, top: 0, left: screen.width - 400, }, () => { console.log("Opened popup!") }) } })

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

相关问题 如何单击 Chrome 扩展程序中的按钮 - How to click on a button in Chrome Extension Chrome 扩展程序不会在按钮单击时运行.JS - Chrome Extension wont run .JS on button click 如何通过将侦听器添加到“窗口”来从Chrome扩展程序单击网页上的按钮? - How to click a button on a webpage from Chrome extension by adding a listener to `window`? 如何在ContextMenu上运行脚本单击Chrome扩展 - How to run script on ContextMenu Click Chrome Extension 单击按钮后,Chrome扩展程序将无法运行功能 - Chrome Extension won't run function on button click 收听 chrome 扩展中的按钮单击 - Listen to a button click in a chrome extension 使用 chrome 扩展程序中的 javascript 单击网站上的按钮 - Click button on a website using javascript from a chrome extension 从 web 页面按钮点击调用 Chrome 扩展中的 JavaScript 方法 - Call JavaScript method in Chrome extension from web page button click 当按钮单击 CHROME EXTENSION 时,如何从内容脚本打开选项页面? - How to open Options page from content script when button click CHROME EXTENSION? 无法从 chrome 扩展的内容脚本中注册跨度按钮上的单击事件 | 谷歌见面 | 镀铬扩展 - cannot register click event on a span button from content script of the chrome extension | Google meet | chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM