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 communication 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.