mirror-chatterino2/browser_ext/inject.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-05-28 18:25:19 +02:00
(() => {
let lastRect = {};
let port = null;
2018-05-28 18:25:19 +02:00
2018-05-30 17:17:27 +02:00
let installedObjects = {};
let rightCollapseButton = null;
let isCollapsed = false;
const ignoredPages = {
"settings": true,
"payments": true,
"inventory": true,
"messages": true,
"subscriptions": true,
"friends": true,
"directory": true,
};
let findChatDiv = () => document.getElementsByClassName("right-column")[0];
let findRightCollapse = () => document.getElementsByClassName("right-column__toggle-visibility")[0];
let findRightColumn = () => document.getElementsByClassName("channel-page__right-column")[0];
let findNavBar = () => document.getElementsByClassName("top-nav__menu")[0];
// logging function
2018-05-28 18:25:19 +02:00
function log(str) {
console.log("Chatterino Native: " + str);
}
2018-05-30 17:17:27 +02:00
// install events
function installChatterino() {
log("trying to install events");
let retry = false;
// right collapse button
if (!installedObjects.rightCollapse) {
retry = true;
let x = findRightCollapse();
if (x != undefined) {
rightCollapseButton = x;
x.addEventListener("click", () => {
2018-05-30 17:17:27 +02:00
let y = findChatDiv();
if (parseInt(y.style.width) == 0) {
y.style.width = "340px";
isCollapsed = false;
} else {
y.style.width = 0;
isCollapsed = true;
}
});
installedObjects.rightCollapse = true;
}
}
// right column
if (!installedObjects.rightColumn && installedObjects.rightCollapse) {
let x = findChatDiv();
if (x != undefined && x.children.length >= 2) {
x.children[0].innerHTML = "<div style='width: 340px; height: 100%; justify-content: center; display: flex; flex-direction: column; text-align: center; color: #999; user-select: none; background: #222;'>" +
"Disconnected from the chatterino extension.<br><br>Please focus the window or refresh the page." +
"</div>";
installedObjects.rightColumn = true;
} else {
retry = true;
}
}
// nav bar
2018-05-30 17:30:31 +02:00
if (!installedObjects.topNav) {
if (rightCollapseButton) {
let x = findNavBar();
2018-05-30 17:17:27 +02:00
2018-05-30 17:30:31 +02:00
x.addEventListener("mouseup", () => {
console.log(isCollapsed)
2018-05-30 17:30:31 +02:00
if (!isCollapsed) {
let collapse = findRightCollapse();
collapse.click();
}
});
installedObjects.topNav = true;
} else {
retry = true;
}
2018-05-30 17:17:27 +02:00
}
// retry if needed
if (retry) {
setTimeout(installChatterino, 1000);
} else {
log("installed all events");
}
2018-05-28 18:25:19 +02:00
}
2018-05-30 17:17:27 +02:00
// query the rect of the chat
2018-05-28 18:25:19 +02:00
function queryChatRect() {
if (!matchChannelName(window.location.href)) return;
2018-05-28 18:25:19 +02:00
let element = findChatDiv();
if (element === undefined) {
log("failed to find chat div");
return;
}
let rect = element.getBoundingClientRect();
2018-05-30 17:17:27 +02:00
/* if (
lastRect.left == rect.left &&
lastRect.right == rect.right &&
lastRect.top == rect.top &&
lastRect.bottom == rect.bottom
) {
// log("skipped sending message");
return;
} */
2018-05-28 18:25:19 +02:00
lastRect = rect;
let data = {
rect: rect,
};
2018-05-30 17:17:27 +02:00
isCollapsed = rect.width == 0;
try {
chrome.runtime.sendMessage(data);
} catch {
// failed to send a message to the runtime -> maybe the extension got reloaded
// alert("reload the page to re-enable chatterino native");
}
2018-05-28 18:25:19 +02:00
}
2018-05-30 17:17:27 +02:00
function queryChatRectLoop() {
2018-05-28 18:25:19 +02:00
let t1 = performance.now();
queryChatRect();
let t2 = performance.now();
console.log("queryCharRect " + (t2 - t1) + "ms");
// setTimeout(queryCharRectLoop, 500);
}
2018-05-30 17:17:27 +02:00
// return channel name if it should contain a chat or undefined
function matchChannelName(url) {
if (!url)
return undefined;
const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9_]+)\/?$/);
let channelName;
if (match && (channelName = match[2], !ignoredPages[channelName])) {
return channelName;
}
return undefined;
2018-05-28 18:25:19 +02:00
}
2018-05-30 17:17:27 +02:00
// event listeners
window.addEventListener("load", () => setTimeout(queryChatRect, 1000));
2018-05-28 18:25:19 +02:00
window.addEventListener("resize", queryChatRect);
window.addEventListener("focus", queryChatRect);
2018-05-30 17:17:27 +02:00
window.addEventListener("mouseup", () => setTimeout(queryChatRect, 10));
window.addEventListener("hashchange", () => {
installedObjects = {};
installChatterino();
});
2018-05-28 18:25:19 +02:00
2018-05-30 17:17:27 +02:00
//
log("hello there in the dev tools 👋");
queryChatRectLoop();
installChatterino();
2018-05-28 18:25:19 +02:00
})()