From b28efc79604748f12a3a36cb35352058610da275 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jan 2025 14:37:48 +0700 Subject: [PATCH] fix cookie API url, implement continuous sync --- extension/background.js | 41 +++++++++++++++++++++++++++++++++++- extension/index.html | 13 +++++++++--- extension/popup.js | 46 +++++++++++++++++++++++++---------------- extension/style.css | 28 ++++++++++++------------- 4 files changed, 92 insertions(+), 36 deletions(-) diff --git a/extension/background.js b/extension/background.js index b004128..a925c26 100644 --- a/extension/background.js +++ b/extension/background.js @@ -47,7 +47,7 @@ async function sendData(path, payload, method) { let access = await getAccess(); const url = `${access.url}:${access.port}/${path}`; console.log(`${method}: ${url}`); - console.log(`${method}: ${JSON.stringify(payload)}`); + if (!path.endsWith('cookie/')) console.log(`${method}: ${JSON.stringify(payload)}`); try { const rawResponse = await fetch(url, { @@ -238,6 +238,35 @@ async function sendCookies() { return response; } +let listenerEnabled = false; +let isThrottled = false; + +async function handleContinuousCookie(checked) { + if (checked === true) { + browserType.cookies.onChanged.addListener(onCookieChange); + listenerEnabled = true; + console.log('Cookie listener enabled'); + } else { + browserType.cookies.onChanged.removeListener(onCookieChange); + listenerEnabled = false; + console.log('Cookie listener disabled'); + } +} + +function onCookieChange(changeInfo) { + if (!isThrottled) { + isThrottled = true; + + console.log('Cookie event detected:', changeInfo); + + sendCookies(); + + setTimeout(() => { + isThrottled = false; + }, 10000); + } +} + /* process and return message if needed the following messages are supported: @@ -245,6 +274,7 @@ type Message = | { type: 'verify' } | { type: 'cookieState' } | { type: 'sendCookie' } + | { type: 'continuousSync', checked: boolean } | { type: 'download', url: string } | { type: 'subscribe', url: string } | { type: 'unsubscribe', url: string } @@ -268,6 +298,9 @@ function handleMessage(request, sender, sendResponse) { case 'sendCookie': { return await sendCookies(); } + case 'continuousSync': { + return await handleContinuousCookie(request.checked); + } case 'download': { return await download(request.url); } @@ -305,3 +338,9 @@ function handleMessage(request, sender, sendResponse) { } browserType.runtime.onMessage.addListener(handleMessage); + +browserType.runtime.onInstalled.addListener(() => { + browserType.storage.local.get('continuousSync', data => { + handleContinuousCookie(data?.continuousSync.checked); + }); +}); diff --git a/extension/index.html b/extension/index.html index 38aab07..6e1fa25 100644 --- a/extension/index.html +++ b/extension/index.html @@ -28,12 +28,19 @@

-

Options:

+

Cookies:

- - Sync YouTube cookies +

TA cookies:

+
+ + Continuous Cookie Sync +
+ +
+

Download:

+
Autostart Downloads diff --git a/extension/popup.js b/extension/popup.js index 96eda88..05292aa 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -78,6 +78,11 @@ document.getElementById('sendCookies').addEventListener('click', function () { sendCookie(); }); +// continuous sync +document.getElementById('continuous-sync').addEventListener('click', function () { + toggleContinuousSync(); +}); + // autostart document.getElementById('autostart').addEventListener('click', function () { toggleAutostart(); @@ -103,8 +108,8 @@ function sendCookie() { function handleResponse(message) { console.log('handle cookie response: ' + JSON.stringify(message)); - let cookie_validated = message.cookie_validated; - document.getElementById('sendCookiesStatus').innerText = 'validated: ' + cookie_validated; + let validattionMessage = `enabled, last verified ${message.validated_str}`; + document.getElementById('sendCookiesStatus').innerText = validattionMessage; } function handleError(error) { @@ -112,20 +117,21 @@ function sendCookie() { setError(error); } - let checked = document.getElementById('sendCookies').checked; + let sending = sendMessage({ type: 'sendCookie' }); + sending.then(handleResponse, handleError); +} + +function toggleContinuousSync() { + const checked = document.getElementById('continuous-sync').checked; let toStore = { - sendCookies: { + continuousSync: { checked: checked, }, }; browserType.storage.local.set(toStore, function () { console.log('stored option: ' + JSON.stringify(toStore)); }); - if (checked === false) { - return; - } - let sending = sendMessage({ type: 'sendCookie' }); - sending.then(handleResponse, handleError); + sendMessage({ type: 'continuousSync', checked }); } function toggleAutostart() { @@ -170,9 +176,11 @@ function setCookieState() { clearError(); function handleResponse(message) { console.log(message); - document.getElementById('sendCookies').checked = message.cookie_enabled; - if (message.validated_str) { - document.getElementById('sendCookiesStatus').innerText = message.validated_str; + if (!message.cookie_enabled) { + document.getElementById('sendCookiesStatus').innerText = 'disabled'; + } else { + let validattionMessage = `enabled, last verified ${message.validated_str}`; + document.getElementById('sendCookiesStatus').innerText = validattionMessage; } } @@ -224,13 +232,13 @@ document.addEventListener('DOMContentLoaded', async () => { addUrl(item.access); } - function setCookiesOptions(result) { - if (!result.sendCookies || result.sendCookies.checked === false) { - console.log('sync cookies not set'); + function setContinuousCookiesOptions(result) { + if (!result.continuousSync || result.continuousSync.checked === false) { + console.log('continuous cookie sync not set'); return; } console.log('set options: ' + JSON.stringify(result)); - setCookieState(); + document.getElementById('continuous-sync').checked = true; } function setAutostartOption(result) { @@ -247,11 +255,13 @@ document.addEventListener('DOMContentLoaded', async () => { onGot(result); }); - browserType.storage.local.get('sendCookies', function (result) { - setCookiesOptions(result); + browserType.storage.local.get('continuousSync', function (result) { + setContinuousCookiesOptions(result); }); browserType.storage.local.get('autostart', function (result) { setAutostartOption(result); }); + + setCookieState(); }); diff --git a/extension/style.css b/extension/style.css index 930a9c2..cdcca87 100644 --- a/extension/style.css +++ b/extension/style.css @@ -18,6 +18,20 @@ hr { background-color: white; margin: 10px 0; } +button { + margin: 10px; + border-radius: 0; + padding: 5px 13px; + border: none; + cursor: pointer; + background-color: #259485; + color: #ffffff; +} +button:hover { + background-color: #97d4c8; + transform: scale(1.05); + color: #00202f; +} #download { text-align: center } @@ -48,20 +62,6 @@ hr { align-items: center; justify-content: center; } -.submit button { - margin: 10px; - border-radius: 0; - padding: 5px 13px; - border: none; - cursor: pointer; - background-color: #259485; - color: #ffffff; -} -.submit button:hover { - background-color: #97d4c8; - transform: scale(1.05); - color: #00202f; -} .options { display: block; padding-bottom: 10px;