Re: GetElemntById does'nt work in my background.js

99 views
Skip to first unread message

Arul Gupta

unread,
Aug 12, 2012, 1:28:27 PM8/12/12
to chromium-...@chromium.org

Hey

Firstly this code is a part of the background.js file.
So the code that you write here is a part of the extension and doesn't execute on the loaded page.
You need to use content scripts or programmatic injection of script to make your code run on the loaded page.


Another thing is that after you create a new tab with your desired url, you have to wait before the page loads.
Otherwise your code to select the elements by id will be executed even before they are loaded and hence it would be unable to find the search input field.




 

Armand Grillet

unread,
Aug 13, 2012, 11:05:09 AM8/13/12
to chromium-...@chromium.org
Ok I have done what you say and it works, thank you for your help. Except Chrome does'nt support local storage on a content script, hmm hmm... Seriously ?!?
I launch the content script with " setTimeout(function() {chrome.tabs.executeScript(null, {file: "insertion.js"});},500); " in background.js, but can I send variables in this line or not ?

PhistucK

unread,
Aug 13, 2012, 12:49:39 PM8/13/12
to Armand Grillet, Mike West, chromium-...@chromium.org
You can use chrome.tabs.sendMessage (and chrome.extension.onMessage within the content script) within the callback function of executeScript (third parameter) to pass data to the content script.
Something like that (untested) -
setTimeout(function() {chrome.tabs.executeScript(null, {file: "insertion.js"}, function (tab) { chrome.tabs.sendMessage(tab.id, {"variable": "value"});})},500);

The documentation seems to be missing information regarding the arguments with which all of the callback functions are called.
PhistucK



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/-/MDepIdGNYs8J.

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

Armand Grillet

unread,
Aug 13, 2012, 4:53:45 PM8/13/12
to chromium-...@chromium.org
Thanks for your help, I tried to write a chrome.extension.onMessage in my content script but I haven't enough information on the google dev website for make that work, I'm searching on the web. But seriously this is so stupid, if localstorage exist it's for something --". 

Le vendredi 10 août 2012 20:35:57 UTC+2, Armand Grillet a écrit :
Hello,
I'm working on a Chromium extension, all is fine except one line. I have checked all the line and my script is running, it's not the problem. I want to enter a value in the search input of http://www.lesnumeriques.com/. I have this code on background.js :
function enterImmediat(){
chrome.tabs.create({"url":"http://www.lesnumeriques.com/","selected":true});
document.getElementById("selection").value = "good";
document.getElementById("form_rech").submit();
        }        
}
chrome.browserAction.onClicked.addListener(enterImmediat);

The value of selection does'nt change and I don't know why, if someone can help me it will be awesome :).
Thank you.

Reply all
Reply to author
Forward
0 new messages