Is it possible to run javascript after a page has loaded using a omnibox extension?

284 Views Asked by At

I am trying to build an omnibox extension. Essentially I want this extension to pull up a url, then wait for that url to load, then execute some simple javascript.

Not sure if it's appreciated to put long blocks of code in questions, please feel free to yell at me if not.

manifest.json:

{
    "manifest_version": 2,
    "name": "AutoATR",
    "version": "0.1",
    "omnibox": { "keyword" : "atr" },
    "icons": {
      "16": "package.png"
    },
    "background": {
      "persistent": true,
      "scripts": ["background.js"]
    }
}

background.js:

function resetDefaultSuggestion() {
  chrome.omnibox.setDefaultSuggestion({
  description: 'atr: Placeholder: %s'
  });
}
resetDefaultSuggestion();

function navigate(url) {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) 
{
  chrome.tabs.update(tabs[0].id, {url: url});
  });
}

function finder(theID) {
  document.getElementById('theElement').value=theID
  document.getElementsByClassName('btn')[1].click()
}

chrome.omnibox.onInputEntered.addListener(function() {
    // Navigate to URL
    navigate("https://theURL.com");
});

// **** Would like to wait for just this tab to load. 
// Then Execute finder(). ****
1

There are 1 best solutions below

2
Alebid On

You can use manifest for your task: change location in background script location of tab to your pathURL and in manifest.json add next code :

  "content_scripts": [{
            "matches": ["http://pathURL"],
            "js": ["js/script.js"]
        }]

js/script.js will be loaded after your browser tab path will match with "http://pathURL" pattern.