0

I have made a simple omnibox extension for Chrome, The idea is you enter a postal code, press enter and it opens a few websites. But it does not work how i want to

    function resetDefaultSuggestion() {
  chrome.omnibox.setDefaultSuggestion({
    description: 'Postcode: Zoek de postcode %s'
  });
}

resetDefaultSuggestion();

chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
  // Suggestion code will end up here.
});

chrome.omnibox.onInputCancelled.addListener(function() {
  resetDefaultSuggestion();
});


function navigate(url) {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.create({url: url});
  });
}

chrome.omnibox.onInputEntered.addListener(function(text) {
  navigate("http://www.gevonden.cc/postcode/" + text + "/streetname,housenr/");
  navigate("http://www.nummerzoeker.com/?color=white&lastname=&str=&hnr=&pc=" + text +"&pl=&phone=&maxrows=100&sort=3&search=Zoeken");
  navigate("http://www.zoekenbel.nl/postcode.asp?zoekop=p&zoek=postcode&postcode=" + text + "&Huis_nr=");
  navigate("https://server.db.kvk.nl/TST-BIN/ZS/ZSWWW01@?TAAL=NL++&TYPE=PCHN&AANT=0&AWCD=" + text + "&NHVC=&HIST=+&submit=");
});

chrome.omnibox.onInputEntered.addListener(function(text) {
  navigate("http://www.funda.nl/koop/zoeksuggestie/" + text + "/"); 
});

This all works ok, the last website (http://www.funda.nl/koop/zoeksuggestie/ + text) does not work perfectly, when the page loading is complete a button needs to be clicked, to display the results. When I run the following in the console, it works.

document.querySelectorAll("input[type='submit']")[0].click();

How do I add this to the extension so it waits for the page to load completely and then clicks the button?

Thanks in advance, LTKort

PS This is the first time making an extension and the first time coding in JS?!

LTKort
  • 113
  • 1
  • 1
  • 7

1 Answers1

0

Use jquery document ready

$(document).ready(function(){
  // your code here
});

The code in that function executes when the document is ready.

To make sure the button doesn't get clicked everytime te page reloads, you can use the jquery cookie plugin.

$(document).ready(function(){
   if($.cookie("visited") == 'true'){
       // do nothing
   } else {
       $.cookie("visited", true);
       $("input[type='submit']").trigger("click");
   }
});
Michel
  • 309
  • 2
  • 17
  • Like this? : $(document).ready(function(){ document.querySelectorAll("input[type='submit']")[0].click(); }); Because that does not work – LTKort Mar 31 '14 at 08:22
  • do you have the jquery [plugins](http://jquery.com/download/#using-jquery-with-a-cdn) loaded? And its faster to do it the jquery way (if that is possible), like: $(document).ready(function(){$("input[type='submit']").trigger("click");}); – Michel Mar 31 '14 at 08:41
  • Same way as they do [here](http://stackoverflow.com/questions/5113318/in-a-chrome-extension-content-script-must-i-wait-for-document-ready-before-proc)? Can't see why it wouldn't work – Michel Mar 31 '14 at 09:05
  • It works now, added content_script and a new js file. The only problem is that the button is clicked everytime the page loads, it only needs to do it the first time... how do i do this? – LTKort Mar 31 '14 at 09:19
  • See updated answer. If this answer helped you, please 'accept' this answer, you can do that by clicking the check mark next to the vote buttons. – Michel Mar 31 '14 at 09:32
  • Thanks, did not use it, did it another way: $(document).ready(function(){ if ( document.location.href.indexOf('zoeksuggestie') > -1 ) { document.querySelectorAll("input[type='submit']")[0].click(); } }); – LTKort Mar 31 '14 at 09:41