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 01:17:25 +02:00
|
|
|
let port = null;
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
// gets the port for communication with chatterino
|
|
|
|
function getPort() {
|
|
|
|
if (port) {
|
|
|
|
return port;
|
|
|
|
} else {
|
|
|
|
// TODO: add cooldown
|
|
|
|
connectPort();
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// connect to port
|
2018-04-12 00:40:18 +02:00
|
|
|
function connectPort() {
|
2018-05-28 18:25:19 +02:00
|
|
|
port = chrome.runtime.connectNative(appName);
|
2018-04-12 01:17:25 +02:00
|
|
|
console.log("port connected");
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-04-25 14:49:30 +02:00
|
|
|
port.onMessage.addListener(function (msg) {
|
2018-04-12 00:40:18 +02:00
|
|
|
console.log(msg);
|
|
|
|
});
|
2018-04-25 14:49:30 +02:00
|
|
|
port.onDisconnect.addListener(function () {
|
2018-04-12 01:17:25 +02:00
|
|
|
console.log("port disconnected");
|
|
|
|
|
|
|
|
port = null;
|
2018-04-12 00:40:18 +02:00
|
|
|
});
|
|
|
|
}
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
// tab activated
|
2018-04-09 22:59:19 +02:00
|
|
|
chrome.tabs.onActivated.addListener((activeInfo) => {
|
2018-05-28 18:25:19 +02:00
|
|
|
console.log(0)
|
2018-04-09 22:59:19 +02:00
|
|
|
chrome.tabs.get(activeInfo.tabId, (tab) => {
|
2018-05-28 18:25:19 +02:00
|
|
|
console.log(1)
|
|
|
|
if (!tab || !tab.url) return;
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
console.log(2)
|
|
|
|
chrome.windows.get(tab.windowId, {}, (window) => {
|
|
|
|
if (!window.focused) return;
|
|
|
|
console.log(3)
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
onTabSelected(tab.url, tab);
|
|
|
|
});
|
2018-04-09 22:59:19 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
// url changed
|
2018-04-09 22:59:19 +02:00
|
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
|
|
|
if (!tab.highlighted)
|
|
|
|
return;
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
onTabSelected(changeInfo.url, tab);
|
|
|
|
});
|
|
|
|
|
|
|
|
// tab detached
|
|
|
|
chrome.tabs.onDetached.addListener((tabId, detachInfo) => {
|
|
|
|
tryDetach(detachInfo.oldWindowId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// tab closed
|
|
|
|
chrome.windows.onRemoved.addListener((windowId) => {
|
|
|
|
tryDetach(windowId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// window selected
|
|
|
|
chrome.windows.onFocusChanged.addListener((windowId) => {
|
|
|
|
chrome.tabs.query({windowId: windowId, highlighted: true}, (tabs) => {
|
|
|
|
if (tabs.length >= 1) {
|
|
|
|
let tab = tabs[0];
|
|
|
|
|
|
|
|
onTabSelected(tab.url, tab);
|
|
|
|
}
|
|
|
|
});
|
2018-04-09 22:59:19 +02:00
|
|
|
});
|
2018-04-12 01:17:25 +02:00
|
|
|
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
/// return channel name if it should contain a chat
|
|
|
|
function matchChannelName(url) {
|
2018-04-12 01:17:25 +02:00
|
|
|
if (!url)
|
2018-05-28 18:25:19 +02:00
|
|
|
return undefined;
|
2018-04-12 01:17:25 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9_]+)\/?$/);
|
2018-04-12 01:17:25 +02:00
|
|
|
|
2018-04-25 14:49:30 +02:00
|
|
|
let channelName;
|
2018-05-28 18:25:19 +02:00
|
|
|
if (match && (channelName = match[2], !ignoredPages[channelName])) {
|
|
|
|
return channelName;
|
|
|
|
}
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
// attach or detach from tab
|
|
|
|
function onTabSelected(url, tab) {
|
|
|
|
let channelName = matchChannelName(url);
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
if (channelName) {
|
2018-04-25 14:49:30 +02:00
|
|
|
chrome.windows.get(tab.windowId, {}, (window) => {
|
2018-05-28 18:25:19 +02:00
|
|
|
// attach to window
|
|
|
|
tryAttach(tab.windowId, {
|
|
|
|
name: channelName,
|
|
|
|
yOffset: window.height - tab.height,
|
|
|
|
});
|
2018-04-25 14:49:30 +02:00
|
|
|
});
|
|
|
|
} else {
|
2018-05-28 18:25:19 +02:00
|
|
|
// detach from window
|
|
|
|
tryDetach(tab.windowId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receiving messages from the inject script
|
|
|
|
function registerTheGarbage() {
|
|
|
|
chrome.runtime.onMessage.addListener((message, sender, callback) => {
|
|
|
|
// is tab highlighted
|
|
|
|
if (!sender.tab.highlighted) return;
|
|
|
|
|
|
|
|
// is window focused
|
|
|
|
chrome.windows.get(sender.tab.windowId, {}, (window) => {
|
|
|
|
if (!window.focused) return;
|
|
|
|
|
|
|
|
// get zoom value
|
|
|
|
chrome.tabs.getZoom(sender.tab.id, (zoom) => {
|
|
|
|
let size = {
|
|
|
|
width: message.rect.width * zoom,
|
|
|
|
height: message.rect.height * zoom,
|
|
|
|
};
|
|
|
|
|
|
|
|
// attach to window
|
|
|
|
tryAttach(sender.tab.windowId, {
|
|
|
|
name: matchChannelName(sender.tab.url),
|
|
|
|
size: size,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerLoop() {
|
|
|
|
// loop until the runtime objects exists because I can't be arsed to figure out the proper way to do this
|
|
|
|
if (chrome.runtime === undefined) {
|
|
|
|
setTimeout(registerLoop(), 100);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerTheGarbage();
|
|
|
|
}
|
|
|
|
registerLoop();
|
|
|
|
|
|
|
|
// attach chatterino to a chrome window
|
|
|
|
function tryAttach(windowId, data) {
|
|
|
|
data.action = "select";
|
|
|
|
data.attach = true;
|
|
|
|
data.type = "twitch";
|
|
|
|
data.winId = "" + windowId;
|
|
|
|
|
|
|
|
let port = getPort();
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
port.postMessage(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// detach chatterino from a chrome window
|
|
|
|
function tryDetach(windowId) {
|
|
|
|
let port = getPort();
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
port.postMessage({
|
|
|
|
action: "detach",
|
|
|
|
winId: "" + windowId
|
|
|
|
})
|
2018-04-12 01:17:25 +02:00
|
|
|
}
|
|
|
|
}
|