javascript 从网页触发/调用 Chrome 扩展程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8712810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:19:23  来源:igfitidea点击:

Trigger/invoke a Chrome extension from a web page

javascriptgoogle-chrome-extension

提问by ksol

For the project I'm currently working on, I need to know if it is possible to invoke a Chrome extension.

对于我目前正在进行的项目,我需要知道是否可以调用 Chrome 扩展程序。

I.e., clicking on a button (or a link) on my page would call the "Read It" extension, something like that.

即,单击我页面上的按钮(或链接)会调用“Read It”扩展程序,类似这样的内容。

采纳答案by osoner

You can inject your content-scriptto every page (register it in extension manifest) and alter the page html to add your buttonor awith your custom id.

您可以将您的注入content-script到每个页面(在扩展清单中注册)并更改页面 html 以添加您的buttona使用您的自定义 ID。

The execution environment exampleexplains it pretty good how you will trigger an event from the page to your content script. After you manage the trigger you can do anything you want as the extension logic.

执行环境例子解释了它不错的你将如何触发来自网页的事件内容的脚本。管理触发器后,您可以做任何想做的事情作为扩展逻辑。

Also keep in mind that this will require your extension's content-scriptto be injected to every page the user visits. It is not possible to actually trigger the execution of your content-scriptfrom the page if thats what you were asking.

还要记住,这将需要将您的扩展程序content-script注入用户访问的每个页面。content-script如果那是您的要求,则不可能从页面实际触发您的执行。

回答by Bob

Yes, it's possible. You could write a so-called Content Scriptto alter the page and hook event handlers to the links or buttons.

是的,这是可能的。您可以编写一个所谓的内容脚本来更改页面并将事件处理程序挂钩到链接或按钮。

回答by Altanai

create a manifest with background.js and content.js . Use

使用 background.js 和 content.js 创建一个清单。利用

chrome.tabs.sendMessage(tabId, {}, function() { ... });

chrome.tabs.sendMessage(tabId, {}, function() { ... });

in background to send messages to content script which is injected into every webpage that is opened when extension is installed and enabled . On the content.js script use

在后台向内容脚本发送消息,内容脚本被注入到安装和启用扩展程序时打开的每个网页中。在 content.js 脚本上使用

chrome.runtime.onMessage.addListener(function(req, sender, callback) {  
    // here use condition to find out when this extension's popup.html should be opened
    // and call the callback function which was passed in the argument list initially
    callback("something");
});

Here the callback function defines in background.js and passed to content.js is the code for opening a new extension window such as

这里background.js中定义的回调函数,传递给content.js的是打开一个新的扩展窗口的代码如

var panel_props = {
            type: 'panel',
            'width': width,
            'height': height,
            'left': left,
            'top': top,
            url: "chrome-extension://" + <extensionid>+ "/index.html"   
          }

          chrome.windows.create(panel_props ,function (newWindow) { 
              vid = newWindow.id;
          });

回答by Miner_Glitch

  1. Make a "fake" ajax call in your web page code.
  2. Intercept it in your extension (see https://medium.com/@gilfink/adding-web-interception-abilities-to-your-chrome-extension-fb42366df425)
  3. In the intercept, cancel the "fake" call and implement your desired behaviour.
  1. 在您的网页代码中进行“假”ajax 调用。
  2. 在您的扩展程序中拦截它(参见https://medium.com/@gilfink/adding-web-interception-abilities-to-your-chrome-extension-fb42366df425
  3. 在拦截中,取消“假”调用并实现您想要的行为。