Skip to content

Commit

Permalink
feature: Enable to store data without any series config
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-freling committed Jul 6, 2024
1 parent e88d871 commit 005b998
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 180 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@
setup:
# Install act for GitHub action local runner: https://nektosact.com/installation/index.html
curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash

.PHONY: docs
docs:
@echo "## Supported Anime" > docs/crunchyroll.md
@jq -r '.services.crunchyroll.series | to_entries[] | "- \(.value.title)"' assets/configs/default_config.json >> docs/crunchyroll.md
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> [!WARNING]
> This is still under development and it's still experimental phase.
This is a brwwser extension to block spoilers for anime in some services.
This is a browser extension to block spoilers for anime in some services.
There are a few features supported to block spoilers

1. Keep the history of the last episode of a season for each anime you watched
Expand All @@ -14,6 +14,4 @@ There are a few features supported to block spoilers
## Supported services

Currently, spoilers are blocked on YouTube only.
You can also see which anime is currently supported on this extension in

- [Crunchyroll](./docs/crunchyroll.md)
Currently, [Crunchyroll](https://www.crunchyroll.com/) is the only supported service to store a watch history on your chrome browser.
32 changes: 7 additions & 25 deletions assets/configs/default_config.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
{
"series": {
"demon_slayer": {
"series": [
{
"title": "Demon Slayer",
"keywords": ["Demon Slayer"]
"keywords": ["Demon Slayer", "Kimetsu no Yaiba"]
},
"jujutsu_kaisen": {
{
"title": "Jujutsu Kaisen",
"keywords": ["Jujutsu Kaisen"]
},
"my_hero_academia": {
{
"title": "My Hero Academia",
"keywords": ["My Hero Academia"]
},
"mushoku_tensei": {
{
"title": "Mushoku Tensei",
"keywords": ["Mushoku Tensei", "Jobless Reincarnation"]
}
},
"services": {
"crunchyroll": {
"series": {
"demon_slayer": {
"title": "Demon Slayer: Kimetsu no Yaiba"
},
"jujutsu_kaisen": {
"title": "Jujutsu Kaisen"
},
"my_hero_academia": {
"title": "My Hero Academia"
},
"mushoku_tensei": {
"title": "Mushoku Tensei: Jobless Reincarnation"
}
}
}
}
]
}
15 changes: 9 additions & 6 deletions assets/configs/user_history_example.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
{
"series": {
"demon_slayer": {
"series": [
{
"title": "Demon Slayer",
"tv": {
"season": 1,
"episode": 1
}
},
"jujutsu_kaisen": {
{
"title": "Jujutsu Kaisen",
"tv": {
"season": 1,
"episode": 1
}
},
"mushoku_tensei": {
{
"title": "Mushoku Tensei",
"tv": {
"season": 2,
"season": 1,
"episode": 1
}
}
}
]
}
5 changes: 0 additions & 5 deletions docs/crunchyroll.md

This file was deleted.

6 changes: 3 additions & 3 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Config, UserHistory } from "../blocker";
import type { StorageAnimeConfig, StorageUserHistory } from "~blocker/storage";
import { Storage } from "@plasmohq/storage";

const storage = new Storage();
Expand Down Expand Up @@ -27,8 +27,8 @@ async function onInstall() {
return await response.json();
})
);
const config: Config = responses[0];
const userHistory: UserHistory = responses[1];
const config: StorageAnimeConfig = responses[0];
const userHistory: StorageUserHistory = responses[1];

storage.set("config", config);
storage.set("userHistory", userHistory);
Expand Down
9 changes: 5 additions & 4 deletions src/background/messages/getConfig.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import type { Config, UserHistory } from "~blocker";
import type { StorageAnimeConfig, StorageUserHistory } from "~blocker/storage";
import { Storage } from "@plasmohq/storage";

const storage = new Storage();

const handler: PlasmoMessaging.MessageHandler<
{},
{
config: Config;
userHistory: UserHistory;
config: StorageAnimeConfig;
userHistory: StorageUserHistory;
}
> = async (req, res) => {
Promise.all([storage.get("config"), storage.get("userHistory")]).then(
(result) => {
const [config, userHistory]: [Config, UserHistory] = result as any;
const [config, userHistory]: [StorageAnimeConfig, StorageUserHistory] =
result as any;
res.send({
config,
userHistory,
Expand Down
59 changes: 13 additions & 46 deletions src/background/messages/updateWatchHistory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import type { Config, UserHistory } from "~blocker";
import type { StorageAnimeConfig, StorageUserHistory } from "~blocker/storage";
import { Storage } from "@plasmohq/storage";
import { UserHistoryManager } from "~blocker/storage";

const storage = new Storage();

export interface UpdateWatchHistoryRequest {
webServiceName: string;
mediaType: string;
series: string;

// The title might be multiple choices. Enable to pass one of them to match a history
titles: string[];
season: number;
episode: number;
}
Expand All @@ -23,53 +26,17 @@ const handler: PlasmoMessaging.MessageHandler<

Promise.all([storage.get("config"), storage.get("userHistory")]).then(
async (promises) => {
const [config, userHistory]: [Config, UserHistory] = promises as any;
if (config.services[message.webServiceName] == null) {
return;
}

let result;
Object.entries(
(config as Config).services[message.webServiceName].series
).forEach(([seriesID, thisSeries]) => {
if (
thisSeries.title.toLocaleLowerCase() ==
message.series.toLocaleLowerCase()
) {
result = seriesID;
return;
}
});

if (result == null) {
const [config, userHistory]: [StorageAnimeConfig, StorageUserHistory] =
promises as any;
const userHistoryManager = new UserHistoryManager(config, userHistory);
const updatedConfigs = userHistoryManager.updateWatchHistory(message);
if (updatedConfigs == null || updatedConfigs.userHistory == null) {
return;
}
if (userHistory.series[result] == null) {
userHistory.series[result] = {
tv: {
season: 0,
episode: 0,
},
};
if (updatedConfigs.config != null) {
await storage.set("config", updatedConfigs.config);
}
if (message.season < userHistory.series[result].tv.season) {
return;
}
if (
message.season == userHistory.series[result].tv.season &&
message.episode <= userHistory.series[result].tv.episode
) {
return;
}

userHistory.series[result].tv.season = message.season;
userHistory.series[result].tv.episode = message.episode;
await storage.set("userHistory", userHistory);
console.info("Updated a watch history", {
series: message.series,
season: userHistory.series[result].tv.season,
episode: userHistory.series[result].tv.episode,
});
await storage.set("userHistory", updatedConfigs.userHistory);
}
);
};
Expand Down
37 changes: 19 additions & 18 deletions src/blocker/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,60 +75,61 @@ describe("TextParser", () => {
};
const textParser = new TextSpoilerAnalyzer(
{
series: {
test: {
series: [
{
keywords,
title: "",
title: "test",
},
},
services: {},
],
},
{
series: {
test: {
series: [
{
title: "test",
tv: {
season: 0,
episode: 0,
},
},
},
],
}
);
const actual = textParser.extractEpisodeFromText(text, "Test", keywords);
expect(actual).toEqual(expected);
});
});

describe("extractSpoilerEpisode", () => {
describe("extractSpoiler", () => {
const defaultTestCase = {
config: {
series: {
test1: {
series: [
{
title: "Test",
keywords: ["test1", "keyword1"],
},
test2: {
{
title: "Test2",
keywords: ["test2"],
},
},
services: {},
],
},
userHistory: {
series: {
test1: {
series: [
{
title: "Test",
tv: {
season: 1,
episode: 2,
},
},
test2: {
{
title: "Test2",
tv: {
season: 2,
episode: 1,
},
},
},
],
},
expected: {
title: "Test",
Expand Down
Loading

0 comments on commit 005b998

Please sign in to comment.