I am developing a chrome extension that requires popup.js to access the URL of the currently active tab in chrome. This URL i need to send as paramenter to a php call using ajax.
The issue is the url scope. I am unable to access it anywhere other than in the function chrome.tabs.query
.
I need it to be accessed outside this function so that i can pass it in the ajax call.
popup.js:
$(document).ready(function(){
var taburl;
$("#clickme").click(function(event){
event.preventDefault();
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
taburl=tabs[0].url;
document.getElementById('urlh3').innerText=taburl;
chrome.storage.local.set({'URL': taburl});
});//query
var url = "";
chrome.storage.local.get('URL', function (result) {
url = result.url;
//alert(result.url);
//$("#URL").val(url);
});
var b="XYZ";
$.ajax({
data:{ url: b},
type: "POST",
dataType: "json",
url: 'http://localhost/hello2.php',
success: function(results){
console.log("Result="+results);
}
});//ajax
}); //click
});//ready
The value XYZ gets passed when i tried as sample. But just like i passed var b;
, i need to pass taburl
, or say do var b = taburl;
outside the chrome.tab.query
function.