-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
82 lines (74 loc) · 2.97 KB
/
background.js
File metadata and controls
82 lines (74 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ripped from https://github.com/kairi003/Get-cookies.txt-LOCALLY/blob/master/src/modules/cookie_format.mjs
// Converts cookies from Chrome's JSON format to Netscape format (which is what yt-dlp expects).
function jsonToNetscapeMapper(cookies) {
return cookies.map(
({domain, expirationDate, path, secure, name, value}) => {
const includeSubDomain = !!domain?.startsWith('.');
const expiry = expirationDate?.toFixed() ?? '0';
const arr = [domain, includeSubDomain, path, secure, expiry, name, value];
return arr.map((v) =>
typeof v === 'boolean' ? v.toString().toUpperCase() : v,
);
},
);
}
function netscapeSerializer(cookies) {
const netscapeTable = jsonToNetscapeMapper(cookies);
const text = [
'# Netscape HTTP Cookie File',
'# http://curl.haxx.se/rfc/cookie_spec.html',
'# This is a generated file! Do not edit.',
'',
...netscapeTable.map((row) => row.join('\t')),
'', // Add a new line at the end
].join('\n');
return text;
}
// when the extension icon is clicked
chrome.action.onClicked.addListener(async (tab) => {
// inject the code
const {agree} = await chrome.storage.local.get({"agree": false});
if (agree) {
await chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ["/promise_utils.js", "/core/content/xmlproxy_content.js", "/core/content/content.js"],
injectImmediately: true,
world: "ISOLATED"
});
} else {
await chrome.tabs.create({url: chrome.runtime.getURL("/pages/agreement/index.html")});
}
});
// so, in firefox only, cookies can only be accessed from the background script.
// the iframe inits it so reloads ask for the cookies again.
async function cookies(url) {
// gather cookies (can only be done from the background script)
const cookies = await chrome.cookies.getAll({url: url});
// serialize to a format yt-dlp expects
return netscapeSerializer(cookies);
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.type) {
case "cookies":
// return cookies for the current URL
cookies(request.url).then(sendResponse);
break;
case "firefox_jspi_warning":
chrome.tabs.create({url: chrome.runtime.getURL("/pages/jspi/index.html")});
break
case "settings":
chrome.runtime.openOptionsPage();
}
// async return
return true;
})
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({url: chrome.runtime.getURL("/pages/welcome/index.html")});
chrome.storage.local.get({"agree": false}).then(({agree}) => {
if (!agree) {
chrome.tabs.create({url: chrome.runtime.getURL("/pages/agreement/index.html")});
}
});
}
});