mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-22 14:23:01 +01:00
Fix Shorts support and add userscript voting
Harden Shorts controls, reaction state, and navigation; share voting logic with the extension; improve ratio-bar rendering; and add comprehensive Jest, Playwright, artifact, and live-browser regression coverage.
This commit is contained in:
1 parent
4e6dfd1977
commit
3657de9b2c
74 files changed
+23328
-588
No files matched your search
@@ -0,0 +1,71 @@
|
||||
const CREDENTIALS_KEY = "rydVoteCredentials";
|
||||
|
||||
function hasModernMethod(name) {
|
||||
return typeof GM !== "undefined" && typeof GM?.[name] === "function";
|
||||
}
|
||||
|
||||
async function getStoredValue() {
|
||||
if (hasModernMethod("getValue")) {
|
||||
return GM.getValue(CREDENTIALS_KEY, null);
|
||||
}
|
||||
if (typeof GM_getValue === "function") {
|
||||
return GM_getValue(CREDENTIALS_KEY, null);
|
||||
}
|
||||
throw new Error("Userscript storage API is unavailable");
|
||||
}
|
||||
|
||||
async function setStoredValue(value) {
|
||||
if (hasModernMethod("setValue")) {
|
||||
await GM.setValue(CREDENTIALS_KEY, value);
|
||||
return;
|
||||
}
|
||||
if (typeof GM_setValue === "function") {
|
||||
await GM_setValue(CREDENTIALS_KEY, value);
|
||||
return;
|
||||
}
|
||||
throw new Error("Userscript storage API is unavailable");
|
||||
}
|
||||
|
||||
async function deleteStoredValue() {
|
||||
if (hasModernMethod("deleteValue")) {
|
||||
await GM.deleteValue(CREDENTIALS_KEY);
|
||||
return;
|
||||
}
|
||||
if (typeof GM_deleteValue === "function") {
|
||||
await GM_deleteValue(CREDENTIALS_KEY);
|
||||
return;
|
||||
}
|
||||
|
||||
// Old managers may expose get/set without delete. Null is treated as an
|
||||
// empty credential by load(), while still allowing the client to recover.
|
||||
await setStoredValue(null);
|
||||
}
|
||||
|
||||
function createGmCredentialStore() {
|
||||
return {
|
||||
async load() {
|
||||
const value = await getStoredValue();
|
||||
if (!value || typeof value !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
userId: value.userId,
|
||||
registrationConfirmed: value.registrationConfirmed === true,
|
||||
};
|
||||
},
|
||||
|
||||
async save(credentials) {
|
||||
await setStoredValue({
|
||||
userId: credentials.userId,
|
||||
registrationConfirmed: credentials.registrationConfirmed === true,
|
||||
});
|
||||
},
|
||||
|
||||
async clear() {
|
||||
await deleteStoredValue();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export { CREDENTIALS_KEY, createGmCredentialStore };
|
||||
@@ -0,0 +1,86 @@
|
||||
import { CREDENTIALS_KEY, createGmCredentialStore } from "./gm-credential-store";
|
||||
|
||||
const LEGACY_METHODS = ["GM_getValue", "GM_setValue", "GM_deleteValue"];
|
||||
|
||||
afterEach(() => {
|
||||
delete global.GM;
|
||||
LEGACY_METHODS.forEach((name) => delete global[name]);
|
||||
});
|
||||
|
||||
describe("createGmCredentialStore", () => {
|
||||
it("uses the modern GM storage contract", async () => {
|
||||
let value = null;
|
||||
global.GM = {
|
||||
getValue: jest.fn(async (_key, fallbackValue) => value ?? fallbackValue),
|
||||
setValue: jest.fn(async (_key, nextValue) => {
|
||||
value = nextValue;
|
||||
}),
|
||||
deleteValue: jest.fn(async () => {
|
||||
value = null;
|
||||
}),
|
||||
};
|
||||
const store = createGmCredentialStore();
|
||||
|
||||
expect(await store.load()).toBeNull();
|
||||
await store.save({ userId: "modern-user", registrationConfirmed: true });
|
||||
expect(await store.load()).toEqual({ userId: "modern-user", registrationConfirmed: true });
|
||||
expect(global.GM.setValue).toHaveBeenCalledWith(CREDENTIALS_KEY, {
|
||||
userId: "modern-user",
|
||||
registrationConfirmed: true,
|
||||
});
|
||||
|
||||
await store.clear();
|
||||
expect(global.GM.deleteValue).toHaveBeenCalledWith(CREDENTIALS_KEY);
|
||||
expect(await store.load()).toBeNull();
|
||||
});
|
||||
|
||||
it("uses legacy synchronous storage methods when modern GM methods are absent", async () => {
|
||||
let value = null;
|
||||
global.GM_getValue = jest.fn((_key, fallbackValue) => value ?? fallbackValue);
|
||||
global.GM_setValue = jest.fn((_key, nextValue) => {
|
||||
value = nextValue;
|
||||
});
|
||||
global.GM_deleteValue = jest.fn(() => {
|
||||
value = null;
|
||||
});
|
||||
const store = createGmCredentialStore();
|
||||
|
||||
await store.save({ userId: "legacy-user", registrationConfirmed: true });
|
||||
expect(await store.load()).toEqual({ userId: "legacy-user", registrationConfirmed: true });
|
||||
await store.clear();
|
||||
expect(global.GM_deleteValue).toHaveBeenCalledWith(CREDENTIALS_KEY);
|
||||
expect(await store.load()).toBeNull();
|
||||
});
|
||||
|
||||
it("falls back to a null value when a legacy manager has no delete method", async () => {
|
||||
global.GM_getValue = jest.fn(async (_key, fallbackValue) => fallbackValue);
|
||||
global.GM_setValue = jest.fn(async () => undefined);
|
||||
|
||||
await createGmCredentialStore().clear();
|
||||
expect(global.GM_setValue).toHaveBeenCalledWith(CREDENTIALS_KEY, null);
|
||||
});
|
||||
|
||||
it("normalizes invalid and unconfirmed stored values", async () => {
|
||||
global.GM = {
|
||||
getValue: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce("invalid")
|
||||
.mockResolvedValueOnce({ userId: "pending", registrationConfirmed: false }),
|
||||
};
|
||||
const store = createGmCredentialStore();
|
||||
|
||||
expect(await store.load()).toBeNull();
|
||||
expect(await store.load()).toEqual({ userId: "pending", registrationConfirmed: false });
|
||||
});
|
||||
|
||||
it("propagates storage failures and reports a missing storage API", async () => {
|
||||
global.GM = { getValue: jest.fn(async () => Promise.reject(new Error("GM storage failed"))) };
|
||||
await expect(createGmCredentialStore().load()).rejects.toThrow("GM storage failed");
|
||||
|
||||
delete global.GM;
|
||||
await expect(createGmCredentialStore().load()).rejects.toThrow("Userscript storage API is unavailable");
|
||||
await expect(createGmCredentialStore().save({ userId: "no-storage", registrationConfirmed: true })).rejects.toThrow(
|
||||
"Userscript storage API is unavailable",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
const SYNTHETIC_DISLIKE_KEY_PREFIX = "rydSyntheticDislikedShort:";
|
||||
|
||||
// Each currently disliked Short owns one independent key. Deliberately do not
|
||||
// evict selected videos: forgetting one would make the next click submit -1
|
||||
// again instead of the required neutral (0) transition.
|
||||
|
||||
function hasModernMethod(name) {
|
||||
return typeof GM !== "undefined" && typeof GM?.[name] === "function";
|
||||
}
|
||||
|
||||
async function getStoredValue(key, fallbackValue) {
|
||||
if (hasModernMethod("getValue")) {
|
||||
return GM.getValue(key, fallbackValue);
|
||||
}
|
||||
if (typeof GM_getValue === "function") {
|
||||
return GM_getValue(key, fallbackValue);
|
||||
}
|
||||
throw new Error("Userscript storage API is unavailable");
|
||||
}
|
||||
|
||||
async function setStoredValue(key, value) {
|
||||
if (hasModernMethod("setValue")) {
|
||||
await GM.setValue(key, value);
|
||||
return;
|
||||
}
|
||||
if (typeof GM_setValue === "function") {
|
||||
await GM_setValue(key, value);
|
||||
return;
|
||||
}
|
||||
throw new Error("Userscript storage API is unavailable");
|
||||
}
|
||||
|
||||
async function deleteStoredValue(key) {
|
||||
if (hasModernMethod("deleteValue")) {
|
||||
await GM.deleteValue(key);
|
||||
return;
|
||||
}
|
||||
if (typeof GM_deleteValue === "function") {
|
||||
await GM_deleteValue(key);
|
||||
return;
|
||||
}
|
||||
|
||||
// Some legacy managers do not expose deleteValue. An explicit false value
|
||||
// has the same read semantics and prevents a stale state from returning.
|
||||
await setStoredValue(key, false);
|
||||
}
|
||||
|
||||
function validateVideoId(videoId) {
|
||||
if (typeof videoId !== "string" || videoId.length === 0) {
|
||||
throw new TypeError("videoId must be a non-empty string");
|
||||
}
|
||||
}
|
||||
|
||||
function syntheticDislikeKey(videoId) {
|
||||
return `${SYNTHETIC_DISLIKE_KEY_PREFIX}${videoId}`;
|
||||
}
|
||||
|
||||
function createGmSyntheticDislikeStore() {
|
||||
let mutationQueue = Promise.resolve();
|
||||
|
||||
function enqueueMutation(mutation) {
|
||||
const result = mutationQueue.catch(() => undefined).then(mutation);
|
||||
mutationQueue = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
async isDisliked(videoId) {
|
||||
validateVideoId(videoId);
|
||||
await mutationQueue.catch(() => undefined);
|
||||
return (await getStoredValue(syntheticDislikeKey(videoId), false)) === true;
|
||||
},
|
||||
|
||||
async setDisliked(videoId, disliked) {
|
||||
validateVideoId(videoId);
|
||||
if (typeof disliked !== "boolean") {
|
||||
throw new TypeError("disliked must be a boolean");
|
||||
}
|
||||
|
||||
return enqueueMutation(() =>
|
||||
disliked ? setStoredValue(syntheticDislikeKey(videoId), true) : deleteStoredValue(syntheticDislikeKey(videoId)),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export { SYNTHETIC_DISLIKE_KEY_PREFIX, createGmSyntheticDislikeStore };
|
||||
@@ -0,0 +1,156 @@
|
||||
import { SYNTHETIC_DISLIKE_KEY_PREFIX, createGmSyntheticDislikeStore } from "./gm-synthetic-dislike-store";
|
||||
|
||||
const LEGACY_METHODS = ["GM_getValue", "GM_setValue", "GM_deleteValue"];
|
||||
const stateKey = (videoId) => `${SYNTHETIC_DISLIKE_KEY_PREFIX}${videoId}`;
|
||||
|
||||
function installModernStorage(initialValues = {}) {
|
||||
const values = new Map(Object.entries(initialValues));
|
||||
global.GM = {
|
||||
getValue: jest.fn(async (key, fallbackValue) => (values.has(key) ? values.get(key) : fallbackValue)),
|
||||
setValue: jest.fn(async (key, value) => {
|
||||
values.set(key, value);
|
||||
}),
|
||||
deleteValue: jest.fn(async (key) => {
|
||||
values.delete(key);
|
||||
}),
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
delete global.GM;
|
||||
LEGACY_METHODS.forEach((name) => delete global[name]);
|
||||
});
|
||||
|
||||
describe("createGmSyntheticDislikeStore", () => {
|
||||
it("uses independent per-video values as the authoritative modern GM state", async () => {
|
||||
const values = installModernStorage({ [stateKey("existing-video")]: true });
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
|
||||
await expect(store.isDisliked("existing-video")).resolves.toBe(true);
|
||||
await expect(store.isDisliked("missing-video")).resolves.toBe(false);
|
||||
|
||||
await store.setDisliked("new-video", true);
|
||||
expect(values.get(stateKey("new-video"))).toBe(true);
|
||||
|
||||
await store.setDisliked("existing-video", false);
|
||||
expect(values.has(stateKey("existing-video"))).toBe(false);
|
||||
await expect(store.isDisliked("existing-video")).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("supports legacy synchronous GM methods", async () => {
|
||||
const values = new Map();
|
||||
global.GM_getValue = jest.fn((key, fallbackValue) => (values.has(key) ? values.get(key) : fallbackValue));
|
||||
global.GM_setValue = jest.fn((key, value) => values.set(key, value));
|
||||
global.GM_deleteValue = jest.fn((key) => values.delete(key));
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
|
||||
await store.setDisliked("legacy-video", true);
|
||||
expect(global.GM_setValue).toHaveBeenCalledWith(stateKey("legacy-video"), true);
|
||||
await expect(store.isDisliked("legacy-video")).resolves.toBe(true);
|
||||
|
||||
await store.setDisliked("legacy-video", false);
|
||||
expect(global.GM_deleteValue).toHaveBeenCalledWith(stateKey("legacy-video"));
|
||||
await expect(store.isDisliked("legacy-video")).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("stores false when a legacy manager has no delete method", async () => {
|
||||
const values = new Map();
|
||||
global.GM_getValue = jest.fn((key, fallbackValue) => (values.has(key) ? values.get(key) : fallbackValue));
|
||||
global.GM_setValue = jest.fn((key, value) => values.set(key, value));
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
|
||||
await store.setDisliked("legacy-video", true);
|
||||
await store.setDisliked("legacy-video", false);
|
||||
|
||||
expect(values.get(stateKey("legacy-video"))).toBe(false);
|
||||
await expect(store.isDisliked("legacy-video")).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("does not lose state when two store instances update different videos concurrently", async () => {
|
||||
const values = installModernStorage();
|
||||
const firstStore = createGmSyntheticDislikeStore();
|
||||
const secondStore = createGmSyntheticDislikeStore();
|
||||
|
||||
await Promise.all([firstStore.setDisliked("first-video", true), secondStore.setDisliked("second-video", true)]);
|
||||
|
||||
expect(values.get(stateKey("first-video"))).toBe(true);
|
||||
expect(values.get(stateKey("second-video"))).toBe(true);
|
||||
await expect(firstStore.isDisliked("first-video")).resolves.toBe(true);
|
||||
await expect(secondStore.isDisliked("second-video")).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it("uses the last completed per-video write for same-video races", async () => {
|
||||
const values = new Map();
|
||||
let releaseTrueWrite;
|
||||
let trueWriteStarted;
|
||||
const trueWriteGate = new Promise((resolve) => {
|
||||
releaseTrueWrite = resolve;
|
||||
});
|
||||
const trueWriteEntered = new Promise((resolve) => {
|
||||
trueWriteStarted = resolve;
|
||||
});
|
||||
global.GM = {
|
||||
getValue: jest.fn(async (key, fallbackValue) => (values.has(key) ? values.get(key) : fallbackValue)),
|
||||
setValue: jest.fn(async (key, value) => {
|
||||
if (key === stateKey("shared-video") && value === true) {
|
||||
trueWriteStarted();
|
||||
await trueWriteGate;
|
||||
}
|
||||
values.set(key, value);
|
||||
}),
|
||||
deleteValue: jest.fn(async (key) => {
|
||||
values.delete(key);
|
||||
}),
|
||||
};
|
||||
const firstStore = createGmSyntheticDislikeStore();
|
||||
const secondStore = createGmSyntheticDislikeStore();
|
||||
|
||||
const delayedTrue = firstStore.setDisliked("shared-video", true);
|
||||
await trueWriteEntered;
|
||||
await secondStore.setDisliked("shared-video", false);
|
||||
releaseTrueWrite();
|
||||
await delayedTrue;
|
||||
|
||||
await expect(firstStore.isDisliked("shared-video")).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it("treats non-true stored values as neutral", async () => {
|
||||
installModernStorage({
|
||||
[stateKey("false-video")]: false,
|
||||
[stateKey("corrupt-video")]: { disliked: true },
|
||||
});
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
|
||||
await expect(store.isDisliked("false-video")).resolves.toBe(false);
|
||||
await expect(store.isDisliked("corrupt-video")).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("propagates storage errors and recovers its local mutation queue", async () => {
|
||||
global.GM = { getValue: jest.fn(async () => Promise.reject(new Error("read failed"))) };
|
||||
await expect(createGmSyntheticDislikeStore().isDisliked("video")).rejects.toThrow("read failed");
|
||||
|
||||
const values = installModernStorage();
|
||||
global.GM.setValue.mockRejectedValueOnce(new Error("write failed"));
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
await expect(store.setDisliked("video", true)).rejects.toThrow("write failed");
|
||||
await expect(store.setDisliked("video", true)).resolves.toBeUndefined();
|
||||
expect(values.get(stateKey("video"))).toBe(true);
|
||||
|
||||
delete global.GM;
|
||||
await expect(createGmSyntheticDislikeStore().isDisliked("video")).rejects.toThrow(
|
||||
"Userscript storage API is unavailable",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects invalid arguments without accessing storage", async () => {
|
||||
installModernStorage();
|
||||
const store = createGmSyntheticDislikeStore();
|
||||
|
||||
await expect(store.isDisliked(123)).rejects.toThrow(TypeError);
|
||||
await expect(store.setDisliked("", true)).rejects.toThrow(TypeError);
|
||||
await expect(store.setDisliked("video", 1)).rejects.toThrow(TypeError);
|
||||
expect(global.GM.getValue).not.toHaveBeenCalled();
|
||||
expect(global.GM.setValue).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user