Skip to content

Commit

Permalink
Support live updates on firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Jul 28, 2023
1 parent db9fc11 commit 4069545
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 20 deletions.
49 changes: 33 additions & 16 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import * as documentScript from "../dist/js/document.js";
import { Tooltip } from "./render/Tooltip";
import { isDeArrowInstalled } from "./utils/crossExtension";
import { runCompatibilityChecks } from "./utils/compatibility";
import { cleanPage } from "./utils/pageCleaner";

cleanPage();

const utils = new Utils();

Expand Down Expand Up @@ -1356,18 +1359,20 @@ async function channelIDChange(channelIDInfo: ChannelIDInfo) {
}

function videoElementChange(newVideo: boolean): void {
if (newVideo) {
setupVideoListeners();
setupSkipButtonControlBar();
setupCategoryPill();
}

checkPreviewbarState();

// Incase the page is still transitioning, check again in a few seconds
setTimeout(checkPreviewbarState, 100);
setTimeout(checkPreviewbarState, 1000);
setTimeout(checkPreviewbarState, 5000);
waitFor(() => Config.isReady()).then(() => {
if (newVideo) {
setupVideoListeners();
setupSkipButtonControlBar();
setupCategoryPill();
}

checkPreviewbarState();

// Incase the page is still transitioning, check again in a few seconds
setTimeout(checkPreviewbarState, 100);
setTimeout(checkPreviewbarState, 1000);
setTimeout(checkPreviewbarState, 5000);
})
}

function checkPreviewbarState(): void {
Expand Down Expand Up @@ -2331,11 +2336,17 @@ function previousChapter(): void {
function addHotkeyListener(): void {
document.addEventListener("keydown", hotkeyListener);

document.addEventListener("DOMContentLoaded", () => {
const onLoad = () => {
// Allow us to stop propagation to YouTube by being deeper
document.removeEventListener("keydown", hotkeyListener);
document.body.addEventListener("keydown", hotkeyListener);
});
};

if (document.readyState === "complete") {
onLoad();
} else {
document.addEventListener("DOMContentLoaded", onLoad);
}
}

function hotkeyListener(e: KeyboardEvent): void {
Expand Down Expand Up @@ -2392,7 +2403,7 @@ function hotkeyListener(e: KeyboardEvent): void {
*/
function addCSS() {
if (!isFirefoxOrSafari() && Config.config.invidiousInstances.includes(new URL(document.URL).hostname)) {
window.addEventListener("DOMContentLoaded", () => {
const onLoad = () => {
const head = document.getElementsByTagName("head")[0];

for (const file of utils.css) {
Expand All @@ -2404,7 +2415,13 @@ function addCSS() {

head.appendChild(fileref);
}
});
};

if (document.readyState === "complete") {
onLoad();
} else {
document.addEventListener("DOMContentLoaded", onLoad);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { showDonationLink } from "./utils/configUtils";

import { waitFor } from "./maze-utils";

window.addEventListener('DOMContentLoaded', init);
if (document.readyState === "complete") {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}

async function init() {
localizeHtmlPage();
Expand Down
6 changes: 5 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ let embed = false;
const categoryChoosers: CategoryChooser[] = [];
const unsubmittedVideos: UnsubmittedVideos[] = [];

window.addEventListener('DOMContentLoaded', init);
if (document.readyState === "complete") {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}

async function init() {
localizeHtmlPage();
Expand Down
6 changes: 5 additions & 1 deletion src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const utils = new Utils();
// Probably due to cyclic dependencies
Config.config;

window.addEventListener('DOMContentLoaded', init);
if (document.readyState === "complete") {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}

async function init() {
localizeHtmlPage();
Expand Down
1 change: 1 addition & 0 deletions src/render/GenericNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class GenericNotice {
const referenceNode = options.referenceNode ?? utils.findReferenceNode();

this.noticeElement = document.createElement("div");
this.noticeElement.className = "sponsorSkipNoticeContainer";
this.noticeElement.id = "sponsorSkipNoticeContainer" + idSuffix;

referenceNode.prepend(this.noticeElement);
Expand Down
1 change: 1 addition & 0 deletions src/render/SkipNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SkipNotice {
idSuffix += amountOfPreviousNotices;

this.noticeElement = document.createElement("div");
this.noticeElement.className = "sponsorSkipNoticeContainer";
this.noticeElement.id = "sponsorSkipNoticeContainer" + idSuffix;

referenceNode.prepend(this.noticeElement);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/pageCleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function cleanPage() {
// For live-updates
for (const element of document.querySelectorAll("#categoryPillParent, .playerButton, .sponsorThumbnailLabel, #submissionNoticeContainer, .sponsorSkipNoticeContainer, #sponsorBlockPopupContainer")) {
element.remove();
}
}

0 comments on commit 4069545

Please sign in to comment.