[英]Trigger/invoke a Chrome extension from a web page
对于我目前正在进行的项目,我需要知道是否可以调用 Chrome 扩展程序。
即,单击我页面上的按钮(或链接)会调用“Read It”扩展,类似的东西。
使用 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;
});
是的,这是可能的。 您可以编写一个所谓的内容脚本来更改页面并将事件处理程序挂钩到链接或按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.