I need some guide on how to achieve this kind of thing.
The thing I want to do is to use page_action to display popup for specific URLs. What I want to achieve is something like this:
When a user loads url in the browser, an AJAX request is sent to my service to check for the url. If url is found on my service, say I will return some text against it. That text will then be displayed in the popup.
For that I am using chrome.tabs.onUpdated.addListener
function. The problem with it is that whenever a user opens a new tab, this function is called, it then updates the popup page, removing the message for the previously opened tab.
Any solution?
Update: I am pasting my code, could someone please check what could be the problem?
manifest.json
{
"manifest_version" : 2,
"name" : "Know your cashback for a site!",
"version" : "1.0",
"description" : "Find out about the cashback of the visiting website right in your browser",
"background" : { "scripts" : ["jquery.js","records.js"]},
"permissions" : [ "http://*/*", "https://*/*", "tabs" ],
"page_action" : {
"default_icon" : "images/icon.png"
}
}
records.js
var result;
function checkForValidUrl(tabId, changeInfo, tab) {
if (tab.url !== undefined && changeInfo.status == "complete") {
$.ajax({
url: 'http://localhost/chrome_extension/index.php',
data: "url=" + encodeURIComponent(tab.url),
type:'GET',
success: function(resp) {
if(resp=="not_found"||resp=="invalid_request") {
// do nothing
} else {
resp = JSON.parse(resp);
chrome.pageAction.show(tabId);
chrome.pageAction.setTitle({
tabId: tabId,
title: resp.cashback
});
chrome.pageAction.setPopup({
tabId: tabId,
popup: "popup.htm"
});
window.result = resp;
//alert('update successful');
}
}
});
}
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
popup.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<span id="request"></span>
<a href="#" id="ref_link"></a>
</body>
</html>
popup.js
var BGPage = chrome.extension.getBackgroundPage();
if(typeof BGPage.result !== undefined) {
document.getElementById('request').innerHTML = BGPage.result.cashback;
document.getElementById('ref_link').href = BGPage.result.store;
}
$('a#ref_link').on('click', function(e) {
var href = e.currentTarget.href;
chrome.tabs.query({active:true}, function (tab){
chrome.tabs.update(tab.id, {url: href});
});
});