I used this site to create my first Chrome extension
https://www.sitepoint.com/create-chrome-extension-10-minutes-flat/
The extention is installed and running well but what I need is this:
The extension javascript script must run in the background before the DOM is loaded and the extension must pop up in certain case.
What I have now is when we click on the extension icon my code is called. But I want that extension running in the background and create a pop up in certain occasion (not only when the user click on the extension icon). For example if I try to go on google.com the script continue and do nothing but if I try to go to google2.com the pop up must appear.
To catch the current URL while browsing I use this code:
chrome.tabs.query({'active': true, 'lastFocusedWindow': true, 'currentWindow': true}, function (tabs) {
var url = tabs[0].url;
const arrayURL= url .split("/");
var BrowsedURL=arrayURL[2];
alert(BrowsedURL);
// I have now the variable BrowsedURL, now I want to do an ajax call on a certain website with that parameter. If the website reply the source code 0, the script will do nothing. If the website reply with the source code 1, the extension pop up appear.
// I know that I can interact with the popup.html page because when I try
// jQuery("#div1").text("some text");
// the "some text" appear on the popup.html page
});
I use the code below (inside the chrome.tabs.query function):
jQuery.ajax({
type: "GET",url: "https://example.com/?myvariable="+BrowsedURL,async: true,
data: {url: url, response: response
},success: function(resp) {
if(resp==0){
// do nothing
}
if(resp==1){
// div1 is a div in the popup.html page
jQuery("#div1").text("some text");
}
});
But it doesnt works! What im doing wrong? the jQuery script is correcly loaded btw