Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I injected contentscript into a page by :

JavaScript
//this is in background.js
  chrome.tabs.executeScript(null, {"file": "contentscript.js"});

Then to check if it inserted well or not I modified an element on the page, but no changes made.
Did I inject it properly?

Here are my files :

**background.js:**

JavaScript
  //injecting contentscript here
        chrome.tabs.executeScript(null, {"file": "contentscript.js"});

  //receiving end
        var title;
        chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
        if(message.method == 'setTitle')
        title = message.title;
        else if(message.method == 'getTitle')
        sendResponse(title);
    });

**contentscript.js:**


JavaScript
<script>

   var table = document.getElementsByClassName("collapse")[0];

   var marks = new Array();

    for(var i=0;i<8;i++)
      marks[i] = table.children[0].children[i+1].children[5].innerHTML;

    var total=0;
    for(var i=0;i<8;i++)
      total += Number(marks[i]);


JavaScript
//code to send the data to background.js
    var fromDOM = total;
    chrome.runtime.sendMessage({method:'setTitle',title:fromDOM});
    
    </script>

**popup.js:**

JavaScript
   var total= 0,percentage = 0;


   chrome.runtime.sendMessage({method:'getTitle'}, function(response){
  $('.total').text(response);
  percentage = total/7.25;
  $('.percentage').text(percentage);

});




Why is this not working? Any changes or errors?


**This is how my popup.html is looking :**

http://i.stack.imgur.com/lrKBZ.png
Posted
Updated 22-Mar-14 19:17pm
v2

1 solution

SQL
I got the problem solved.

I've just moved the code to insert contenscript from background.js to popup.js. Actually, when it is in background.js , it is getting inserted only once, when I moved it to popup.js, it is getting inserted everytime the popup is clicked and is active.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900