2023-05-19 15:02:25 -04:00
|
|
|
/* eslint strict: off */
|
|
|
|
'use strict';
|
|
|
|
(() => {
|
2024-10-09 17:09:09 -04:00
|
|
|
const browser = globalThis.browser ?? globalThis.chrome;
|
2023-05-19 15:02:25 -04:00
|
|
|
|
2024-10-09 17:09:09 -04:00
|
|
|
if (
|
|
|
|
// Don't run on certain sub-domains.
|
|
|
|
/^(?:localhost\.rig|blog|im|chatdepot|tmi|api|brand|dev|gql|passport)\./.test(location.hostname)
|
|
|
|
||
|
|
|
|
// Don't run on pages that have disabled FFZ.
|
|
|
|
/disable_frankerfacez/.test(location.search)
|
2024-10-22 17:19:03 -04:00
|
|
|
||
|
|
|
|
// Don't run on pages we've already run on.
|
|
|
|
document.body.dataset.ffzSource
|
2024-10-09 17:09:09 -04:00
|
|
|
) {
|
|
|
|
// Tell the service worker we aren't injecting.
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
type: 'ffz_not_supported'
|
|
|
|
});
|
2023-11-06 20:47:19 -05:00
|
|
|
return;
|
2024-10-09 17:09:09 -04:00
|
|
|
}
|
|
|
|
|
2024-10-22 17:19:03 -04:00
|
|
|
document.body.dataset.ffzSource = 'extension';
|
|
|
|
|
2024-10-09 17:09:09 -04:00
|
|
|
// Make sure to wake the service worker up early.
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
type: 'ffz_injecting'
|
|
|
|
});
|
|
|
|
|
2024-12-03 16:15:11 -05:00
|
|
|
// Set up a bridge for connections, since Firefox
|
|
|
|
// doesn't support externally_connectable.
|
|
|
|
const connections = new Map;
|
|
|
|
|
|
|
|
function handleConnect(id) {
|
|
|
|
if ( connections.has(id) )
|
2024-10-09 17:09:09 -04:00
|
|
|
return;
|
|
|
|
|
2024-12-03 16:15:11 -05:00
|
|
|
const port = browser.runtime.connect();
|
|
|
|
connections.set(id, port);
|
|
|
|
|
|
|
|
port.onMessage.addListener(msg => {
|
|
|
|
window.postMessage({
|
|
|
|
type: 'ffz-con-message',
|
|
|
|
id,
|
|
|
|
payload: msg
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
port.onDisconnect.addListener(() => {
|
|
|
|
connections.delete(id);
|
|
|
|
window.postMessage({
|
|
|
|
type: 'ffz-con-disconnect',
|
|
|
|
id
|
2024-10-09 17:09:09 -04:00
|
|
|
});
|
2024-12-03 16:15:11 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDisconnect(id) {
|
|
|
|
const port = connections.get(id);
|
|
|
|
if ( port ) {
|
|
|
|
connections.delete(id);
|
|
|
|
port.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMessage(id, payload) {
|
|
|
|
const port = connection.get(id);
|
|
|
|
if ( port ) {
|
|
|
|
port.postMessage(payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', evt => {
|
|
|
|
if (evt.source !== window || ! evt.data )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const { type, id, payload } = evt.data;
|
|
|
|
|
|
|
|
if ( type === 'ffz-con-connect' )
|
|
|
|
handleConnect(id);
|
|
|
|
|
|
|
|
else if ( type === 'ffz-con-message' )
|
|
|
|
handleMessage(id, payload);
|
|
|
|
|
|
|
|
else if ( type === 'ffz-con-disconnect' )
|
|
|
|
handleDisconnect(id);
|
2024-10-09 17:09:09 -04:00
|
|
|
});
|
2023-11-06 20:47:19 -05:00
|
|
|
|
2024-12-03 16:15:11 -05:00
|
|
|
|
|
|
|
// Let the extension send messages to the page directly.
|
2024-10-09 17:09:09 -04:00
|
|
|
browser.runtime.onMessage.addListener((msg, sender) => {
|
2024-10-11 17:12:54 -04:00
|
|
|
if (msg?.type === 'ffz_to_page')
|
|
|
|
window.postMessage(msg.data, '*');
|
|
|
|
|
|
|
|
return false;
|
2024-10-09 17:09:09 -04:00
|
|
|
});
|
2023-09-09 17:43:51 -04:00
|
|
|
|
2024-10-09 17:09:09 -04:00
|
|
|
// Now, inject our script into the page context.
|
|
|
|
const HOST = location.hostname,
|
2023-09-07 14:05:10 -04:00
|
|
|
SERVER = browser.runtime.getURL("web"),
|
2023-05-19 15:02:25 -04:00
|
|
|
script = document.createElement('script');
|
|
|
|
|
|
|
|
let FLAVOR =
|
|
|
|
HOST.includes('player') ? 'player' :
|
|
|
|
HOST.includes('clips') ? 'clips' :
|
|
|
|
(location.pathname === '/p/ffz_bridge/' ? 'bridge' : 'avalon');
|
|
|
|
|
|
|
|
if (FLAVOR === 'clips' && location.pathname === '/embed')
|
|
|
|
FLAVOR = 'player';
|
|
|
|
|
|
|
|
script.id = 'ffz-script';
|
|
|
|
script.async = true;
|
|
|
|
script.crossOrigin = 'anonymous';
|
|
|
|
script.src = `${SERVER}/${FLAVOR}.js?_=${Date.now()}`;
|
2023-09-01 17:01:52 -04:00
|
|
|
script.dataset.path = SERVER;
|
2023-09-07 14:05:10 -04:00
|
|
|
|
2023-05-19 15:02:25 -04:00
|
|
|
document.head.appendChild(script);
|
|
|
|
})();
|