我正在尝试将网页发送到Chrome扩展程序,该扩展程序会在用户访问网站时触发(例如google)
的manifest.json
{
"manifest_version": 2,
"name": "Test Addon",
"version": "0.01",
"icons": { "16": "icon-16.ico" },
"browser_action": {
"default_icon" : "icon-16.ico",
"default_popup" : "popup.html"
},
"content_scripts": [
{
"matches": ["*://*.google.com/*" ],
"js": ["cs.js"]
}
],
"externally_connectable": {
"matches": ["*://*.mywebsite.com/*"]
}
}
cs.js
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
alert("world");
});
mywebsite.com/testpage.php
// The ID of my chrome extension (In developer mode)
var editorExtensionId = "cjgeckgdpfhnedenpkaanpehddchlkle";
// Send a message
chrome.runtime.sendMessage("Hello");
</script>
据我所知,内容脚本可以访问onMessage和sendMessage但我在访问google时没有收到提醒
答案 0 :(得分:1)
根据docs,扩展ID应该是调用sendMessage()
时的第一个参数。你只是省略了这个参数
第二个问题是来自外部网页的消息只能接收后台脚本。查看onMessageExternal()
// The ID of my chrome extension (In developer mode)
var editorExtensionId = "cjgeckgdpfhnedenpkaanpehddchlkle";
// Send a message
chrome.runtime.sendMessage(editorExtensionId, "Hello");
使用此代码将消息发送到后台扩展脚本:
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);