从web页面触发/调用Chrome扩展

时间:2022-08-26 21:32:09

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”扩展,类似这样。

3 个解决方案

#1


3  

You can inject your content-script to every page (register it in extension manifest) and alter the page html to add your button or a with your custom id.

您可以将内容脚本注入到每个页面(在扩展名清单中注册它),并更改页面html以添加按钮或带有自定义id的a。

The execution environment example explains 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-script to be injected to every page the user visits. It is not possible to actually trigger the execution of your content-script from the page if thats what you were asking.

还要记住,这需要将扩展的内容脚本注入到用户访问的每个页面。如果你问的是内容脚本的话,实际上不可能从页面触发执行。

#2


1  

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

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

#3


0  

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

用背景创建一个清单。js和内容。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

在后台向内容脚本发送消息,该脚本在安装和启用扩展时注入到每个打开的页面。在内容上。js脚本使用

chrome.runtime.onMessage.addListener(function(req, sender, callback) {  
    < here use condition to find out when this exetnsion's popup.html should be opened and call the callback function which was passed in the argument list intially >
    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

这个回调函数在后台定义。并传递给内容。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;
          });

#1


3  

You can inject your content-script to every page (register it in extension manifest) and alter the page html to add your button or a with your custom id.

您可以将内容脚本注入到每个页面(在扩展名清单中注册它),并更改页面html以添加按钮或带有自定义id的a。

The execution environment example explains 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-script to be injected to every page the user visits. It is not possible to actually trigger the execution of your content-script from the page if thats what you were asking.

还要记住,这需要将扩展的内容脚本注入到用户访问的每个页面。如果你问的是内容脚本的话,实际上不可能从页面触发执行。

#2


1  

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

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

#3


0  

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

用背景创建一个清单。js和内容。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

在后台向内容脚本发送消息,该脚本在安装和启用扩展时注入到每个打开的页面。在内容上。js脚本使用

chrome.runtime.onMessage.addListener(function(req, sender, callback) {  
    < here use condition to find out when this exetnsion's popup.html should be opened and call the callback function which was passed in the argument list intially >
    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

这个回调函数在后台定义。并传递给内容。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;
          });