Add auto-config import to clickbait extension, move more funcs to shared lib

Also fix a isSafari call
This commit is contained in:
Ajay
2023-05-08 17:02:49 -04:00
parent 71dd4a97bf
commit f2482ee973
14 changed files with 51 additions and 93 deletions

View File

@@ -30,5 +30,11 @@
"guidelines": "https://wiki.sponsor.ajay.app/w/Guidelines",
"mute": "https://wiki.sponsor.ajay.app/w/Mute_Segment",
"chapter": "https://wiki.sponsor.ajay.app/w/Chapter"
}
},
"extensionCommunicationAllowList": [
"enamippconapkdmgfgjchkhakpfinmaj",
"deArrow@ajay.app",
"deArrowBETA@ajay.app",
"app.ajay.dearrow.extension"
]
}

14
package-lock.json generated
View File

@@ -27,7 +27,7 @@
],
"license": "LGPL-3.0-or-later",
"dependencies": {
"@ajayyy/maze-utils": "1.1.24",
"@ajayyy/maze-utils": "1.1.25",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
@@ -67,9 +67,9 @@
}
},
"node_modules/@ajayyy/maze-utils": {
"version": "1.1.24",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.24.tgz",
"integrity": "sha512-dtzhC7Mv9G05dk6PBCy4Z+mzsC7n6RKjR+S419nJzgRWMVP/f41djHZXq/TMGgOmRYafKiEJlHz7o/Zp1xZUxg==",
"version": "1.1.25",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.25.tgz",
"integrity": "sha512-GntQY3v0SfP35/IYuHeCeCuT7z+Pc15padVmRoYqD9aShsU0yrNBgmFJWxkFMIDoltMiY4ZqSWNlPX5p+Vyovw==",
"funding": [
{
"type": "individual",
@@ -13602,9 +13602,9 @@
},
"dependencies": {
"@ajayyy/maze-utils": {
"version": "1.1.24",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.24.tgz",
"integrity": "sha512-dtzhC7Mv9G05dk6PBCy4Z+mzsC7n6RKjR+S419nJzgRWMVP/f41djHZXq/TMGgOmRYafKiEJlHz7o/Zp1xZUxg=="
"version": "1.1.25",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.25.tgz",
"integrity": "sha512-GntQY3v0SfP35/IYuHeCeCuT7z+Pc15padVmRoYqD9aShsU0yrNBgmFJWxkFMIDoltMiY4ZqSWNlPX5p+Vyovw=="
},
"@ampproject/remapping": {
"version": "2.2.0",

View File

@@ -4,7 +4,7 @@
"description": "",
"main": "background.js",
"dependencies": {
"@ajayyy/maze-utils": "1.1.24",
"@ajayyy/maze-utils": "1.1.25",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"

View File

@@ -79,6 +79,20 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
}
});
chrome.runtime.onMessageExternal.addListener((request, sender, callback) => {
if (CompileConfig.extensionCommunicationAllowList.includes(sender.id)) {
if (request.message === "requestConfig") {
callback({
userID: Config.config.userID,
allowExpirements: Config.config.allowExpirements,
showDonationLink: Config.config.showDonationLink,
showUpsells: Config.config.showUpsells,
darkMode: Config.config.darkMode,
})
}
}
});
chrome.runtime.onConnect.addListener((port) => {
if (port.name === "popup") {
chrome.tabs.query({

View File

@@ -7,13 +7,13 @@ import NoticeTextSelectionComponent from "./NoticeTextSectionComponent";
import Utils from "../utils";
const utils = new Utils();
import { getSkippingText } from "../utils/categoryUtils";
import { keybindToString } from "../utils/configUtils";
import ThumbsUpSvg from "../svg-icons/thumbs_up_svg";
import ThumbsDownSvg from "../svg-icons/thumbs_down_svg";
import PencilSvg from "../svg-icons/pencil_svg";
import { downvoteButtonColor, SkipNoticeAction } from "../utils/noticeUtils";
import { generateUserID } from "@ajayyy/maze-utils/lib/setup";
import { keybindToString } from "@ajayyy/maze-utils/lib/config";
enum SkipButtonState {
Undo, // Unskip

View File

@@ -1,9 +1,8 @@
import * as React from "react";
import { createRoot, Root } from 'react-dom/client';
import Config from "../../config";
import { Keybind } from "../../types";
import KeybindDialogComponent from "./KeybindDialogComponent";
import { keybindEquals, keybindToString, formatKey } from "../../utils/configUtils";
import { formatKey, Keybind, keybindEquals, keybindToString } from "@ajayyy/maze-utils/lib/config";
export interface KeybindProps {
option: string;

View File

@@ -1,8 +1,7 @@
import * as React from "react";
import { ChangeEvent } from "react";
import Config from "../../config";
import { Keybind } from "../../types";
import { keybindEquals, formatKey } from "../../utils/configUtils";
import { Keybind, formatKey, keybindEquals } from "@ajayyy/maze-utils/lib/config";
export interface KeybindDialogProps {
option: string;

View File

@@ -1,8 +1,7 @@
import * as CompileConfig from "../config.json";
import * as invidiousList from "../ci/invidiouslist.json";
import { Category, CategorySelection, CategorySkipOption, NoticeVisbilityMode, PreviewBarOption, SponsorTime, Keybind, VideoID, SponsorHideType } from "./types";
import { keybindEquals } from "./utils/configUtils";
import { ProtoConfig } from "@ajayyy/maze-utils/lib/config";
import { Category, CategorySelection, CategorySkipOption, NoticeVisbilityMode, PreviewBarOption, SponsorTime, VideoID, SponsorHideType } from "./types";
import { Keybind, ProtoConfig, keybindEquals } from "@ajayyy/maze-utils/lib/config";
import { HashedValue } from "@ajayyy/maze-utils/lib/hash";
export interface Permission {

View File

@@ -6,7 +6,6 @@ import {
ChannelIDInfo,
ChannelIDStatus,
ContentContainer,
Keybind,
ScheduledTime,
SegmentUUID,
SkipToTimeParams,
@@ -26,7 +25,6 @@ import { Message, MessageResponse, VoteResponse } from "./messageTypes";
import { SkipButtonControlBar } from "./js-components/skipButtonControlBar";
import { getStartTimeFromUrl } from "./utils/urlParser";
import { getControls, getExistingChapters, getHashParams, isVisible } from "./utils/pageUtils";
import { isSafari, keybindEquals } from "./utils/configUtils";
import { CategoryPill } from "./render/CategoryPill";
import { AnimationUtils } from "./utils/animationUtils";
import { GenericUtils } from "./utils/genericUtils";
@@ -34,10 +32,10 @@ import { logDebug } from "./utils/logger";
import { importTimes } from "./utils/exporter";
import { ChapterVote } from "./render/ChapterVote";
import { openWarningDialog } from "./utils/warnings";
import { waitFor } from "@ajayyy/maze-utils";
import { isFirefoxOrSafari, waitFor } from "@ajayyy/maze-utils";
import { getFormattedTime } from "@ajayyy/maze-utils/lib/formating";
import { getChannelIDInfo, getVideo, getIsAdPlaying, getIsLivePremiere, setIsAdPlaying, checkVideoIDChange, getVideoID, getYouTubeVideoID, setupVideoModule, checkIfNewVideoID, isOnInvidious, isOnMobileYouTube } from "@ajayyy/maze-utils/lib/video";
import { StorageChangesObject } from "@ajayyy/maze-utils/lib/config";
import { Keybind, StorageChangesObject, isSafari, keybindEquals } from "@ajayyy/maze-utils/lib/config";
import { findValidElement } from "@ajayyy/maze-utils/lib/dom"
import { getHash, HashedValue } from "@ajayyy/maze-utils/lib/hash";
import { generateUserID } from "@ajayyy/maze-utils/lib/setup";
@@ -675,7 +673,7 @@ async function startSponsorSchedule(includeIntersectingSegments = false, current
let delayTime = timeUntilSponsor * 1000 * (1 / getVideo().playbackRate);
if (delayTime < 300) {
let forceStartIntervalTime: number | null = null;
if (utils.isFirefox() && !isSafari() && delayTime > 100) {
if (isFirefoxOrSafari() && !isSafari() && delayTime > 100) {
forceStartIntervalTime = await waitForNextTimeChange();
}
@@ -692,7 +690,7 @@ async function startSponsorSchedule(includeIntersectingSegments = false, current
currentSkipInterval = setInterval(() => {
// Estimate delay, but only take the current time right after a change
// Current time remains the same for many "frames" on Firefox
if (utils.isFirefox() && !lastKnownVideoTime.fromPause && startWaitingForReportedTimeToChange
if (isFirefoxOrSafari() && !lastKnownVideoTime.fromPause && startWaitingForReportedTimeToChange
&& reportedVideoTimeAtStart !== getVideo().currentTime) {
startWaitingForReportedTimeToChange = false;
const delay = getVirtualTime() - getVideo().currentTime;
@@ -702,7 +700,7 @@ async function startSponsorSchedule(includeIntersectingSegments = false, current
const intervalDuration = performance.now() - startIntervalTime;
if (intervalDuration + skipBuffer * 1000 >= delayTime || getVideo().currentTime >= skipTime[0]) {
clearInterval(currentSkipInterval);
if (!utils.isFirefox() && !getVideo().muted) {
if (!isFirefoxOrSafari() && !getVideo().muted) {
// Workaround for more accurate skipping on Chromium
getVideo().muted = true;
getVideo().muted = false;
@@ -714,7 +712,7 @@ async function startSponsorSchedule(includeIntersectingSegments = false, current
} else {
logDebug(`Starting timeout to skip ${getVideo().currentTime} to skip at ${skipTime[0]}`);
const offset = (utils.isFirefox() && !isSafari() ? 300 : 150);
const offset = (isFirefoxOrSafari() && !isSafari() ? 300 : 150);
// Schedule for right before to be more precise than normal timeout
currentSkipSchedule = setTimeout(skippingFunction, Math.max(0, delayTime - offset));
}
@@ -929,7 +927,7 @@ function updateVirtualTime() {
lastKnownVideoTime.preciseTime = performance.now();
// If on Firefox, wait for the second time change (time remains fixed for many "frames" for privacy reasons)
if (utils.isFirefox()) {
if (isFirefoxOrSafari()) {
let count = 0;
let rawCount = 0;
let lastTime = lastKnownVideoTime.videoTime;
@@ -1195,7 +1193,7 @@ function retryFetch(errorCode: number): void {
*/
function startSkipScheduleCheckingForStartSponsors() {
// switchingVideos is ignored in Safari due to event fire order. See #1142
if ((!switchingVideos || isSafari) && sponsorTimes) {
if ((!switchingVideos || isSafari()) && sponsorTimes) {
// See if there are any starting sponsors
let startingSegmentTime = getStartTimeFromUrl(document.URL) || -1;
let found = false;
@@ -2350,7 +2348,7 @@ function hotkeyListener(e: KeyboardEvent): void {
* Adds the CSS to the page if needed. Required on optional sites with Chrome.
*/
function addCSS() {
if (!utils.isFirefox() && Config.config.invidiousInstances.includes(new URL(document.URL).host)) {
if (!isFirefoxOrSafari() && Config.config.invidiousInstances.includes(new URL(document.URL).host)) {
window.addEventListener("DOMContentLoaded", () => {
const head = document.getElementsByTagName("head")[0];

View File

@@ -1,8 +1,8 @@
import Config from "../config";
import { SponsorTime } from "../types";
import { getSkippingText } from "../utils/categoryUtils";
import { keybindToString } from "../utils/configUtils";
import { AnimationUtils } from "../utils/animationUtils";
import { keybindToString } from "@ajayyy/maze-utils/lib/config";
export interface SkipButtonControlBarProps {
skip: (segment: SponsorTime) => void;

View File

@@ -16,6 +16,7 @@ import { showDonationLink } from "./utils/configUtils";
import { localizeHtmlPage } from "./utils/pageUtils";
import { StorageChangesObject } from "@ajayyy/maze-utils/lib/config";
import { getHash } from "@ajayyy/maze-utils/lib/hash";
import { isFirefoxOrSafari } from "@ajayyy/maze-utils";
const utils = new Utils();
let embed = false;
@@ -186,7 +187,7 @@ async function init() {
}
// Permission needed on Firefox
if (utils.isFirefox()) {
if (isFirefoxOrSafari()) {
const permissionSuccess = await new Promise((resolve) => {
chrome.permissions.request({
origins: [textChangeInput.value + "/"],

View File

@@ -222,14 +222,6 @@ export enum NoticeVisbilityMode {
FadedForAll = 4
}
export type Keybind = {
key: string;
code?: string;
ctrl?: boolean;
alt?: boolean;
shift?: boolean;
}
export interface ButtonListener {
name: string;
listener: (e?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;

View File

@@ -3,10 +3,10 @@ import { CategorySelection, SponsorTime, BackgroundScriptContainer, Registration
import { getHash, HashedValue } from "@ajayyy/maze-utils/lib/hash";
import * as CompileConfig from "../config.json";
import { waitFor } from "@ajayyy/maze-utils";
import { isSafari } from "./utils/configUtils";
import { isFirefoxOrSafari, waitFor } from "@ajayyy/maze-utils";
import { findValidElementFromSelector } from "@ajayyy/maze-utils/lib/dom";
import { FetchResponse, sendRequestToCustomServer } from "@ajayyy/maze-utils/lib/background-request-proxy"
import { isSafari } from "@ajayyy/maze-utils/lib/config";
export default class Utils {
@@ -49,7 +49,7 @@ export default class Utils {
setupExtraSitePermissions(callback: (granted: boolean) => void): void {
let permissions = ["webNavigation"];
if (!isSafari()) permissions.push("declarativeContent");
if (this.isFirefox() && !isSafari()) permissions = [];
if (isFirefoxOrSafari() && !isSafari()) permissions = [];
chrome.permissions.request({
origins: this.getPermissionRegex(),
@@ -113,7 +113,7 @@ export default class Utils {
});
}
if (!this.isFirefox() && chrome.declarativeContent) {
if (!isFirefoxOrSafari() && chrome.declarativeContent) {
// Only if we have permission
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
}
@@ -143,7 +143,7 @@ export default class Utils {
containsInvidiousPermission(): Promise<boolean> {
return new Promise((resolve) => {
let permissions = ["declarativeContent"];
if (this.isFirefox()) permissions = [];
if (isFirefoxOrSafari()) permissions = [];
chrome.permissions.contains({
origins: this.getPermissionRegex(),
@@ -330,13 +330,6 @@ export default class Utils {
return Boolean(num.match(/^[0-9a-f]+$/i));
}
/**
* Is this Firefox (web-extensions)
*/
isFirefox(): boolean {
return typeof(browser) !== "undefined";
}
async addHiddenSegment(videoID: VideoID, segmentUUID: string, hidden: SponsorHideType) {
if (chrome.extension.inIncognitoContext || !Config.config.trackDownvotes) return;

View File

@@ -1,48 +1,5 @@
import Config from "../config";
import { Keybind } from "../types";
export function showDonationLink(): boolean {
return navigator.vendor !== "Apple Computer, Inc." && Config.config.showDonationLink;
}
export function isSafari(): boolean {
return typeof(navigator) !== "undefined" && navigator.vendor === "Apple Computer, Inc.";
}
export function keybindEquals(first: Keybind, second: Keybind): boolean {
if (first == null || second == null ||
Boolean(first.alt) != Boolean(second.alt) || Boolean(first.ctrl) != Boolean(second.ctrl) || Boolean(first.shift) != Boolean(second.shift) ||
first.key == null && first.code == null || second.key == null && second.code == null)
return false;
if (first.code != null && second.code != null)
return first.code === second.code;
if (first.key != null && second.key != null)
return first.key.toUpperCase() === second.key.toUpperCase();
return false;
}
export function formatKey(key: string): string {
if (key == null)
return "";
else if (key == " ")
return "Space";
else if (key.length == 1)
return key.toUpperCase();
else
return key;
}
export function keybindToString(keybind: Keybind): string {
if (keybind == null || keybind.key == null)
return "";
let ret = "";
if (keybind.ctrl)
ret += "Ctrl+";
if (keybind.alt)
ret += "Alt+";
if (keybind.shift)
ret += "Shift+";
return ret += formatKey(keybind.key);
}