Chrome扩展:堆栈跟踪:TypeError:无法读取未定义的属性'selectedtext' [英] chrome extension: Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

查看:471
本文介绍了Chrome扩展:堆栈跟踪:TypeError:无法读取未定义的属性'selectedtext'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的contentscript.js:

  chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
if(request.ask ===selectedtext){
sendResponse({selectedtext:window.getSelection()。toString()});
}
});

我的background.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 =发送单词到background.js;
var id = chrome.contextMenus.create({title :title,contexts:[context],id:context1+ context});
}
});

更新

  {
name:Send Data Plugin,
version:1.1,
description:使用示例。,
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 发送单词到background.js ,我在控制台中得到错误:

 堆栈跟踪:TypeError:无法读取未定义的属性'selectedtext'

我做错了什么?我在Google上搜索并阅读了一些q& a,但似乎没有任何帮助。

解决方案

好吧,你的错误意味着你作为你的回应,你会得到不确定的答案。即没有数据从上下文页面返回。

你的content.js看起来很好,我认为最有可能的是没有内容脚本从另一端进行通信。在您的内容脚本中放置一个console.log,并确保它正在被击中。



您可能只需刷新您与之通信的页面,因为内容脚本可能不是当前的那个?

my contentscript.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
   if (request.ask === "selectedtext"){
    sendResponse({selectedtext: window.getSelection().toString()});
   }      
});

my background.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});   
  } 
});

UPDATE:

{
"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"]
 }]
}

but, once I click on contextMenu send the word to background.js, i am getting the error in console:

Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

what am i doing wrong? i googled and read some q&a's in here, but none seems to be helping..

解决方案

Well your error means that you're getting undefined as your response. i.e no data is coming back from the context page.

Your content.js looks fine, I think the most likely thing is there is no content script communicating from the other end. Put a console.log in your content script and make sure that's being hit.

You may just need to refresh the page that you're communicating with as the content script may not be the current one?

这篇关于Chrome扩展:堆栈跟踪:TypeError:无法读取未定义的属性'selectedtext'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆