mirror-chatterino2/browser_ext/background.js

89 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-04-09 22:59:19 +02:00
const ignoredPages = {
"settings": true,
"payments": true,
"inventory": true,
"messages": true,
"subscriptions": true,
"friends": true,
"directory": true,
};
const appName = "com.chatterino.chatterino";
/// Connect to port
2018-04-12 00:40:18 +02:00
let port = null;
2018-04-09 22:59:19 +02:00
2018-04-12 00:40:18 +02:00
function connectPort() {
port = chrome.runtime.connectNative("com.chatterino.chatterino");
console.log("port connected");
2018-04-09 22:59:19 +02:00
2018-04-12 00:40:18 +02:00
port.onMessage.addListener(function(msg) {
console.log(msg);
});
port.onDisconnect.addListener(function() {
console.log("port disconnected");
port = null;
2018-04-12 00:40:18 +02:00
});
}
2018-04-09 22:59:19 +02:00
function getPort() {
if (port) {
return port;
} else {
// TODO: add cooldown
connectPort();
2018-04-09 22:59:19 +02:00
return port;
}
2018-04-09 22:59:19 +02:00
}
/// Tab listeners
2018-04-09 22:59:19 +02:00
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
if (!tab)
return;
if (!tab.url)
return;
matchUrl(tab.url);
});
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!tab.highlighted)
return;
matchUrl(changeInfo.url);
});
/// Misc
function matchUrl(url) {
if (!url)
return;
const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9]+)\/?$/);
if (match) {
const channelName = match[2];
if (!ignoredPages[channelName]) {
selectChannel(channelName);
}
}
}
function selectChannel(channelName) {
console.log("select" + channelName);
let port = getPort();
if (port) {
2018-04-20 19:54:45 +02:00
port.postMessage({action: "select", type: "twitch", name: channelName});
}
}