added some more race conditions to the browser extension

This commit is contained in:
fourtf 2018-05-29 23:58:11 +02:00
parent 6d4344e66c
commit e01a3a0978
4 changed files with 117 additions and 69 deletions

View file

@ -8,6 +8,22 @@ const ignoredPages = {
"directory": true, "directory": true,
}; };
/// return channel name if it should contain a chat
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;
}
const appName = "com.chatterino.chatterino"; const appName = "com.chatterino.chatterino";
let port = null; let port = null;
@ -41,15 +57,11 @@ function connectPort() {
// tab activated // tab activated
chrome.tabs.onActivated.addListener((activeInfo) => { chrome.tabs.onActivated.addListener((activeInfo) => {
console.log(0)
chrome.tabs.get(activeInfo.tabId, (tab) => { chrome.tabs.get(activeInfo.tabId, (tab) => {
console.log(1)
if (!tab || !tab.url) return; if (!tab || !tab.url) return;
console.log(2)
chrome.windows.get(tab.windowId, {}, (window) => { chrome.windows.get(tab.windowId, {}, (window) => {
if (!window.focused) return; if (!window.focused) return;
console.log(3)
onTabSelected(tab.url, tab); onTabSelected(tab.url, tab);
}); });
@ -76,8 +88,12 @@ chrome.windows.onRemoved.addListener((windowId) => {
// window selected // window selected
chrome.windows.onFocusChanged.addListener((windowId) => { chrome.windows.onFocusChanged.addListener((windowId) => {
chrome.tabs.query({windowId: windowId, highlighted: true}, (tabs) => { console.log(windowId);
if (tabs.length >= 1) { if (windowId == -1) return;
// this returns all tabs when the query fails
chrome.tabs.query({ windowId: windowId, highlighted: true }, (tabs) => {
if (tabs.length === 1) {
let tab = tabs[0]; let tab = tabs[0];
onTabSelected(tab.url, tab); onTabSelected(tab.url, tab);
@ -86,33 +102,19 @@ chrome.windows.onFocusChanged.addListener((windowId) => {
}); });
/// return channel name if it should contain a chat
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;
}
// attach or detach from tab // attach or detach from tab
function onTabSelected(url, tab) { function onTabSelected(url, tab) {
let channelName = matchChannelName(url); let channelName = matchChannelName(url);
if (channelName) { if (channelName) {
chrome.windows.get(tab.windowId, {}, (window) => { // chrome.windows.get(tab.windowId, {}, (window) => {
// attach to window // // attach to window
tryAttach(tab.windowId, { // tryAttach(tab.windowId, {
name: channelName, // name: channelName,
yOffset: window.height - tab.height, // yOffset: window.height - tab.height,
}); // });
}); // });
} else { } else {
// detach from window // detach from window
tryDetach(tab.windowId); tryDetach(tab.windowId);
@ -120,8 +122,9 @@ function onTabSelected(url, tab) {
} }
// receiving messages from the inject script // receiving messages from the inject script
function registerTheGarbage() { chrome.runtime.onMessage.addListener((message, sender, callback) => {
chrome.runtime.onMessage.addListener((message, sender, callback) => { console.log(message);
// is tab highlighted // is tab highlighted
if (!sender.tab.highlighted) return; if (!sender.tab.highlighted) return;
@ -132,30 +135,21 @@ function registerTheGarbage() {
// get zoom value // get zoom value
chrome.tabs.getZoom(sender.tab.id, (zoom) => { chrome.tabs.getZoom(sender.tab.id, (zoom) => {
let size = { let size = {
width: message.rect.width * zoom, width: Math.floor(message.rect.width * zoom),
height: message.rect.height * zoom, height: Math.floor(message.rect.height * zoom),
}; };
console.log(zoom);
// attach to window // attach to window
tryAttach(sender.tab.windowId, { tryAttach(sender.tab.windowId, {
name: matchChannelName(sender.tab.url), name: matchChannelName(sender.tab.url),
size: size, 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 // attach chatterino to a chrome window
function tryAttach(windowId, data) { function tryAttach(windowId, data) {

View file

@ -1,5 +1,6 @@
(() => { (() => {
let lastRect = {}; let lastRect = {};
let port = null;
function log(str) { function log(str) {
console.log("Chatterino Native: " + str); console.log("Chatterino Native: " + str);
@ -10,6 +11,8 @@
} }
function queryChatRect() { function queryChatRect() {
if (!matchChannelName(window.location.href)) return;
let element = findChatDiv(); let element = findChatDiv();
if (element === undefined) { if (element === undefined) {
@ -17,9 +20,24 @@
return; return;
} }
// element.firstChild.style.opacity = 0; if (!element.chatterino) {
let xd = element.getElementsByClassName("channel-page__right-column")[0]
if (xd != undefined) {
element.chatterino = true;
xd.style.opacity = 0;
setTimeout(() => {
xd.innerHTML = "<div style='width: 340px; height: 100%; justify-content: center; display: flex; flex-direction: column; text-align: center; color: #999; user-select: none;'>" +
"Disconnected from the chatterino extension.<br><br>Please refresh the page." +
"</div>";
xd.style.opacity = 1;
}, 2000);
}
}
let rect = element.getBoundingClientRect(); let rect = element.getBoundingClientRect();
// if ( // if (
// lastRect.left == rect.left && // lastRect.left == rect.left &&
// lastRect.right == rect.right && // lastRect.right == rect.right &&
@ -27,7 +45,6 @@
// lastRect.bottom == rect.bottom // lastRect.bottom == rect.bottom
// ) { // ) {
// // log("skipped sending message"); // // log("skipped sending message");
// return; // return;
// } // }
lastRect = rect; lastRect = rect;
@ -36,10 +53,12 @@
rect: rect, rect: rect,
}; };
chrome.runtime.sendMessage(data, (response) => { try {
// log("received message response"); chrome.runtime.sendMessage(data);
// console.log(response) } catch {
}); // failed to send a message to the runtime -> maybe the extension got reloaded
// alert("reload the page to re-enable chatterino native");
}
} }
function queryCharRectLoop() { function queryCharRectLoop() {
@ -47,11 +66,43 @@
queryChatRect(); queryChatRect();
let t2 = performance.now(); let t2 = performance.now();
console.log("queryCharRect " + (t2 - t1) + "ms"); console.log("queryCharRect " + (t2 - t1) + "ms");
setTimeout(queryCharRectLoop, 500); // setTimeout(queryCharRectLoop, 500);
}
const ignoredPages = {
"settings": true,
"payments": true,
"inventory": true,
"messages": true,
"subscriptions": true,
"friends": true,
"directory": true,
};
/// return channel name if it should contain a chat
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;
} }
queryCharRectLoop(); queryCharRectLoop();
window.addEventListener("load", () => {
setTimeout(queryChatRect, 1000);
});
window.addEventListener("resize", queryChatRect); window.addEventListener("resize", queryChatRect);
window.addEventListener("focus", queryChatRect);
window.addEventListener("mouseup", () => {
setTimeout(queryChatRect, 10);
});
log("initialized"); log("initialized");
})() })()

View file

@ -172,10 +172,12 @@ void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &ro
if (attach) { if (attach) {
#ifdef USEWINSDK #ifdef USEWINSDK
if (args.height != -1) {
auto *window = widgets::AttachedWindow::get(::GetForegroundWindow(), args); auto *window = widgets::AttachedWindow::get(::GetForegroundWindow(), args);
if (!name.isEmpty()) { if (!name.isEmpty()) {
window->setChannel(app->twitch.server->getOrAddChannel(name)); window->setChannel(app->twitch.server->getOrAddChannel(name));
} }
}
// window->show(); // window->show();
#endif #endif
} }

View file

@ -80,7 +80,7 @@ AttachedWindow *AttachedWindow::get(void *target, const GetArgs &args)
if (show) { if (show) {
window->show(); window->show();
window->resize(size); // window->resize(size);
} }
return window; return window;
@ -156,8 +156,9 @@ void AttachedWindow::attachToHwnd(void *_hwnd)
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
if (this->_height == -1) { if (this->_height == -1) {
::MoveWindow(hwnd, rect.right - this->_width - 8, rect.top + this->yOffset - 8, // ::MoveWindow(hwnd, rect.right - this->_width - 8, rect.top + this->yOffset
this->_width, rect.bottom - rect.top - this->yOffset, false); // - 8,
// this->_width, rect.bottom - rect.top - this->yOffset, false);
} else { } else {
::MoveWindow(hwnd, rect.right - this->_width - 8, rect.bottom - this->_height - 8, ::MoveWindow(hwnd, rect.right - this->_width - 8, rect.bottom - this->_height - 8,
this->_width, this->_height, false); this->_width, this->_height, false);