javascript - Chrome Extension : Issue with SendMessage - Stack Overflow

I am trying to change the contents of a page based on the output of a xhr call. I am sending a message

I am trying to change the contents of a page based on the output of a xhr call. I am sending a message from content.js making the xrh call in the background js file and then passing the output to content.js which alters the content of the page.

From my content.js file I am doing the following.

var s = document.createElement('script'); 
s.src = chrome.extension.getURL('src/content/main.js'); 
(document.head || document.documentElement).appendChild(s);

In my main.js I am doing

  chrome.runtime.sendMessage({
    method: 'GET',
    action: 'xhttp',
    url: myurl
  }, function(responseText) {
      console.log("Response Text is ", responseText);
  });

And in my bg.js I have the following

chrome.runtime.onMessage.addListener(function(request, sender, callback) {
    if (request.action == "xhttp") {
        var xhttp = new XMLHttpRequest();
        var method = request.method ? request.method.toUpperCase() : 'GET';
        xhttp.onload = function() {
            callback(xhttp.responseText);
        };
        xhttp.onerror = function() {
            // Do whatever you want on error. Don't forget to invoke the
            // callback to clean up the munication port.
            callback('Error');
        };
        xhttp.open(method, request.url, true);
        if (method == 'POST') {
            xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        xhttp.send(request.data);
        return true; // prevents the callback from being called too early on return
    }
});

The issue I am facing is I keep getting the error Invalid arguments to connect. for chrome.runtime.sendMessage function.

I am not sure what I am missing. Any help us is greatly appreciated.

I am trying to change the contents of a page based on the output of a xhr call. I am sending a message from content.js making the xrh call in the background js file and then passing the output to content.js which alters the content of the page.

From my content.js file I am doing the following.

var s = document.createElement('script'); 
s.src = chrome.extension.getURL('src/content/main.js'); 
(document.head || document.documentElement).appendChild(s);

In my main.js I am doing

  chrome.runtime.sendMessage({
    method: 'GET',
    action: 'xhttp',
    url: myurl
  }, function(responseText) {
      console.log("Response Text is ", responseText);
  });

And in my bg.js I have the following

chrome.runtime.onMessage.addListener(function(request, sender, callback) {
    if (request.action == "xhttp") {
        var xhttp = new XMLHttpRequest();
        var method = request.method ? request.method.toUpperCase() : 'GET';
        xhttp.onload = function() {
            callback(xhttp.responseText);
        };
        xhttp.onerror = function() {
            // Do whatever you want on error. Don't forget to invoke the
            // callback to clean up the munication port.
            callback('Error');
        };
        xhttp.open(method, request.url, true);
        if (method == 'POST') {
            xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        xhttp.send(request.data);
        return true; // prevents the callback from being called too early on return
    }
});

The issue I am facing is I keep getting the error Invalid arguments to connect. for chrome.runtime.sendMessage function.

I am not sure what I am missing. Any help us is greatly appreciated.

Share Improve this question edited Apr 13, 2015 at 9:22 Gokul N K asked Apr 13, 2015 at 8:01 Gokul N KGokul N K 2,4582 gold badges33 silver badges41 bronze badges 5
  • How do you inject the content.js code? I don't see any immediate errors with your code, and I've never seen such an error - sounds like using connect instead of sendMessage. – Xan Commented Apr 13, 2015 at 8:09
  • @Xan I was using the following in content.js and had the remaining logic in main.js as I wanted jQuery to be loaded before executing the script. Looks like that was causing the issue. Now I moved it to content.js it works fine. var s = document.createElement('script'); s.src = chrome.extension.getURL('src/content/main.js'); (document.head || document.documentElement).appendChild(s); But however I can't use jQuery now. Any idea for fixing that? – Gokul N K Commented Apr 13, 2015 at 9:15
  • Aha, so my theory is correct. – Xan Commented Apr 13, 2015 at 9:16
  • @Xan Thanks for pointing that out. Any idea, why that won't work? Post it as an answer so that I can accept it. Also do you have any idea how to fix the jQuery not loaded issue? – Gokul N K Commented Apr 13, 2015 at 9:17
  • I will. Can you edit this information into the question itself? – Xan Commented Apr 13, 2015 at 9:18
Add a ment  | 

1 Answer 1

Reset to default 8

You have been trying to inject your content script into the page with a <script> tag.

When you do it, your script ceases to be a content script: it executes in the context of the page, and loses all elevated access to Chrome API, including sendMessage.

You should read up on isolated world concept and this question about page-level scripts.

To use jQuery, you should not rely on the copy provided by the page - it's in another context and therefore unusable. You need to include a local copy of jQuery with your files and load it before your script:

  1. If you're using the manifest to inject scripts, you can add jQuery to the list before your script:

    "content_scripts": [
      {
        matches: ["http://*.example./*"],
        js: ["jquery.js", "content.js"]
      }
    ],
    
  2. If you are using programmatic injection, chain-load your scripts to ensure the load order:

    chrome.tabs.executeScript(tabId, {file: "jquery.js"}, function() {
      chrome.tabs.executeScript(tabId, {file: "content.js"});
    });
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275380a4566303.html

相关推荐

  • javascript - Chrome Extension : Issue with SendMessage - Stack Overflow

    I am trying to change the contents of a page based on the output of a xhr call. I am sending a message

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信