Skip to content

Commit f0cb176

Browse files
committed
build: add typecheck step
1 parent 7cfb9df commit f0cb176

File tree

10 files changed

+50
-33
lines changed

10 files changed

+50
-33
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "3.1.1",
44
"private": true,
55
"scripts": {
6-
"build": "npm-run-all --parallel build:chrome build:firefox",
6+
"typecheck": "tsc --noEmit",
7+
"build": "npm run typecheck && npm-run-all --parallel build:chrome build:firefox",
78
"build:chrome": "cross-env BROWSER=chrome vite build",
89
"build:firefox": "cross-env BROWSER=firefox vite build",
910
"dev:chrome": "cross-env MINIFY=false BROWSER=chrome vite build --watch",

src/scripts/chat-background.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
2727

2828
chrome.runtime.onConnect.addListener(hc => {
2929
// frameId and tabId should be int
30-
const { frameId, tabId } = JSON.parse(hc.name);
30+
const { frameId, tabId } = JSON.parse(hc.name) as { frameId: number, tabId: number };
3131
const interceptorPort = chrome.tabs.connect(tabId, { frameId });
3232
interceptorPort.onMessage.addListener(msg => {
3333
hc.postMessage(msg);
@@ -38,7 +38,9 @@ chrome.runtime.onConnect.addListener(hc => {
3838
});
3939

4040
// see https://i.imgur.com/cGciqrX.png
41-
chrome.storage.local.onChanged.addListener(changes => {
41+
chrome.storage.onChanged.addListener((changes, areaName) => {
42+
if (areaName !== 'local') return;
43+
4244
let delta = 0;
4345
for (const key of Object.keys(changes)) {
4446
if (noUpdateKeys.has(key)) continue;

src/scripts/chat-metagetter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
window.dispatchEvent(new CustomEvent('fetchMeta', {
22
detail: JSON.stringify(window.ytcfg)
33
}));
4+
5+
export {};

src/ts/chat-actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { writable } from 'svelte/store';
22
import { ChatReportUserOptions, ChatUserActions } from './chat-constants';
33
import { reportDialog } from './storage';
4+
import type { Chat } from './typings/chat';
45

56
export function useBanHammer(
67
message: Ytc.ParsedMessage,

src/ts/chat-parser.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const parseMessageRuns = (runs?: Ytc.MessageRun[]): Ytc.ParsedRun[] => {
6666
// takes an array of runs, finds newline-only runs, and splits the array by them, up to maxSplit times
6767
// final output will have maximum length of maxSplit + 1
6868
// maxSplit = -1 will have no limit for splits
69-
const splitRunsByNewline = (runs: Ytc.ParsedRun[], maxSplit: number = -1): Ytc.ParsedRun[][] =>
69+
const splitRunsByNewline = (runs: Ytc.ParsedRun[], maxSplit: number = -1): Ytc.ParsedRun[][] =>
7070
runs.reduce((acc: Ytc.ParsedRun[][], run: Ytc.ParsedRun) => {
7171
if (run.type === 'text' && run.text === '\n' && (maxSplit == -1 || acc.length <= maxSplit)) {
7272
acc.push([]);
@@ -80,7 +80,7 @@ const parseChatSummary = (renderer: Ytc.AddChatItem, actionId: string, showtime:
8080
if (!renderer.liveChatBannerChatSummaryRenderer) {
8181
return;
8282
}
83-
const baseRenderer = renderer.liveChatBannerChatSummaryRenderer!;
83+
const baseRenderer = renderer.liveChatBannerChatSummaryRenderer;
8484
const runs = parseMessageRuns(renderer.liveChatBannerChatSummaryRenderer?.chatSummary.runs);
8585
const splitRuns = splitRunsByNewline(runs, 2);
8686
if (splitRuns.length < 3) {
@@ -100,44 +100,44 @@ const parseChatSummary = (renderer: Ytc.AddChatItem, actionId: string, showtime:
100100
actionId: baseRenderer.liveChatSummaryId,
101101
item: {
102102
header: splitRuns[0],
103-
subheader: subheader,
104-
message: splitRuns[2],
103+
subheader,
104+
message: splitRuns[2]
105105
},
106-
showtime: showtime,
107-
timestamp: formatTimestamp(Date.now() * 1000),
106+
showtime,
107+
timestamp: formatTimestamp(Date.now() * 1000)
108108
};
109109
return item;
110-
}
110+
};
111111

112112
const parseRedirectBanner = (renderer: Ytc.AddChatItem, actionId: string, showtime: number): Ytc.ParsedRedirect | undefined => {
113113
if (!renderer.liveChatBannerRedirectRenderer) {
114114
return;
115115
}
116-
const baseRenderer = renderer.liveChatBannerRedirectRenderer!;
116+
const baseRenderer = renderer.liveChatBannerRedirectRenderer;
117117
const profileIcon = {
118118
src: fixUrl(baseRenderer.authorPhoto?.thumbnails[0].url ?? ''),
119119
alt: 'Redirect profile icon'
120120
};
121-
const url = baseRenderer.inlineActionButton?.buttonRenderer.command.urlEndpoint?.url ||
122-
(baseRenderer.inlineActionButton?.buttonRenderer.command.watchEndpoint?.videoId ?
123-
"/watch?v=" + baseRenderer.inlineActionButton?.buttonRenderer.command.watchEndpoint?.videoId
124-
: '');
121+
const url = baseRenderer.inlineActionButton?.buttonRenderer.command.urlEndpoint?.url ||
122+
(baseRenderer.inlineActionButton?.buttonRenderer.command.watchEndpoint?.videoId
123+
? '/watch?v=' + baseRenderer.inlineActionButton?.buttonRenderer.command.watchEndpoint?.videoId
124+
: '');
125125
const item: Ytc.ParsedRedirect = {
126126
type: 'redirect',
127-
actionId: actionId,
127+
actionId,
128128
item: {
129129
message: parseMessageRuns(baseRenderer.bannerMessage.runs),
130-
profileIcon: profileIcon,
130+
profileIcon,
131131
action: {
132132
url: fixUrl(url),
133-
text: parseMessageRuns(baseRenderer.inlineActionButton?.buttonRenderer.text?.runs),
133+
text: parseMessageRuns(baseRenderer.inlineActionButton?.buttonRenderer.text?.runs)
134134
}
135135
},
136-
showtime: showtime,
137-
timestamp: formatTimestamp(Date.now() * 1000),
136+
showtime,
137+
timestamp: formatTimestamp(Date.now() * 1000)
138138
};
139139
return item;
140-
}
140+
};
141141

142142
const parseAddChatItemAction = (action: Ytc.AddChatItemAction, isReplay = false, liveTimeoutOrReplayMs = 0): Ytc.ParsedMessage | undefined => {
143143
const actionItem = action.item;
@@ -152,12 +152,14 @@ const parseAddChatItemAction = (action: Ytc.AddChatItemAction, isReplay = false,
152152
}
153153

154154
const isGiftPurchase = isMembershipGiftPurchaseRenderer(renderer);
155-
const messageRenderer = isGiftPurchase ? renderer.header.liveChatSponsorshipsHeaderRenderer : renderer;
155+
const messageRenderer: Ytc.TextMessageRenderer = isGiftPurchase
156+
? (renderer as Ytc.MembershipGiftPurchaseRenderer).header.liveChatSponsorshipsHeaderRenderer
157+
: renderer as Ytc.TextMessageRenderer;
156158

157159
const authorTypes: string[] = [];
158160
let customBadge: Ytc.ParsedImage | undefined;
159161
if (messageRenderer.authorBadges) {
160-
messageRenderer.authorBadges.forEach((badge) => {
162+
messageRenderer.authorBadges.forEach((badge: Ytc.AuthorBadge) => {
161163
const badgeRenderer = badge.liveChatAuthorBadgeRenderer;
162164
const iconType = badgeRenderer.icon?.iconType;
163165
if (iconType != null) {
@@ -233,7 +235,7 @@ const parseAddChatItemAction = (action: Ytc.AddChatItemAction, isReplay = false,
233235
: parseMessageRuns(renderer.headerSubtext.runs)
234236
};
235237
} else if (isGiftPurchase) {
236-
const header = renderer.header.liveChatSponsorshipsHeaderRenderer;
238+
const header = (renderer as Ytc.MembershipGiftPurchaseRenderer).header.liveChatSponsorshipsHeaderRenderer;
237239
item.membershipGiftPurchase = {
238240
headerPrimaryText: parseMessageRuns(header.primaryText.runs),
239241
image: {
@@ -274,7 +276,7 @@ const parsePollRenderer = (baseRenderer: Ytc.PollRenderer): Ytc.ParsedPoll | und
274276
type: 'poll',
275277
actionId: baseRenderer.liveChatPollId,
276278
item: {
277-
profileIcon: profileIcon,
279+
profileIcon,
278280
header: parseMessageRuns(baseRenderer.header.pollHeaderRenderer.metadataText.runs),
279281
question: parseMessageRuns(baseRenderer.header.pollHeaderRenderer.pollQuestion.runs),
280282
choices: baseRenderer.choices.map((choice) => {
@@ -284,10 +286,10 @@ const parsePollRenderer = (baseRenderer: Ytc.PollRenderer): Ytc.ParsedPoll | und
284286
ratio: choice.voteRatio,
285287
percentage: choice.votePercentage?.simpleText
286288
};
287-
}),
289+
})
288290
}
289291
};
290-
}
292+
};
291293

292294
const parseBannerAction = (action: Ytc.AddPinnedAction): Ytc.ParsedMisc | undefined => {
293295
const baseRenderer = action.bannerRenderer.liveChatBannerRenderer;
@@ -302,8 +304,8 @@ const parseBannerAction = (action: Ytc.AddPinnedAction): Ytc.ParsedMisc | undefi
302304

303305
// fold both auto-disappear and auto-collapse into just collapse for showtime
304306
const showtime = action.bannerProperties?.isEphemeral
305-
? (action.bannerProperties?.bannerTimeoutMs || 0)
306-
: 1000 * (action.bannerProperties?.autoCollapseDelay?.seconds || baseRenderer.bannerProperties?.autoCollapseDelay?.seconds || 0);
307+
? (action.bannerProperties?.bannerTimeoutMs || 0)
308+
: 1000 * (action.bannerProperties?.autoCollapseDelay?.seconds || baseRenderer.bannerProperties?.autoCollapseDelay?.seconds || 0);
307309

308310
if (baseRenderer.contents.liveChatBannerChatSummaryRenderer) {
309311
return parseChatSummary(baseRenderer.contents, actionId, showtime);
@@ -319,14 +321,14 @@ const parseBannerAction = (action: Ytc.AddPinnedAction): Ytc.ParsedMisc | undefi
319321
}
320322
return {
321323
type: 'pin',
322-
actionId: actionId,
324+
actionId,
323325
item: {
324326
header: parseMessageRuns(
325327
baseRenderer.header.liveChatBannerHeaderRenderer.text.runs
326328
),
327329
contents: parsedContents
328330
},
329-
showtime: showtime,
331+
showtime
330332
};
331333
};
332334

@@ -361,7 +363,7 @@ const processCommonAction = (
361363
} else if (action.removeBannerForLiveChatCommand) {
362364
return {
363365
type: 'unpin',
364-
targetActionId: action.removeBannerForLiveChatCommand.targetActionId,
366+
targetActionId: action.removeBannerForLiveChatCommand.targetActionId
365367
} as Ytc.ParsedRemoveBanner;
366368
} else if (action.addLiveChatTickerItemAction) {
367369
return parseTickerAction(action.addLiveChatTickerItemAction, isReplay, liveTimeoutOrReplayMs);

src/ts/messaging.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Unsubscriber } from './queue';
22
import { ytcQueue } from './queue';
33
import sha1 from 'sha-1';
44
import { chatReportUserOptions, ChatUserActions, ChatReportUserOptions } from '../ts/chat-constants';
5+
import type { Chat } from './typings/chat';
56

67
const currentDomain = location.protocol.includes('youtube') ? (location.protocol + '//' + location.host) : 'https://www.youtube.com';
78

src/ts/queue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseChatResponse } from './chat-parser';
2+
import type { Chat } from './typings/chat';
23

34
interface QueueItem<T> { data: T, next?: QueueItem<T> }
45
export interface Queue<T> {

src/ts/storage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Writable } from 'svelte/store';
44
import { getClient, AvailableLanguages } from 'iframe-translator';
55
import type { IframeTranslatorClient, AvailableLanguageCodes } from 'iframe-translator';
66
import { ChatReportUserOptions, Theme, YoutubeEmojiRenderMode } from './chat-constants';
7+
import type { Chat } from './typings/chat';
78

89
export const stores = webExtStores();
910

src/ts/typings/chat.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare namespace Chat {
148148
type BackgroundMessage =
149149
RegisterInterceptorMsg | RegisterClientMsg | processJsonMsg |
150150
setInitialDataMsg | updatePlayerProgressMsg | setThemeMsg | getThemeMsg |
151-
RegisterYtcInterceptorMsg | sendLtlMessageMsg | executeChatActionMsg | chatUserActionResponse;
151+
RegisterYtcInterceptorMsg | sendLtlMessageMsg | executeChatActionMsg | chatUserActionResponse | Ping;
152152

153153
type Port = Omit<chrome.runtime.Port, 'postMessage' | 'onMessage'> & {
154154
postMessage: (message: BackgroundMessage | BackgroundResponse) => void;

src/ts/typings/vite-env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22
declare const __BROWSER__: 'chrome' | 'firefox';
33
declare const __VERSION__: string;
4+
5+
// Vite CSS imports with ?inline suffix
6+
declare module '*?inline' {
7+
const content: string;
8+
export default content;
9+
}

0 commit comments

Comments
 (0)