Skip to content

Commit

Permalink
Add way to copy debug logs from popup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Sep 8, 2024
1 parent 2a28967 commit 12e8c7a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
2 changes: 2 additions & 0 deletions public/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ <h1 class="sbHeader" style="padding: 8px 15px;">
<a href="https://discord.gg/SponsorBlock" target="_blank" rel="noopener">Discord</a>
<a href="https://matrix.to/#/#sponsor:ajay.app?via=ajay.app&via=matrix.org&via=mozilla.org" target="_blank" rel="noopener">Matrix</a>
<a href="https://sponsor.ajay.app/donate" target="_blank" rel="noopener" id="sbDonate">__MSG_Donate__</a>
<br />
<a id="debugLogs">__MSG_copyDebugLogs__</a>
</footer>

<button id="showNoticeAgain" style="display: none">__MSG_showNotice__</button>
Expand Down
24 changes: 23 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,26 @@ const localDefaults = {
};

const Config = new ConfigClass(syncDefaults, localDefaults, migrateOldSyncFormats);
export default Config;
export default Config;

export function generateDebugDetails(): string {
// Build output debug information object
const output = {
debug: {
userAgent: navigator.userAgent,
platform: navigator.platform,
language: navigator.language,
extensionVersion: chrome.runtime.getManifest().version
},
config: JSON.parse(JSON.stringify(Config.cachedSyncConfig)) // Deep clone config object
};

// Sanitise sensitive user config values
delete output.config.userID;
output.config.serverAddress = (output.config.serverAddress === CompileConfig.serverAddress)
? "Default server address" : "Custom server address";
output.config.invidiousInstances = output.config.invidiousInstances.length;
output.config.whitelistedChannels = output.config.whitelistedChannels.length;

return JSON.stringify(output, null, 4);
}
6 changes: 6 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
metaKey: request.metaKey
}));
break;
case "getLogs":
sendResponse({
debug: window["SBLogs"].debug,
warn: window["SBLogs"].warn
});
break;
}

sendResponse({});
Expand Down
11 changes: 9 additions & 2 deletions src/messageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ interface DefaultMessage {
| "isChannelWhitelisted"
| "submitTimes"
| "refreshSegments"
| "closePopup";
| "closePopup"
| "getLogs";
}

interface BoolValueMessage {
Expand Down Expand Up @@ -104,7 +105,8 @@ export type MessageResponse =
| Record<string, never> // empty object response {}
| VoteResponse
| ImportSegmentsResponse
| RefreshSegmentsResponse;
| RefreshSegmentsResponse
| LogResponse;

export interface VoteResponse {
successType: number;
Expand All @@ -120,6 +122,11 @@ export interface RefreshSegmentsResponse {
hasVideo: boolean;
}

export interface LogResponse {
debug: string[];
warn: string[];
}

export interface TimeUpdateMessage {
message: "time";
time: number;
Expand Down
30 changes: 6 additions & 24 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import { createRoot } from 'react-dom/client';

import Config from "./config";
import Config, { generateDebugDetails } from "./config";
import * as CompileConfig from "../config.json";

Check failure on line 5 in src/options.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'CompileConfig' is defined but never used
import * as invidiousList from "../ci/invidiouslist.json";

Expand Down Expand Up @@ -698,32 +698,14 @@ function validateServerAddress(input: string): string {
}

function copyDebugOutputToClipboard() {
// Build output debug information object
const output = {
debug: {
userAgent: navigator.userAgent,
platform: navigator.platform,
language: navigator.language,
extensionVersion: chrome.runtime.getManifest().version
},
config: JSON.parse(JSON.stringify(Config.cachedSyncConfig)) // Deep clone config object
};

// Sanitise sensitive user config values
delete output.config.userID;
output.config.serverAddress = (output.config.serverAddress === CompileConfig.serverAddress)
? "Default server address" : "Custom server address";
output.config.invidiousInstances = output.config.invidiousInstances.length;
output.config.whitelistedChannels = output.config.whitelistedChannels.length;

// Copy object to clipboard
navigator.clipboard.writeText(JSON.stringify(output, null, 4))
.then(() => {
navigator.clipboard.writeText(generateDebugDetails())
.then(() => {
alert(chrome.i18n.getMessage("copyDebugInformationComplete"));
})
.catch(() => {
})
.catch(() => {
alert(chrome.i18n.getMessage("copyDebugInformationFailed"));
});
});
}

function isIncognitoAllowed(): Promise<boolean> {
Expand Down
13 changes: 11 additions & 2 deletions src/popup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Config from "./config";
import Config, { generateDebugDetails } from "./config";

import Utils from "./utils";
import {
Expand All @@ -12,6 +12,7 @@ import {
GetChannelIDResponse,
IsChannelWhitelistedResponse,
IsInfoFoundMessageResponse,
LogResponse,
Message,
MessageResponse,
PopupMessage,
Expand Down Expand Up @@ -184,7 +185,8 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
"exportSegmentsButton",
"importSegmentsMenu",
"importSegmentsText",
"importSegmentsSubmit"
"importSegmentsSubmit",
"debugLogs"

].forEach(id => PageElements[id] = document.getElementById(id));

Expand Down Expand Up @@ -255,6 +257,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.helpButton.addEventListener("click", openHelp);
PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments);
PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await getHash(Config.config.userID)));
PageElements.debugLogs.addEventListener("click", copyDebgLogs);

// Forward click events
if (window !== window.top) {
Expand Down Expand Up @@ -1143,6 +1146,12 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
}
}

function copyDebgLogs() {
sendTabMessage({ message: "getLogs" }, (logs: LogResponse) => {
copyToClipboard(`${generateDebugDetails()}\n\nWarn:\n${logs.warn.join("\n")}\n\nDebug:\n${logs.debug.join("\n")}`);
});
}

function onMessage(msg: PopupMessage) {
switch (msg.message) {
case "time":
Expand Down

0 comments on commit 12e8c7a

Please sign in to comment.