繁体   English   中英

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

[英]Trigger/invoke a Chrome extension from a web page

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

即,单击我页面上的按钮(或链接)会调用“Read It”扩展,类似的东西。

你可以注入你的content-script到每个页面(注册设备扩展清单)和改变页面的HTML添加您的button或者a与您的自定义ID。

执行环境示例很好地解释了如何触发从页面到内容脚本的事件。 管理触发器后,您可以做任何想做的事情作为扩展逻辑。

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

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

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

在后台向内容脚本发送消息,内容脚本被注入到安装和启用扩展程序时打开的每个网页中。 在 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");
});

这里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;
          });
  1. 在您的网页代码中进行“假”ajax 调用。
  2. 在您的扩展程序中拦截它(参见https://medium.com/@gilfink/adding-web-interception-abilities-to-your-chrome-extension-fb42366df425
  3. 在拦截中,取消“假”调用并实现您想要的行为。

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

暂无
暂无

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

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