mirror-chatterino2/browser_ext/background.js

66 lines
1.2 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";
2018-04-12 00:40:18 +02:00
let port;
2018-04-09 22:59:19 +02:00
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);
}
}
}
2018-04-12 00:40:18 +02:00
function connectPort() {
let port = chrome.runtime.connectNative("com.chatterino.chatterino");
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("Disconnected");
});
}
2018-04-09 22:59:19 +02:00
function selectChannel(channelName) {
console.log(channelName);
port.postMessage({channelName: channelName});
}
/// add listeners
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);
});