Javascript chrome extension: problems in timing code after page reload

160 Views Asked by At

I am trying to write a simple chrome extension to practice with javascript, since I already have basic skills in C and I would like to learn JS too. The extension I'm planning to write should simply work on expedia website (expedia.it in my case), making a search and returning a specific result in an output file, let's say in a spreadsheet. This is what I've written so far:

Manifest.json:

{
"name":"Myextension",
"version":"1.0",
"manifest_version": 2,
"description":"extension sample",
"icons":{ "16":"icon16.png" },
"permissions": [
        "tabs",
        "activeTab",
        "https://ajax.googleapis.com/",
        "http://*/*",
        "https://*/*"
    ],
"content_scripts":[
    {
        "matches":["*://www.expedia.it/*"],
        "js":["script.js"]
    }
    ]
}

script.js:

function refine(){
document.getElementById("inpHotelMirror").value='refinedsearch';
}
document.getElementById('package-departing').value='date1';
document.getElementById('package-returning').value='date2';
document.getElementById('search-button').click();

As the code shows, it should write in input "date1" and "date2" (to be defined before launching the extension) and then submit the form. Until here, everything is ok, it works and the search begins. The problem is that I want to launch the refine function after the page has reloaded, which takes about 10-15 seconds. In order to do that I tried setTimeout, setInterval, window.onload and other tricks like a while loop to make the function write the input after the page has loaded, but none of these seems to work. I've searched many times questions similar to mine, but still couldn't solve the problem. Is this a problem of my code and I should change it, or is this a problem with the website, preventing similar scripts?

I apologise for being a newbie, and thanks for helping.

EDIT: I tried adding the run_at line, then calling the refine function with a setTimeout. Now the extension doesn't work, neither the first part that was actually working. I also tried the document_idle version, now the first part works again but the second still doesn't; it seems like there are problems while injecting the code in the reloaded page.

0

There are 0 best solutions below