Skip to content

Commit

Permalink
Re:turn (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sopisoft authored Sep 27, 2024
1 parent 8734f52 commit a7b8217
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 272 deletions.
136 changes: 69 additions & 67 deletions src/@types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,97 +38,99 @@ type SearchErrorResponse = {
type SearchResult = {
meta: {
status: number;
totalCount: number;
id: string;
code: string; // "HTTP_200"
};
data: {
ads: null;
category: null;
channel: {
id: string;
name: string;
isOfficialAnime: boolean;
isDisplayAdBanner: boolean;
thumbnail: {
url: string;
smallUrl: string;
};
viewer: {
follow: {
isFollowed: boolean;
isBookmarked: boolean;
token: string;
tokenTimestamp: number;
response: {
channel: {
id: string;
name: string;
isOfficialAnime: boolean;
isDisplayAdBanner: boolean;
thumbnail: {
url: string;
smallUrl: string;
};
viewer: {
follow: {
isFollowed: boolean;
isBookmarked: boolean;
token: string;
tokenTimestamp: number;
};
};
} | null;
client: {
nicosid: string;
watchId: string;
watchTrackId: string;
};
} | null;
client: {
nicosid: string;
watchId: string;
watchTrackId: string;
};
comment: {
server: {
url: string;
};
keys: {
userKey: string;
};
layers: [
{
comment: {
server: {
url: string;
};
keys: {
userKey: string;
};
layers: {
index: number;
isTranslucent: boolean;
threadIds: thread[];
}[],
];
threads: [
{
}[];
threads: {
id: thread["id"];
fork: thread["fork"];
forkLabel: thread["forkLabel"];
videoId: string; // contentId
isOwnerThread: boolean;
isActive: boolean;
isDefaultPostTarget: boolean;
isEasyCommentPostTarget: boolean;
isLeafRequired: boolean;
isThreadkeyRequired: boolean;
threadkey: string;
is184Forced: boolean;
hasNicoscript: boolean;
label:
| "owner"
| "default"
| "community"
| "easy"
| "extra-community"
| "extra-easy";
postKeyStatus: number;
server: string;
}[];
nvComment: {
threadKey: string;
server: string;
}[],
];
nvComment: {
threadKey: string;
server: "https://nv-comment.nicovideo.jp";
params: {
targets: [
{
params: {
targets: {
id: string; // thread["id"] to stringify
fork: thread["forkLabel"];
}[],
];
language: "ja-jp";
}[];
language: "ja-jp";
};
};
};
};
video: {
id: string; // contentId
title: string;
description: string;
count: {
view: number;
comment: number;
mylist: number;
like: number;
};
duration: number;
thumbnail: {
url: string;
middleUrl: string;
largeUrl: string;
player: string;
ogp: string;
video: {
id: string; // contentId
title: string;
description: string; // HTML
count: {
view: number;
comment: number;
mylist: number;
like: number;
};
duration: number;
thumbnail: {
url: string;
middleUrl: string;
largeUrl: string;
player: string;
ogp: string;
};
};
};
};
Expand Down
63 changes: 8 additions & 55 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,76 +15,31 @@
along with d-comments. If not, see <https://www.gnu.org/licenses/>.
*/

import { getConfig } from "@/config";
import browser from "webextension-polyfill";
import { openHowToUseIfNotRead } from "./how_to_use/how_to_use";

/**
* 任意の範囲のランダムな整数を返す
* @param min 最小値
* @param max 最大値
* @returns min 以上 max 以下のランダムな整数
*/
const getRandomInt = (min: number, max: number) => {
const minNum = Math.ceil(min);
const maxNum = Math.floor(max);
return Math.floor(Math.random() * (maxNum - minNum) + minNum);
};

function getActionTrackId() {
const f = Math.random().toString(36).slice(-10);
const b = getRandomInt(10 ** 12, 10 ** 13);
return `${f}_${b}`;
}

/**
* 動画情報を取得する
* @param videoId ニコニコ動画の動画ID
* @param sendResponse (response) => void
*/

const getVideoData = async (videoId: string) => {
const config: boolean = await getConfig("allow_login_to_nicovideo");
const url = `https://www.nicovideo.jp/api/watch/${
config ? "v3" : "v3_guest"
}/${videoId}`;
const params = {
_frontendId: "6",
_frontendVersion: "0",
actionTrackId: getActionTrackId(),
};
const fetch_options: RequestInit = {
credentials: config ? "include" : "omit",
headers: {
"x-frontend-id": "6",
"x-frontend-version": "0",
},
};
if (config)
browser.cookies
.get({ url: "https://www.nicovideo.jp/", name: "user_session" })
.then((cookie) => {
if (!cookie) return;
Object.assign(fetch_options.headers as HeadersInit, {
Cookie: `user_session=${cookie.value}`,
});
});

return await fetch(`${url}?${new URLSearchParams(params)}`, fetch_options)
async function getVideoData(videoId: string): Promise<SearchResponse | Error> {
const url = `https://www.nicovideo.jp/watch/${videoId}?responseType=json`;
const res = await fetch(url)
.then(async (res) => {
const json = (await res.json()) as SearchResponse;
return json;
const json = await res.json();
return json as SearchResponse;
})
.catch((e) => {
return e as Error;
});
};
return res;
}

/**
* コメントスレッドの情報とコメントを取得
*/
const getThreadComments = async (
nvComment: SearchResult["data"]["comment"]["nvComment"]
nvComment: SearchResult["data"]["response"]["comment"]["nvComment"]
) => {
const { server, threadKey, params } = nvComment;
const serverUrl = `${server}/v1/threads`;
Expand Down Expand Up @@ -158,9 +113,7 @@ const search = async (
/**
*
* @param type "user" | "channel"
* @param videoId 動画ID
* @param ownerId ユーザーID または チャンネルID
* @returns Owner
*/
const get_user_info = async (
type: "user" | "channel",
Expand Down
4 changes: 0 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ function get_default_configs() {
value: true as boolean,
type: "checkbox",
},
allow_login_to_nicovideo: {
value: false as boolean,
type: "switch",
},
comment_ng_words: {
value: [] as { key: string; value: string; enabled: boolean }[],
type: "text_list",
Expand Down
91 changes: 0 additions & 91 deletions src/content_scripts/export.ts

This file was deleted.

Loading

0 comments on commit a7b8217

Please sign in to comment.