mirror of
https://github.com/tubearchivist/browser-extension.git
synced 2025-02-05 15:40:14 +00:00
fix cookie API url, implement continuous sync
This commit is contained in:
parent
492db3ed3c
commit
b28efc7960
@ -47,7 +47,7 @@ async function sendData(path, payload, method) {
|
|||||||
let access = await getAccess();
|
let access = await getAccess();
|
||||||
const url = `${access.url}:${access.port}/${path}`;
|
const url = `${access.url}:${access.port}/${path}`;
|
||||||
console.log(`${method}: ${url}`);
|
console.log(`${method}: ${url}`);
|
||||||
console.log(`${method}: ${JSON.stringify(payload)}`);
|
if (!path.endsWith('cookie/')) console.log(`${method}: ${JSON.stringify(payload)}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rawResponse = await fetch(url, {
|
const rawResponse = await fetch(url, {
|
||||||
@ -238,6 +238,35 @@ async function sendCookies() {
|
|||||||
return response;
|
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
|
process and return message if needed
|
||||||
the following messages are supported:
|
the following messages are supported:
|
||||||
@ -245,6 +274,7 @@ type Message =
|
|||||||
| { type: 'verify' }
|
| { type: 'verify' }
|
||||||
| { type: 'cookieState' }
|
| { type: 'cookieState' }
|
||||||
| { type: 'sendCookie' }
|
| { type: 'sendCookie' }
|
||||||
|
| { type: 'continuousSync', checked: boolean }
|
||||||
| { type: 'download', url: string }
|
| { type: 'download', url: string }
|
||||||
| { type: 'subscribe', url: string }
|
| { type: 'subscribe', url: string }
|
||||||
| { type: 'unsubscribe', url: string }
|
| { type: 'unsubscribe', url: string }
|
||||||
@ -268,6 +298,9 @@ function handleMessage(request, sender, sendResponse) {
|
|||||||
case 'sendCookie': {
|
case 'sendCookie': {
|
||||||
return await sendCookies();
|
return await sendCookies();
|
||||||
}
|
}
|
||||||
|
case 'continuousSync': {
|
||||||
|
return await handleContinuousCookie(request.checked);
|
||||||
|
}
|
||||||
case 'download': {
|
case 'download': {
|
||||||
return await download(request.url);
|
return await download(request.url);
|
||||||
}
|
}
|
||||||
@ -305,3 +338,9 @@ function handleMessage(request, sender, sendResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
browserType.runtime.onMessage.addListener(handleMessage);
|
browserType.runtime.onMessage.addListener(handleMessage);
|
||||||
|
|
||||||
|
browserType.runtime.onInstalled.addListener(() => {
|
||||||
|
browserType.storage.local.get('continuousSync', data => {
|
||||||
|
handleContinuousCookie(data?.continuousSync.checked);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -28,12 +28,19 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="error-out"></div>
|
<div id="error-out"></div>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Options:</p>
|
<p>Cookies:</p>
|
||||||
<div class="options">
|
<div class="options">
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" id="sendCookies" name="sendCookies">
|
<p>TA cookies: <span id="sendCookiesStatus" /></p>
|
||||||
<span>Sync YouTube cookies</span><span id="sendCookiesStatus"></span>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" id="continuous-sync" name="continuous-sync">
|
||||||
|
<span>Continuous Cookie Sync</span>
|
||||||
|
</div>
|
||||||
|
<button id="sendCookies">Copy Now</button>
|
||||||
|
</div>
|
||||||
|
<p>Download:</p>
|
||||||
|
<div class="options">
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" id="autostart" name="autostart">
|
<input type="checkbox" id="autostart" name="autostart">
|
||||||
<span>Autostart Downloads</span>
|
<span>Autostart Downloads</span>
|
||||||
|
@ -78,6 +78,11 @@ document.getElementById('sendCookies').addEventListener('click', function () {
|
|||||||
sendCookie();
|
sendCookie();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// continuous sync
|
||||||
|
document.getElementById('continuous-sync').addEventListener('click', function () {
|
||||||
|
toggleContinuousSync();
|
||||||
|
});
|
||||||
|
|
||||||
// autostart
|
// autostart
|
||||||
document.getElementById('autostart').addEventListener('click', function () {
|
document.getElementById('autostart').addEventListener('click', function () {
|
||||||
toggleAutostart();
|
toggleAutostart();
|
||||||
@ -103,8 +108,8 @@ function sendCookie() {
|
|||||||
|
|
||||||
function handleResponse(message) {
|
function handleResponse(message) {
|
||||||
console.log('handle cookie response: ' + JSON.stringify(message));
|
console.log('handle cookie response: ' + JSON.stringify(message));
|
||||||
let cookie_validated = message.cookie_validated;
|
let validattionMessage = `enabled, last verified ${message.validated_str}`;
|
||||||
document.getElementById('sendCookiesStatus').innerText = 'validated: ' + cookie_validated;
|
document.getElementById('sendCookiesStatus').innerText = validattionMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleError(error) {
|
function handleError(error) {
|
||||||
@ -112,20 +117,21 @@ function sendCookie() {
|
|||||||
setError(error);
|
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 = {
|
let toStore = {
|
||||||
sendCookies: {
|
continuousSync: {
|
||||||
checked: checked,
|
checked: checked,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
browserType.storage.local.set(toStore, function () {
|
browserType.storage.local.set(toStore, function () {
|
||||||
console.log('stored option: ' + JSON.stringify(toStore));
|
console.log('stored option: ' + JSON.stringify(toStore));
|
||||||
});
|
});
|
||||||
if (checked === false) {
|
sendMessage({ type: 'continuousSync', checked });
|
||||||
return;
|
|
||||||
}
|
|
||||||
let sending = sendMessage({ type: 'sendCookie' });
|
|
||||||
sending.then(handleResponse, handleError);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleAutostart() {
|
function toggleAutostart() {
|
||||||
@ -170,9 +176,11 @@ function setCookieState() {
|
|||||||
clearError();
|
clearError();
|
||||||
function handleResponse(message) {
|
function handleResponse(message) {
|
||||||
console.log(message);
|
console.log(message);
|
||||||
document.getElementById('sendCookies').checked = message.cookie_enabled;
|
if (!message.cookie_enabled) {
|
||||||
if (message.validated_str) {
|
document.getElementById('sendCookiesStatus').innerText = 'disabled';
|
||||||
document.getElementById('sendCookiesStatus').innerText = message.validated_str;
|
} 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);
|
addUrl(item.access);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCookiesOptions(result) {
|
function setContinuousCookiesOptions(result) {
|
||||||
if (!result.sendCookies || result.sendCookies.checked === false) {
|
if (!result.continuousSync || result.continuousSync.checked === false) {
|
||||||
console.log('sync cookies not set');
|
console.log('continuous cookie sync not set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('set options: ' + JSON.stringify(result));
|
console.log('set options: ' + JSON.stringify(result));
|
||||||
setCookieState();
|
document.getElementById('continuous-sync').checked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setAutostartOption(result) {
|
function setAutostartOption(result) {
|
||||||
@ -247,11 +255,13 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||||||
onGot(result);
|
onGot(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
browserType.storage.local.get('sendCookies', function (result) {
|
browserType.storage.local.get('continuousSync', function (result) {
|
||||||
setCookiesOptions(result);
|
setContinuousCookiesOptions(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
browserType.storage.local.get('autostart', function (result) {
|
browserType.storage.local.get('autostart', function (result) {
|
||||||
setAutostartOption(result);
|
setAutostartOption(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setCookieState();
|
||||||
});
|
});
|
||||||
|
@ -18,6 +18,20 @@ hr {
|
|||||||
background-color: white;
|
background-color: white;
|
||||||
margin: 10px 0;
|
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 {
|
#download {
|
||||||
text-align: center
|
text-align: center
|
||||||
}
|
}
|
||||||
@ -48,20 +62,6 @@ hr {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: 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 {
|
.options {
|
||||||
display: block;
|
display: block;
|
||||||
padding-bottom: 10px;
|
padding-bottom: 10px;
|
||||||
|
Loading…
Reference in New Issue
Block a user