-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome-extensions-list.js
176 lines (147 loc) · 4.99 KB
/
chrome-extensions-list.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require('fs.promises');
const fs = require('fs');
const fsp = fs.promises;
const { Html5Entities } = require('html-entities');
const https = require('https');
const ora = require('ora');
const path = require('path');
const args = process.argv.slice(2);
const optionsMap = {
'--print': 'print',
'--help': 'help'
};
const options = Object.entries(optionsMap).reduce((options, [optionArg, option]) => (
Object.assign(options, { [option]: args.includes(optionArg) })
), {});
let [profileRelPath, htmlPath] = args.filter(arg => !Object.keys(optionsMap).includes(arg));
if (options.help || !profileRelPath) {
console.log(`
Exports a list of installed Chrome extensions from a profile (defaults to program location) to HTML file.
Make sure chrome-extensions-list has read and write access to these locations.
Usage:
chrome-extensions-list --help
chrome-extensions-list --print <path-to-profile>
chrome-extensions-list <path-to-profile-dir> [path-to-html-file]
`);
process.exit(1);
}
let profilePath = path.resolve(profileRelPath);
let extsPath = path.join(profilePath, 'Extensions');
if (!fs.existsSync(extsPath)) {
profilePath = path.resolve(profileRelPath, 'Default');
extsPath = path.join(profilePath, 'Extensions');
if (!fs.existsSync(extsPath)) {
console.log('Extensions path not found');
process.exit(1);
}
}
(async () => {
const spinner = ora({ text: 'Exporting', spinner: 'line' }).start();
try {
await init();
} catch (err) {
console.warn(err);
} finally {
spinner.stop();
}
})();
async function init() {
const extSettings = {};
try {
const preferencesPath = path.join(profilePath, 'Secure Preferences');
const preferences = JSON.parse(await fsp.readFile(preferencesPath, { encoding: 'utf8' }));
Object.assign(extSettings, preferences.extensions.settings);
} catch (_err) {}
const exts = [];
const extFilenames = (await fsp.readdir(extsPath))
.filter(filename => /^[a-z]{32}$/.test(filename));
for (const extId of extFilenames) {
let extName;
let extVer;
try {
const extPath = path.join(extsPath, extId);
const [highestVerDirname] = (await fsp.readdir(extPath))
.filter(highestVerDirname => /^\d[\d\.]*(?:_\d+|)$/.test(highestVerDirname))
.sort((a,b) => a.localeCompare(b, 'en-US', { numeric: true, sensitivity: 'base' }))
.reverse();
[, extVer] = highestVerDirname.match(/^(\d[\d\.]*)/) || [];
const manifestPath = path.join(extPath, highestVerDirname, 'manifest.json');
const manifest = JSON.parse(await fsp.readFile(manifestPath, { encoding: 'utf8' }));
extName = !/^__MSG_/.test(manifest.name) ? manifest.name : extSettings[extId].manifest.name;
} catch (_err) {}
const disabled = (extSettings[extId] && extSettings[extId].state === 0);
let unavailable;
try {
unavailable = await isExtUnavailable(extId);
await delay(50);
} catch (_err) {}
const extNameHtml = Html5Entities.encode(extName || extId);
exts.push({
disabled,
name: extName,
html: `<p><a ${unavailable === true ? 'class="unavailable" ' : ''}href="https://chrome.google.com/webstore/detail/${extId}">${extNameHtml} ${extVer || ''}</a></p>`
});
}
exts.sort((a, b) => (a.name || '').localeCompare(b.name || '', 'en-US'));
const enabledExtsHtml = exts.filter(({ disabled }) => !disabled).map(({ html }) => html).join('\n');
const disabledExtsHtml = exts.filter(({ disabled }) => disabled).map(({ html }) => html).join('\n');
const html = `<html>
<head>
<title>Chrome extensions</title>
<style>
body { color: #333; }
a.unavailable { opacity: 0.75; text-decoration: line-through; }
</style>
</head>
<body>
<section>
<h2>Enabled extensions</h2>
${enabledExtsHtml}
</section>
<section>
<h2>Disabled extensions</h2>
${disabledExtsHtml}
</section>
</body>
</html>`;
if (options.print) {
console.log(html);
} else {
if (!htmlPath) {
const timestamp = new Date().toISOString().slice(0,-1).replace(/(\D)/g, '-');
const appDirPath = process.pkg ? path.dirname(process.execPath) : __dirname;
htmlPath = path.join(appDirPath, `chrome-extensions-${timestamp}.html`);
}
await fsp.writeFile(htmlPath, html, { encoding: 'utf8' });
}
}
function isExtUnavailable(extId) {
const extUrlRegexp = /^https:\/\/chrome\.google\.com\/webstore\/detail\/.+\/[a-z]{32}$/;
const options = {
host: 'chrome.google.com',
port: 443,
path: `/webstore/detail/${extId}`,
method: 'HEAD'
};
return new Promise((resolve, reject) => {
https.request(options, ({ statusCode, headers }) => {
const isAvailable = statusCode === 301 && extUrlRegexp.test(headers.location);
const isUnavailable = statusCode === 404;
if (isAvailable || isUnavailable) {
resolve(isUnavailable);
} else {
reject();
}
})
.on('error', () => reject())
.on('timeout', () => {
req.abort();
reject();
})
.setTimeout(5000)
.end();
});
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}