【发布时间】:2014-06-09 12:42:27
【问题描述】:
我的 contentscript.js:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.ask === "selectedtext"){
sendResponse({selectedtext: window.getSelection().toString()});
}
});
我的背景.js:
function onClickHandler(info, tab) {
chrome.tabs.sendMessage(tab.id, {ask: "selectedtext"}, function(response) {
console.log(response.selectedtext);
});
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function() {
var contexts = ["selection"];
for (var i = 0; i < contexts.length; i++){
var context = contexts[i];
var title = "send the word to background.js";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],"id": "context1" + context});
}
});
更新:
{
"name" : "Send Data Plugin",
"version" : "1.1",
"description" : "A trivial usage example.",
"permissions": [
"browsingData", "contextMenus", "http://chromeplugin.sites.djangoeurope.com/"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"manifest_version": 2,
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["contentscript.js"]
}]
}
但是,一旦我点击 contextMenu send the word to background.js,我就会在控制台中收到错误:
Stack trace: TypeError: Cannot read property 'selectedtext' of undefined
我做错了什么?我在这里搜索并阅读了一些问答,但似乎没有任何帮助..
【问题讨论】:
标签: javascript google-chrome google-chrome-extension