Thank you so much for this article! This is exactly what I needed to do, and you saved me a ton of figuring out the Chrome documentation.
I took the freedom to clean up your code a little bit in the process of understanding it. It's still basically the same thing, in case you want to include it somewhere in the article:
function sendRequest(update, tab){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};url = tab.url;
xhttp.open(“POST”, “http://192.168.1.162:5000/send_url");
xhttp.send(“url=” + tab.url);
}chrome.tabs.onActivated.addListener((tabId) => {
sendRequest(false, tabId);
});chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
if (tab.active && change.url) {
sendRequest(true, change)}
});// define a mapping between tabId and url:
var tabToUrl = {};chrome.tabs.onUpdated.addListener(function (tabId, tab) {
//store tabId and tab url as key value pair:
tabToUrl[tabId] = tab.url;
});chrome.tabs.onRemoved.addListener(function (tabId) {
console.log(tabToUrl[tabId]);
sendRequest(false, tabToUrl[tabId]);delete tabToUrl[tabId];
});