Chrome.tabs.sendMessage Returns Undefined

Asked 1 years ago, Updated 1 years ago, 133 views

I'm trying to create an add-on with chrome that does the following:

The code for each file is as follows:

  • manifest.json

    {
       "name": "test",
       "version": "0.0.2",
       "manifest_version"—2,
       "description": "test",
       "icons": {
               "16": "img/icon_16.png"
             },
       "browser_action": {
         "default_icon": "img/icon_16.png",
         "default_title": "test",
         "default_popup": "popup.html"  
       },
       "content_scripts":[
         {
           "matches": ["https://*/*",
           "js": ["js/jquery-2.1.1.min.js", "js/content.js",
           "run_at": " document_end"
         }
       ],
       "permissions": [
         "tabs"
       ]
     } 
    
  • popup.js

    $( document).ready(function(){
       chrome.tabs.query({active:true}, function(tab){
         $('#do_action').click(function(e){
           e.preventDefault();
    
           var list="";
    
           list=$("[name='list']").val();
           if(list==""){
             alert("Please insert characters into the text area.");
             return false;
           }
    
           var list_array=list.split("\n");
    
           chrome.tabs.sendMessage(tab[0].id, {list:list_array}, function(response){
             alert(response.result);
           });
         });
       });
     });
    
  • content.js

    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
       var hit_datas=[];
       var send_mes="";
       $.each(request.list, function(){
         varsearch_value = this;
         $('tr').each(function(){
           if($('td', this).eq(2).text()==search_value){
               hit_datas.push(search_value+" found.");
             }
           }
         });
       });
    
       if(hit_datas.length!=0){
         send_mes = hit_datas.join("\n");
       } else {
         send_mes="No matching information was found.";
       }
       sendResponse({result:send_mes});
     });
    

manifest.json

{
   "name": "test",
   "version": "0.0.2",
   "manifest_version"—2,
   "description": "test",
   "icons": {
           "16": "img/icon_16.png"
         },
   "browser_action": {
     "default_icon": "img/icon_16.png",
     "default_title": "test",
     "default_popup": "popup.html"  
   },
   "content_scripts":[
     {
       "matches": ["https://*/*",
       "js": ["js/jquery-2.1.1.min.js", "js/content.js",
       "run_at": " document_end"
     }
   ],
   "permissions": [
     "tabs"
   ]
 } 

popup.js

$( document).ready(function(){
   chrome.tabs.query({active:true}, function(tab){
     $('#do_action').click(function(e){
       e.preventDefault();

       var list="";

       list=$("[name='list']").val();
       if(list==""){
         alert("Please insert characters into the text area.");
         return false;
       }

       var list_array=list.split("\n");

       chrome.tabs.sendMessage(tab[0].id, {list:list_array}, function(response){
         alert(response.result);
       });
     });
   });
 });

content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
   var hit_datas=[];
   var send_mes="";
   $.each(request.list, function(){
     varsearch_value = this;
     $('tr').each(function(){
       if($('td', this).eq(2).text()==search_value){
           hit_datas.push(search_value+" found.");
         }
       }
     });
   });

   if(hit_datas.length!=0){
     send_mes = hit_datas.join("\n");
   } else {
     send_mes="No matching information was found.";
   }
   sendResponse({result:send_mes});
 });

The popup.html contains a button that swings the id do_action, a text area named list, and a description that reads popup.js.


in the popup.js alert(response.result); section of
extensions::uncaught_exception_handler:8 Error in event handler for (unknown):TypeError:Cannot read property 'result' of undefined
The error will occur.

I looked into various things, but I couldn't solve them by myself.
Thank you for your cooperation.

google-chrome chrome-extension

2022-09-29 22:27

1 Answers


in the popup.js alert(response.result); section of
extensions::uncaught_exception_handler:8 Error in event handler for (unknown):TypeError:Cannot read property 'result' of undefined
The error will occur.

The callback function is called without arguments (that is, the response argument value is undefined), indicating that something is wrong.The cause should be in chrome.runtime.lastError, so check the contents of that lastError.You will know the cause.


2022-09-29 22:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.