added discord functionality to extension and polished some of downloader process.
This commit is contained in:
@@ -2,16 +2,75 @@
|
||||
* Background script - handles cookie operations and API calls
|
||||
*/
|
||||
|
||||
// Store for captured Discord token
|
||||
let discordToken = null;
|
||||
let discordTokenCapturedAt = null;
|
||||
|
||||
// Initialize API on startup
|
||||
browser.runtime.onInstalled.addListener(async () => {
|
||||
console.log('GallerySubscriber extension installed');
|
||||
await api.init();
|
||||
await loadDiscordToken();
|
||||
});
|
||||
|
||||
browser.runtime.onStartup.addListener(async () => {
|
||||
await api.init();
|
||||
await loadDiscordToken();
|
||||
});
|
||||
|
||||
/**
|
||||
* Load Discord token from storage
|
||||
*/
|
||||
async function loadDiscordToken() {
|
||||
const stored = await browser.storage.local.get(['discordToken', 'discordTokenCapturedAt']);
|
||||
if (stored.discordToken) {
|
||||
discordToken = stored.discordToken;
|
||||
discordTokenCapturedAt = stored.discordTokenCapturedAt;
|
||||
console.log('Loaded Discord token from storage (captured:', discordTokenCapturedAt, ')');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Discord token to storage
|
||||
*/
|
||||
async function saveDiscordToken(token) {
|
||||
discordToken = token;
|
||||
discordTokenCapturedAt = new Date().toISOString();
|
||||
await browser.storage.local.set({
|
||||
discordToken: token,
|
||||
discordTokenCapturedAt: discordTokenCapturedAt
|
||||
});
|
||||
console.log('Discord token captured and saved');
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen for Discord API requests to capture Authorization header
|
||||
*/
|
||||
browser.webRequest.onSendHeaders.addListener(
|
||||
(details) => {
|
||||
// Look for Authorization header
|
||||
const authHeader = details.requestHeaders.find(
|
||||
h => h.name.toLowerCase() === 'authorization'
|
||||
);
|
||||
|
||||
if (authHeader && authHeader.value) {
|
||||
// Discord user tokens don't have "Bearer " prefix
|
||||
// Bot tokens have "Bot " prefix - we want user tokens
|
||||
const token = authHeader.value;
|
||||
|
||||
// Only capture if it looks like a user token (not a bot token)
|
||||
if (!token.startsWith('Bot ') && token.length > 50) {
|
||||
// Only save if it's different from what we have
|
||||
if (token !== discordToken) {
|
||||
saveDiscordToken(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{ urls: ['*://discord.com/api/*', '*://*.discord.com/api/*'] },
|
||||
['requestHeaders']
|
||||
);
|
||||
|
||||
// Handle messages from popup and options pages
|
||||
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
handleMessage(message)
|
||||
@@ -70,8 +129,27 @@ async function exportCookies(platformKey) {
|
||||
let credentialType;
|
||||
|
||||
if (platform.authType === 'token') {
|
||||
// Discord and other token-based platforms require manual entry
|
||||
throw new Error(`${platform.name} requires manual token entry in the web UI. Token extraction is not supported for security reasons.`);
|
||||
// Discord - use captured token from webRequest listener
|
||||
if (platformKey === 'discord') {
|
||||
if (!discordToken) {
|
||||
throw new Error(`No Discord token captured. Please open Discord in this browser and interact with it (e.g., switch channels) to capture the token.`);
|
||||
}
|
||||
credentialData = discordToken;
|
||||
credentialType = 'token';
|
||||
|
||||
// Send to backend
|
||||
const result = await api.uploadCredentials(platformKey, credentialType, credentialData);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
platform: platformKey,
|
||||
tokenCapturedAt: discordTokenCapturedAt,
|
||||
message: result.message || 'Token exported successfully'
|
||||
};
|
||||
}
|
||||
|
||||
// Other token-based platforms require manual entry
|
||||
throw new Error(`${platform.name} requires manual token entry in the web UI.`);
|
||||
}
|
||||
|
||||
// Cookie-based auth
|
||||
@@ -108,8 +186,8 @@ async function exportAllCookies() {
|
||||
for (const platformKey of platforms) {
|
||||
const platform = PLATFORMS[platformKey];
|
||||
|
||||
// Skip token-based platforms
|
||||
if (platform.authType === 'token') {
|
||||
// Skip token-based platforms (except Discord if we have a token)
|
||||
if (platform.authType === 'token' && platformKey !== 'discord') {
|
||||
results[platformKey] = {
|
||||
success: false,
|
||||
platform: platformKey,
|
||||
@@ -119,6 +197,17 @@ async function exportAllCookies() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip Discord if no token captured
|
||||
if (platformKey === 'discord' && !discordToken) {
|
||||
results[platformKey] = {
|
||||
success: false,
|
||||
platform: platformKey,
|
||||
skipped: true,
|
||||
message: 'No token captured - open Discord in browser first'
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
results[platformKey] = await exportCookies(platformKey);
|
||||
} catch (error) {
|
||||
@@ -144,13 +233,27 @@ async function getPlatformStatus() {
|
||||
try {
|
||||
if (platform.authType === 'token') {
|
||||
// Token-based platforms
|
||||
status[key] = {
|
||||
name: platform.name,
|
||||
authType: 'token',
|
||||
hasCookies: false,
|
||||
cookieCount: 0,
|
||||
note: platform.note || 'Requires manual token entry'
|
||||
};
|
||||
if (key === 'discord') {
|
||||
status[key] = {
|
||||
name: platform.name,
|
||||
authType: 'token',
|
||||
hasToken: !!discordToken,
|
||||
tokenCapturedAt: discordTokenCapturedAt,
|
||||
hasCookies: false,
|
||||
cookieCount: 0,
|
||||
note: discordToken
|
||||
? `Token captured ${new Date(discordTokenCapturedAt).toLocaleString()}`
|
||||
: 'Open Discord in browser to capture token'
|
||||
};
|
||||
} else {
|
||||
status[key] = {
|
||||
name: platform.name,
|
||||
authType: 'token',
|
||||
hasCookies: false,
|
||||
cookieCount: 0,
|
||||
note: platform.note || 'Requires manual token entry'
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// Cookie-based platforms
|
||||
const cookies = await extractCookiesForPlatform(key);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"storage",
|
||||
"tabs",
|
||||
"activeTab",
|
||||
"webRequest",
|
||||
"*://*.patreon.com/*",
|
||||
"*://*.subscribestar.com/*",
|
||||
"*://*.subscribestar.adult/*",
|
||||
|
||||
@@ -86,13 +86,17 @@ function createPlatformCard(key, platform, status) {
|
||||
card.className = 'platform-card';
|
||||
card.dataset.platform = key;
|
||||
|
||||
const isDisabled = platform.authType === 'token';
|
||||
if (isDisabled) {
|
||||
// Discord is clickable if we have a token captured
|
||||
// Other token-based platforms are disabled
|
||||
const isDisabled = platform.authType === 'token' && key !== 'discord';
|
||||
const isDiscordWithoutToken = key === 'discord' && !status.hasToken;
|
||||
|
||||
if (isDisabled || isDiscordWithoutToken) {
|
||||
card.classList.add('disabled');
|
||||
}
|
||||
|
||||
const statusText = getStatusText(status, platform);
|
||||
const statusClass = getStatusClass(status, platform);
|
||||
const statusText = getStatusText(status, platform, key);
|
||||
const statusClass = getStatusClass(status, platform, key);
|
||||
|
||||
card.innerHTML = `
|
||||
<div class="platform-icon" style="background-color: ${platform.color}">
|
||||
@@ -109,7 +113,7 @@ function createPlatformCard(key, platform, status) {
|
||||
</span>
|
||||
`;
|
||||
|
||||
if (!isDisabled) {
|
||||
if (!isDisabled && !isDiscordWithoutToken) {
|
||||
card.addEventListener('click', () => exportPlatformCookies(key, card));
|
||||
}
|
||||
|
||||
@@ -120,9 +124,18 @@ function createPlatformCard(key, platform, status) {
|
||||
* Get status text for display
|
||||
* @param {object} status - Platform status
|
||||
* @param {object} platform - Platform definition
|
||||
* @param {string} key - Platform key
|
||||
* @returns {string}
|
||||
*/
|
||||
function getStatusText(status, platform) {
|
||||
function getStatusText(status, platform, key) {
|
||||
// Special handling for Discord token-based auth
|
||||
if (key === 'discord') {
|
||||
if (status.hasToken) {
|
||||
return 'Token captured - ready to export';
|
||||
}
|
||||
return 'Open Discord in browser to capture token';
|
||||
}
|
||||
|
||||
if (platform.authType === 'token') {
|
||||
return 'Manual token entry required';
|
||||
}
|
||||
@@ -139,9 +152,15 @@ function getStatusText(status, platform) {
|
||||
* Get status CSS class
|
||||
* @param {object} status - Platform status
|
||||
* @param {object} platform - Platform definition
|
||||
* @param {string} key - Platform key
|
||||
* @returns {string}
|
||||
*/
|
||||
function getStatusClass(status, platform) {
|
||||
function getStatusClass(status, platform, key) {
|
||||
// Special handling for Discord
|
||||
if (key === 'discord') {
|
||||
return status.hasToken ? 'ready' : 'no-cookies';
|
||||
}
|
||||
|
||||
if (platform.authType === 'token') {
|
||||
return 'no-cookies';
|
||||
}
|
||||
@@ -172,7 +191,11 @@ async function exportPlatformCookies(platformKey, cardElement) {
|
||||
if (result.error) {
|
||||
showError(result.error);
|
||||
} else {
|
||||
showSuccess(`${PLATFORMS[platformKey].name}: ${result.cookieCount} cookies exported!`);
|
||||
// Handle different response types (cookies vs token)
|
||||
const successMsg = result.cookieCount !== undefined
|
||||
? `${PLATFORMS[platformKey].name}: ${result.cookieCount} cookies exported!`
|
||||
: `${PLATFORMS[platformKey].name}: Token exported successfully!`;
|
||||
showSuccess(successMsg);
|
||||
// Refresh status
|
||||
await loadPlatformStatus();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user