Content script comunicating with html page

81 views
Skip to first unread message

Jeremiah Heisenberg

unread,
Aug 21, 2016, 10:16:04 AM8/21/16
to Chromium-Extensions-Announce

I need my content script to run with privileges.

For my script to perform a privileged action, it needs to send a "message" to my background page,(background.html), so the background pages peforms it for it.

Here is my current content script:



function checkReq(){
var list = document.getElementById("gkpluginsExtListReq");
if(list===null){
    return;
}
list.title = "ready";
if(list.childNodes.length>0){
    var curReq = list.firstChild;
    if(typeof curReq.innerHTML=="undefined"){
        list.removeChild(curReq);
        return;
    }
    var obj = JSON.parse(atob(curReq.innerHTML));
    obj.onload = obj.onerror = obj.onabor = function(response){
        var txtout = document.createElement("textarea");
        txtout.id = obj.extreqid;
        txtout.style.display = "none";
        var Hfres = response.status+" "+response.statusText+"\r\n"+response.responseHeaders;
        if(response.finalUrl){
            Hfres += "FinalLocation: "+response.finalUrl+"\r\n";
        }
        if(obj.returndtaescape){
            txtout.value = escape(Hfres+"\r\n"+response.responseText);
        }else if(obj.returndtab64){
            txtout.value = btoa(Hfres+"\r\n"+response.responseText);
        }else{
            txtout.value = Hfres+"\r\n"+response.responseText;
        }
        document.body.appendChild(txtout);
    };
    GM_xmlhttpRequest(obj);
    list.removeChild(curReq);
}
}

setInterval(checkReq,100);


My background.html page is empty, because i until now, i had no luck to let it "comunicate" with my content script. I appreciate any help.

P.S: The Message Passing page from google chrome developer section didn't really help me out.

Jeremiah Heisenberg

unread,
Aug 21, 2016, 10:25:39 AM8/21/16
to Chromium-Extensions-Announce
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

PhistucK

unread,
Aug 21, 2016, 11:41:27 AM8/21/16
to Jeremiah Heisenberg, Chromium-Extensions-Announce
Have you debugged your content script? Does it actually send a network request?
Has the load event fired for the request?

Also, onabor should probably be onabort.

Wait, where is the code that sends a message to the background page?


PhistucK

On Sun, Aug 21, 2016 at 5:25 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/26c4e729-3e8d-4ea9-8de8-8bc3cec0a79a%40chromium.org.

Jeremiah Heisenberg

unread,
Aug 21, 2016, 3:04:54 PM8/21/16
to Chromium-Extensions-Announce, h41...@gmail.com
Hey, first sorry, this is my first chrome extension.
It does it work for other xhr work, but when it comes to set the origin header and such it gives me this kind of error:

I've read about one hundred or more posts on stackoverflow, and then i found out that i had to make the html page to do that kind of work instead of the script?

The javascript is also just another "worker" for another script that is @my website.

When i install my script via tamermonkey or greasemonkey, it works completly fine.


Am Sonntag, 21. August 2016 17:41:27 UTC+2 schrieb PhistucK:
Have you debugged your content script? Does it actually send a network request?
Has the load event fired for the request?

Also, onabor should probably be onabort.

Wait, where is the code that sends a message to the background page?


PhistucK

On Sun, Aug 21, 2016 at 5:25 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.

PhistucK

unread,
Aug 21, 2016, 4:44:11 PM8/21/16
to Jeremiah Heisenberg, Chromium-Extensions-Announce
While they are errors, they are not exceptions (so you code keeps running).

You have not answered my final (and most important, I guess) question - where in the code are you sending a message to your background page?

You have not answered most of my questions, really. I am not entirely sure I even understand what it is that you are asking.


PhistucK

On Sun, Aug 21, 2016 at 10:04 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Hey, first sorry, this is my first chrome extension.
It does it work for other xhr work, but when it comes to set the origin header and such it gives me this kind of error:

I've read about one hundred or more posts on stackoverflow, and then i found out that i had to make the html page to do that kind of work instead of the script?

The javascript is also just another "worker" for another script that is @my website.

When i install my script via tamermonkey or greasemonkey, it works completly fine.


Am Sonntag, 21. August 2016 17:41:27 UTC+2 schrieb PhistucK:
Have you debugged your content script? Does it actually send a network request?
Has the load event fired for the request?

Also, onabor should probably be onabort.

Wait, where is the code that sends a message to the background page?


PhistucK

On Sun, Aug 21, 2016 at 5:25 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Jeremiah Heisenberg

unread,
Aug 21, 2016, 5:46:00 PM8/21/16
to Chromium-Extensions-Announce, h41...@gmail.com
That's the problem, i don't know how to "convert" the script, to make it "communicate" with the background.html page.

Am Sonntag, 21. August 2016 22:44:11 UTC+2 schrieb PhistucK:
While they are errors, they are not exceptions (so you code keeps running).

You have not answered my final (and most important, I guess) question - where in the code are you sending a message to your background page?

You have not answered most of my questions, really. I am not entirely sure I even understand what it is that you are asking.


PhistucK

On Sun, Aug 21, 2016 at 10:04 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Hey, first sorry, this is my first chrome extension.
It does it work for other xhr work, but when it comes to set the origin header and such it gives me this kind of error:

I've read about one hundred or more posts on stackoverflow, and then i found out that i had to make the html page to do that kind of work instead of the script?

The javascript is also just another "worker" for another script that is @my website.

When i install my script via tamermonkey or greasemonkey, it works completly fine.


Am Sonntag, 21. August 2016 17:41:27 UTC+2 schrieb PhistucK:
Have you debugged your content script? Does it actually send a network request?
Has the load event fired for the request?

Also, onabor should probably be onabort.

Wait, where is the code that sends a message to the background page?


PhistucK

On Sun, Aug 21, 2016 at 5:25 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

PhistucK

unread,
Aug 22, 2016, 1:46:44 AM8/22/16
to Jeremiah Heisenberg, Chromium-Extensions-Announce
Let us back up a little.
What is the privileged functionality you need that only the background page can do?


PhistucK

On Mon, Aug 22, 2016 at 12:46 AM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
That's the problem, i don't know how to "convert" the script, to make it "communicate" with the background.html page.

Am Sonntag, 21. August 2016 22:44:11 UTC+2 schrieb PhistucK:
While they are errors, they are not exceptions (so you code keeps running).

You have not answered my final (and most important, I guess) question - where in the code are you sending a message to your background page?

You have not answered most of my questions, really. I am not entirely sure I even understand what it is that you are asking.


PhistucK

On Sun, Aug 21, 2016 at 10:04 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Hey, first sorry, this is my first chrome extension.
It does it work for other xhr work, but when it comes to set the origin header and such it gives me this kind of error:

I've read about one hundred or more posts on stackoverflow, and then i found out that i had to make the html page to do that kind of work instead of the script?

The javascript is also just another "worker" for another script that is @my website.

When i install my script via tamermonkey or greasemonkey, it works completly fine.


Am Sonntag, 21. August 2016 17:41:27 UTC+2 schrieb PhistucK:
Have you debugged your content script? Does it actually send a network request?
Has the load event fired for the request?

Also, onabor should probably be onabort.

Wait, where is the code that sends a message to the background page?


PhistucK

On Sun, Aug 21, 2016 at 5:25 PM, Jeremiah Heisenberg <h41...@gmail.com> wrote:
Forgot to mention, the script needs GM_xmlhttpRequest granted.
// @grant        GM_xmlhttpRequest

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Jeremiah Heisenberg

unread,
Aug 23, 2016, 10:29:06 AM8/23/16
to Chromium-Extensions-Announce, h41...@gmail.com
I have no clue.


PhistucK



PhistucK



PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

PhistucK

unread,
Aug 23, 2016, 1:19:59 PM8/23/16
to Jeremiah Heisenberg, Chromium-Extensions-Announce
Hm... right. So, I am glad this is solved.


PhistucK



PhistucK



PhistucK



PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Eric White

unread,
Aug 23, 2016, 9:18:23 PM8/23/16
to Chromium-Extensions-Announce
Seems you need to step back to learn how message passing works.

Content script will make a call 
chrome.runtime.sendMessage(.......
Mean while your backgroud.js is waiting
Background script with  listener.
chrome.runtime.onMessage.addListener(function....
do stuff in protected background....
sendResponse( {results of stuff for your content script.} )

Search for some of the messaging samples.  that will help you get the idea.

cheers.

Eric

Jeremiah Heisenberg

unread,
Aug 24, 2016, 3:40:44 PM8/24/16
to Chromium-Extensions-Announce
thanks, will take time to recode my whole page^^
Reply all
Reply to author
Forward
0 new messages