-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Removed some Chrome extensions that weren't supported, now require Electron >=18, `installExtension` now resolves with a full extension object instead of just the name * Expanding API where it makes sense * Allow providing `session` * Cleaning up old code style, moving to async/await etc. * Cleaning up left over IDMap * Removing dead chrome extensions Closes #180 Closes #134
- Loading branch information
1 parent
7422d39
commit 8e96039
Showing
9 changed files
with
227 additions
and
3,339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,59 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as rimraf from 'rimraf'; | ||
|
||
import { getPath, downloadFile, changePermissions } from './utils'; | ||
|
||
const unzip: any = require('unzip-crx-3'); | ||
|
||
const downloadChromeExtension = ( | ||
export const downloadChromeExtension = async ( | ||
chromeStoreID: string, | ||
forceDownload?: boolean, | ||
attempts = 5, | ||
{ | ||
forceDownload = false, | ||
attempts = 5, | ||
}: { | ||
forceDownload?: boolean; | ||
attempts?: number; | ||
} = {}, | ||
): Promise<string> => { | ||
const extensionsStore = getPath(); | ||
if (!fs.existsSync(extensionsStore)) { | ||
fs.mkdirSync(extensionsStore, { recursive: true }); | ||
await fs.promises.mkdir(extensionsStore, { recursive: true }); | ||
} | ||
const extensionFolder = path.resolve(`${extensionsStore}/${chromeStoreID}`); | ||
return new Promise((resolve, reject) => { | ||
if (!fs.existsSync(extensionFolder) || forceDownload) { | ||
if (fs.existsSync(extensionFolder)) { | ||
rimraf.sync(extensionFolder); | ||
|
||
if (!fs.existsSync(extensionFolder) || forceDownload) { | ||
if (fs.existsSync(extensionFolder)) { | ||
await fs.promises.rmdir(extensionFolder, { | ||
recursive: true, | ||
}); | ||
} | ||
const fileURL = `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${chromeStoreID}%26uc&prodversion=${process.versions.chrome}`; // eslint-disable-line | ||
const filePath = path.resolve(`${extensionFolder}.crx`); | ||
try { | ||
await downloadFile(fileURL, filePath); | ||
|
||
try { | ||
await unzip(filePath, extensionFolder); | ||
changePermissions(extensionFolder, 755); | ||
return extensionFolder; | ||
} catch (err) { | ||
if (!fs.existsSync(path.resolve(extensionFolder, 'manifest.json'))) { | ||
throw err; | ||
} | ||
} | ||
} catch (err) { | ||
console.error(`Failed to fetch extension, trying ${attempts - 1} more times`); // eslint-disable-line | ||
if (attempts <= 1) { | ||
throw err; | ||
} | ||
const fileURL = `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${chromeStoreID}%26uc&prodversion=${process.versions.chrome}`; // eslint-disable-line | ||
const filePath = path.resolve(`${extensionFolder}.crx`); | ||
downloadFile(fileURL, filePath) | ||
.then(() => { | ||
unzip(filePath, extensionFolder) | ||
.then(() => { | ||
changePermissions(extensionFolder, 755); | ||
resolve(extensionFolder); | ||
}) | ||
.catch((err: Error) => { | ||
if (!fs.existsSync(path.resolve(extensionFolder, 'manifest.json'))) { | ||
return reject(err); | ||
} | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.log(`Failed to fetch extension, trying ${attempts - 1} more times`); // eslint-disable-line | ||
if (attempts <= 1) { | ||
return reject(err); | ||
} | ||
setTimeout(() => { | ||
downloadChromeExtension(chromeStoreID, forceDownload, attempts - 1) | ||
.then(resolve) | ||
.catch(reject); | ||
}, 200); | ||
}); | ||
} else { | ||
resolve(extensionFolder); | ||
await new Promise<void>((resolve) => setTimeout(resolve, 200)); | ||
|
||
return await downloadChromeExtension(chromeStoreID, { | ||
forceDownload, | ||
attempts: attempts - 1, | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export default downloadChromeExtension; | ||
return extensionFolder; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.