我想创建一个不属于chrome插件的网站,而只是使用该插件公开给它的一些API。这可能吗,如果可以,我该怎么做?我用谷歌搜索了这个问题,却找不到任何东西。
我正在尝试使用内容脚本,但没有任何反应。有人可以解释这是怎么回事吗?
manifest.json
{ “ manifest_version”:2 “ name”:“ Hello World Extension”, “ description”:“此扩展程序打印您好世界。”, “ version”:“ 1.0”, “背景”: { “ page”:“ background.html” }, “ browser_action”:{ “ default_icon”:“ img / icon.png”, “ default_popup”:“ popup.html” }, “ content_scripts”:[ { “ matches”:[“ http:// locahost:8888 / *”], “ js”:[“ EmotivAPI.js”] } ] }
EmotivAPI.js
var port = chrome.runtime.connect(); console.log(“ Hello?”); window.addEventListener(“ message”,function(event){ //我们只接受来自我们自己的消息 如果(event.source!=窗口) 返回; 如果(event.data.type &&(event.data.type ==“ FROM_PAGE”)){ console.log(“已收到内容脚本:” + event.data.text); port.postMessage(event.data.text); 警报(“收到!”); } },错误);
网页中的js
window.sayHello = function(){ window.postMessage({类型:“ FROM_PAGE”,文本:“您好,来自网页!”},“ *”); } console.log('Emotiv扩展已加载。'); }
我从控制台调用window.sayHello()
在这种情况下,内容脚本可以为您提供帮助。
内容脚本将注入到页面中:
"content_scripts": [
{
"matches": ["http://www.google.com/*"], // try with "http://localhost:*/*" or "http://localhost:*"
"css": ["mystyles.css"],
"js": ["content_script.js"]
}
]
如果您只想有时注入代码,请改用“权限”字段
/* in manifest.json */
"permissions": [
"tabs", "http://*/*"
],
在扩展HTML文件中,您可以通过以下方式执行脚本:
chrome.tabs.executeScript(null, {file: "content_script.js"});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句