所以我一直在尝试从网页发送数据到Chrome应用程序(而不是Chrome扩展)。根据我的看法,这主要需要使用url_handlers
来从网页调用Chrome应用,content scripts
将JS注入网页和/或Messaging
以用于Chrome应用和网页之间的通信。使用内容脚本/消息与Chrome应用程序(而不是扩展)发送数据
此外,安装应用程序从它的.crx
,否则加载解压目录导致这个错误:
"
content_scripts
is only allowed for extensions and legacy packaged apps, but this is a packaged app."
现在,我试图在需要部位注射JS。但是,在使用Chrome开发工具进行检查时,该网站的Sources->Content scripts
部分中没有这样的条目。它只是在使用Chrome应用程序时没有注入。它适用于扩展,但我想使用Chrome应用程序的其他功能。
作为替代,我看了Messaging
例子,其中它的用法被提及:
"... your app or extension can receive and respond to messages from regular web pages."
但是,不知何故,我无法得到它的工作。
任何一种方法的任何标题?其他建议?
manifest.json的:
{
"name": "App",
"version": "1.0",
"manifest_version": 2,
"minimum_chrome_version": "31",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
{"fileSystem": ["write", "retainEntries", "directory"]},
"storage",
"http://example.com/*"
],
"externally_connectable": {
"matches": ["http://example.com/*"]
},
"content_scripts": [{
"matches": ["http://example.com/*"],
"js": ["content.js"]
}]
}
谢谢你的抬头。所以内容脚本完全排除。这对我来说至关重要。此外,早些时候我已经浏览了你提到的一个代码片段(这里是https://developer.chrome.com/extensions/messaging#external-webpage)。事实上,它的工作! 想象我正在尝试在没有打印的background.js中使用'console.log()'。这次我尝试在example.com脚本的回调中打印并完成! #Acptedpted :) 在类似的行上,我如何在background.js中调试东西? –
您需要打开'background.js'的开发工具。您可以从'chrome:// extensions'页面中执行此操作,查找您应用的条目。注意后台页面会在空闲时卸载(并丢失所有控制台输出),除非开发工具已打开。 'chrome:// inspect /#apps'页面也很有用。 – Xan
啊,明白了!点击'chrome:// extensions'页面中的“Inspect views”中的'background page'后,会打开'background.js'的控制台。即使右键点击Chrome应用程序本身也可以选择“检查后台页面”。感谢您通知'chrome:// inspect /#apps'页面。 –