Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 109 additions & 84 deletions chrome/background.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,122 @@
var tab_listeners = {};
var tab_push = {}, tab_lasturl = {};
var selectedId = -1;
let selectedId = -1;

function refreshCount() {
txt = tab_listeners[selectedId] ? tab_listeners[selectedId].length : 0;
chrome.tabs.get(selectedId, function() {
if (!chrome.runtime.lastError) {
chrome.browserAction.setBadgeText({"text": ''+txt, tabId: selectedId});
if(txt > 0) {
chrome.browserAction.setBadgeBackgroundColor({ color: [255, 0, 0, 255]});
} else {
chrome.browserAction.setBadgeBackgroundColor({ color: [0, 0, 255, 0] });
}
}
});
async function getStorage() {
const data = await chrome.storage.local.get(['tab_listeners', 'tab_push', 'tab_lasturl']);
return {
tab_listeners: data.tab_listeners || {},
tab_push: data.tab_push || {},
tab_lasturl: data.tab_lasturl || {}
};
}

function logListener(data) {
chrome.storage.sync.get({
log_url: ''
}, function(items) {
log_url = items.log_url;
if(!log_url.length) return;
data = JSON.stringify(data);
try {
fetch(log_url, {
method: 'post',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: data
});
} catch(e) { }
});
async function updateStorage(changes) {
await chrome.storage.local.set(changes);
}

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
console.log('message from cs', msg);
tabId = sender.tab.id;
if(msg.listener) {
if(msg.listener == 'function () { [native code] }') return;
msg.parent_url = sender.tab.url;
if(!tab_listeners[tabId]) tab_listeners[tabId] = [];
tab_listeners[tabId][tab_listeners[tabId].length] = msg;
logListener(msg);
}
if(msg.pushState) {
tab_push[tabId] = true;
}
if(msg.changePage) {
delete tab_lasturl[tabId];
}
if(msg.log) {
console.log(msg.log);
} else {
refreshCount();
}
async function refreshCount() {
if (selectedId === -1) return;
const { tab_listeners } = await getStorage();
const txt = tab_listeners[selectedId] ? tab_listeners[selectedId].length : 0;

try {
const tab = await chrome.tabs.get(selectedId);
if (tab) {
chrome.action.setBadgeText({ text: '' + (txt || ''), tabId: selectedId });
if (txt > 0) {
chrome.action.setBadgeBackgroundColor({ color: [255, 0, 0, 255], tabId: selectedId });
} else {
chrome.action.setBadgeBackgroundColor({ color: [0, 0, 255, 0], tabId: selectedId });
}
}
} catch (e) {
// Tab might be gone
}
}

async function logListener(data) {
const items = await chrome.storage.sync.get({ log_url: '' });
const log_url = items.log_url;
if (!log_url.length) return;

try {
await fetch(log_url, {
method: 'POST',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify(data)
});
} catch (e) { }
}

chrome.runtime.onMessage.addListener(async (msg, sender) => {
if (!sender.tab) return;
const tabId = sender.tab.id;
let { tab_listeners, tab_push, tab_lasturl } = await getStorage();

if (msg.listener) {
if (msg.listener === 'function () { [native code] }') return;
msg.parent_url = sender.tab.url;
if (!tab_listeners[tabId]) tab_listeners[tabId] = [];
tab_listeners[tabId].push(msg);
await updateStorage({ tab_listeners });
logListener(msg);
}

if (msg.pushState) {
tab_push[tabId] = true;
await updateStorage({ tab_push });
}

if (msg.changePage) {
delete tab_lasturl[tabId];
await updateStorage({ tab_lasturl });
}

if (msg.log) {
console.log(msg.log);
} else {
await refreshCount();
}
});

chrome.tabs.onUpdated.addListener(function(tabId, props) {
console.log(props);
if (props.status == "complete") {
if(tabId == selectedId) refreshCount();
} else if(props.status) {
if(tab_push[tabId]) {
//this was a pushState, ignore
delete tab_push[tabId];
} else {
//if(props.url && tab_lasturl[tabId] && props.url.split('#')[0] == tab_lasturl[tabId]) {
//same url as before, only a hash change, ignore
//} else
if(!tab_lasturl[tabId]) {
//wipe on other statuses, but only if lastpage is not set (aka, changePage did not run)
tab_listeners[tabId] = [];
}
}
}
if(props.status == "loading")
tab_lasturl[tabId] = true;
chrome.tabs.onUpdated.addListener(async (tabId, props) => {
if (props.status === "complete") {
if (tabId === selectedId) await refreshCount();
} else if (props.status) {
let { tab_listeners, tab_push, tab_lasturl } = await getStorage();
if (tab_push[tabId]) {
delete tab_push[tabId];
await updateStorage({ tab_push });
} else {
if (!tab_lasturl[tabId]) {
tab_listeners[tabId] = [];
await updateStorage({ tab_listeners });
}
}
}
if (props.status === "loading") {
let { tab_lasturl } = await getStorage();
tab_lasturl[tabId] = true;
await updateStorage({ tab_lasturl });
}
});

chrome.tabs.onActivated.addListener(function(activeInfo) {
selectedId = activeInfo.tabId;
refreshCount();
chrome.tabs.onActivated.addListener((activeInfo) => {
selectedId = activeInfo.tabId;
refreshCount();
});

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
selectedId = tabs[0].id;
refreshCount();
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
selectedId = tabs[0].id;
refreshCount();
}
});

chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
port.postMessage({listeners:tab_listeners});
});
})
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener(async (msg) => {
const { tab_listeners } = await getStorage();
port.postMessage({ listeners: tab_listeners });
});
});
Loading