diff options
author | Ajay <[email protected]> | 2023-05-08 17:02:49 -0400 |
---|---|---|
committer | Ajay <[email protected]> | 2023-05-08 17:02:49 -0400 |
commit | f2482ee973784bae687a7893dafbcd89d0ad4613 (patch) | |
tree | 1e6307b04d14c7cb0c3c77cf33d542df184b2ac2 | |
parent | 71dd4a97bfa6881555e1a0e3f799ddea0846ea81 (diff) | |
download | SponsorBlock-f2482ee973784bae687a7893dafbcd89d0ad4613.tar.gz SponsorBlock-f2482ee973784bae687a7893dafbcd89d0ad4613.zip |
Add auto-config import to clickbait extension, move more funcs to shared lib
Also fix a isSafari call
-rw-r--r-- | config.json.example | 8 | ||||
-rw-r--r-- | package-lock.json | 14 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/background.ts | 14 | ||||
-rw-r--r-- | src/components/SkipNoticeComponent.tsx | 2 | ||||
-rw-r--r-- | src/components/options/KeybindComponent.tsx | 3 | ||||
-rw-r--r-- | src/components/options/KeybindDialogComponent.tsx | 3 | ||||
-rw-r--r-- | src/config.ts | 5 | ||||
-rw-r--r-- | src/content.ts | 20 | ||||
-rw-r--r-- | src/js-components/skipButtonControlBar.ts | 2 | ||||
-rw-r--r-- | src/options.ts | 3 | ||||
-rw-r--r-- | src/types.ts | 8 | ||||
-rw-r--r-- | src/utils.ts | 17 | ||||
-rw-r--r-- | src/utils/configUtils.ts | 43 |
14 files changed, 51 insertions, 93 deletions
diff --git a/config.json.example b/config.json.example index c5e86444..66dc1567 100644 --- a/config.json.example +++ b/config.json.example @@ -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", + "[email protected]", + "[email protected]", + "app.ajay.dearrow.extension" + ] } diff --git a/package-lock.json b/package-lock.json index b0adbeb7..87fe7040 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 3b139822..112764be 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/background.ts b/src/background.ts index ad5fe807..3f2ccfb7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -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({ diff --git a/src/components/SkipNoticeComponent.tsx b/src/components/SkipNoticeComponent.tsx index b84c2b2d..9d7f450f 100644 --- a/src/components/SkipNoticeComponent.tsx +++ b/src/components/SkipNoticeComponent.tsx @@ -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 diff --git a/src/components/options/KeybindComponent.tsx b/src/components/options/KeybindComponent.tsx index 4d49c7f4..180d57f2 100644 --- a/src/components/options/KeybindComponent.tsx +++ b/src/components/options/KeybindComponent.tsx @@ -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; diff --git a/src/components/options/KeybindDialogComponent.tsx b/src/components/options/KeybindDialogComponent.tsx index 7226dff2..2c5405f3 100644 --- a/src/components/options/KeybindDialogComponent.tsx +++ b/src/components/options/KeybindDialogComponent.tsx @@ -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; diff --git a/src/config.ts b/src/config.ts index 7f4e43df..f91024fc 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 { diff --git a/src/content.ts b/src/content.ts index 7bb5847d..a716eddf 100644 --- a/src/content.ts +++ b/src/content.ts @@ -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]; diff --git a/src/js-components/skipButtonControlBar.ts b/src/js-components/skipButtonControlBar.ts index d78f8a74..6b3654f2 100644 --- a/src/js-components/skipButtonControlBar.ts +++ b/src/js-components/skipButtonControlBar.ts @@ -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; diff --git a/src/options.ts b/src/options.ts index 277717a1..63384605 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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 + "/"], diff --git a/src/types.ts b/src/types.ts index 7780a4a4..4913fdad 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; diff --git a/src/utils.ts b/src/utils.ts index 82ffc61a..7709d9df 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; diff --git a/src/utils/configUtils.ts b/src/utils/configUtils.ts index 0afecbc1..8aec5208 100644 --- a/src/utils/configUtils.ts +++ b/src/utils/configUtils.ts @@ -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); }
\ No newline at end of file |