-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from Anthony-YY/support_download_inspect
support chrome download activity access under headless mode
- Loading branch information
Showing
17 changed files
with
274 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"require" : "../../utils/ts-mocha.js", | ||
"watch-extensions": "ts", | ||
"timeout": 30000, | ||
"reporter": "dot", | ||
"spec": "test/**/*.spec.ts" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
test | ||
mocha.opts | ||
tsconfig.json |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
test | ||
mocha.opts | ||
tsconfig.json |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
save-exact=true | ||
|
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# @testring/dwnld-collector-crx | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @testring/dwnld-collector-crx | ||
``` | ||
|
||
## How to use | ||
accessing chrome internal page like chrome://downloads is not allowed in headless mode, as a result, checking download results becomes unavaiable. | ||
once this chrome extension installed. chrome download items can be accessed within page via localStorage, like this: | ||
```javascript | ||
const downloadsJSONStr = await browser.execute(() => { | ||
return localStorage.getItem('_DOWNLOADS_'); | ||
}) | ||
// the result is already sort ASC by startTime | ||
const downloads = JSON.parse(downloadsJSONStr); | ||
|
||
``` | ||
downloads is an array of download items, each item has following properties: | ||
```javascript | ||
{ | ||
fileName: 'example.pdf', | ||
filePath: '/Users/username/Downloads/example.pdf', | ||
state: 'complete', | ||
startTime: '2021-01-01T00:00:00.000Z', | ||
state: 'complete', | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module '@testring/dwnld-collector-crx' { | ||
export const getCrxBase64: () => string; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@testring/dwnld-collector-crx", | ||
"version": "0.6.11", | ||
"main": "./dist/index.js", | ||
"types": "./index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ringcentral/testring.git" | ||
}, | ||
"author": "RingCentral", | ||
"license": "MIT", | ||
"scripts": { | ||
"postinstall": "mkdir -p dist && crx pack src/extension -o dist/extension.crx" | ||
}, | ||
"dependencies": { | ||
"crx": "5.0.1" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/download-collector-crx/src/extension/background.js
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
chrome.downloads.onCreated.addListener((downloadItem) => { | ||
chrome.tabs.query({}, (tabs) => { | ||
tabs.forEach((tab) => { | ||
chrome.tabs.sendMessage(tab.id, { | ||
action: 'downloadStarted', | ||
downloadItem, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
chrome.downloads.onDeterminingFilename.addListener((downloadItem) => { | ||
chrome.tabs.query({}, (tabs) => { | ||
tabs.forEach((tab) => { | ||
chrome.tabs.sendMessage(tab.id, { | ||
action: 'downloadDetermined', | ||
downloadItem, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
chrome.downloads.onChanged.addListener((downloadItem) => { | ||
chrome.tabs.query({}, (tabs) => { | ||
tabs.forEach((tab) => { | ||
chrome.tabs.sendMessage(tab.id, { | ||
action: 'downloadChanged', | ||
downloadItem, | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const DOWNLOAD_KEY = "_DOWNLOADS_"; | ||
const DOWNLOADS_PERSISTED = localStorage.getItem(DOWNLOAD_KEY) ? JSON.parse(localStorage.getItem(DOWNLOAD_KEY) ?? "[]") : []; | ||
const DOWNLOADS = DOWNLOADS_PERSISTED.reduce((acc, download) => { | ||
acc[download.id] = download; | ||
return acc; | ||
}, {}); | ||
|
||
chrome.runtime.onMessage.addListener((message) => { | ||
const {action, downloadItem} = message; | ||
if (action === 'downloadStarted') { | ||
DOWNLOADS[downloadItem.id] = { | ||
id: downloadItem.id, | ||
fileName: '', | ||
fileUrl: '', | ||
state: downloadItem.state, | ||
startTime: new Date(downloadItem.startTime).getTime(), | ||
}; | ||
updatePageVariable(); | ||
return; | ||
} | ||
|
||
if (action === 'downloadDetermined') { | ||
const download = DOWNLOADS[downloadItem.id]; | ||
download.fileName = downloadItem.filename; | ||
download.state = downloadItem.state; | ||
updatePageVariable(); | ||
return; | ||
} | ||
|
||
if (action === 'downloadChanged') { | ||
const download = DOWNLOADS[downloadItem.id]; | ||
const filePath = downloadItem.filename?.current; | ||
const state = downloadItem.state?.current; | ||
if (filePath) { | ||
download.filePath = filePath; | ||
} | ||
if (state) { | ||
download.state = state; | ||
} | ||
updatePageVariable(); | ||
} | ||
}); | ||
|
||
function updatePageVariable() { | ||
const downloads = Object.values(DOWNLOADS); | ||
downloads.sort((a, b) => b.startTime - a.startTime); | ||
localStorage.setItem('_DOWNLOADS_', JSON.stringify(downloads)); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/download-collector-crx/src/extension/manifest.json
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"manifest_version": 3, | ||
"name": "Download Monitor Extension", | ||
"version": "1.0", | ||
"permissions": [ | ||
"downloads", | ||
"downloads.shelf", | ||
"tabs", | ||
"activeTab", | ||
"storage" | ||
], | ||
"background": { | ||
"service_worker": "background.js" | ||
}, | ||
"content_scripts": [ | ||
{ | ||
"matches": ["<all_urls>"], | ||
"js": ["content.js"] | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const crxFilePath = path.join(__dirname, './extension.crx'); | ||
|
||
let CACHED_CRX_BASE64 = null; | ||
|
||
function getCrxBase64() { | ||
if (CACHED_CRX_BASE64) { | ||
return CACHED_CRX_BASE64; | ||
} | ||
|
||
const crxData = fs.readFileSync(crxFilePath); | ||
CACHED_CRX_BASE64 = crxData.toString('base64'); | ||
|
||
return CACHED_CRX_BASE64; | ||
} | ||
|
||
module.exports = { | ||
getCrxBase64, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBhPtoAnu/e1jg | ||
OTn1twETyvuuri4i82iFL+Pxm5xFxb2f9y1apFgtPCV4ZQnReso4f60tojmWrk1b | ||
3vZaXK3em9GgMaYOm76f+t6qY+w0IaGhg/NTj69pZ43pzxTkTXmc0CDqrz+0osWb | ||
bh3qZdugcnxDmX3RSYQBMf/oWAjtkETbCnWaxX0m0WQGrXRbjVC+yQVEl7xNSFMW | ||
PCS5T254Tv41i+q4LaL14D+PA1TXcwHTmfZyrX/Iz+deKZnlAK4kKFl9X+7c+y++ | ||
pnUYpwfhfkXwONwO/hy6c3c8f3PR/vegMsCuPCCzBHZrTXPdlFnKin6CAvzjB1r6 | ||
rznBVGOBAgMBAAECggEBAJnEvCvSRVhKf71zW222Y6HBmakceEaHWRbzjdFOj6cV | ||
T+7K7nvmuLYA49k9l8afJg4szYPEMrRbfdaxXNlCaVnIQJJkwQk8kgT2x3Vm/qoR | ||
yyfW/EL6mixL/4S4amZadXa4Hl+8rwcui4xMvHKjSxe7wKfKUCI7oyt7+lc5lKaG | ||
p01BBYTHQaFUmuLRMxWlTZr6qZM1btXXd0A9qQQOUsRZ8N017LdB6+VYCWY1t36q | ||
Ey8FrbZKVXFvwt9yd8MxdaOHmSpsIGLlnNpJykneP9f17tT8UskH0GQgjgZ9ba3I | ||
8l1RysCP802pHOwaMfNtMYJ11i85tBaIz7AB/neZqJECgYEA+cLj7C2wYDvCiTd3 | ||
Nmp51sdM8UP4pYLF8F73XUNEyG6VRBXmzJqIOSKrlGqgEgfU/gjpWtdtef2NKMNY | ||
fdz0k5Oyv7De+es24KI+7rlhP6Ptv7YM2urpm8C42i4iC7XBGtgxyaqnqPaQm283 | ||
TM4Tqdx40Y9TpMM2JYWuqd4aVV0CgYEAxlpza0b/2q6mM79T8eCdjjCRW1G8NMl8 | ||
PXvQrpPo0pLRrTptpqMDg7JRcCnxNifHGna8PsrzHkWVjsfwYkwbp2NANG+eoXI9 | ||
wTnqtIUsFTRCeqkKl3SPyi4EAih3bluEZC+WdvDiy3eWXiCfHxz/EVZGw9suoeCu | ||
g/7oMjNi4HUCgYAkiyx4IRM+cV/8Xb42mwuqrkyGvJBD/0dg7TQ6VB5bSTrT1HSJ | ||
mU63NWhvdc5n9PdoF/u0y/J7t+qQfUyUVeD/OswbmhB19sF3yqV0nnEpM54Uv9lP | ||
qrF1lZQ2cCuRFQ3lFJ7sR+jyIulzpKkttrVP1C9lUhhF8j4Y7V9qAVJPDQKBgEg3 | ||
pHA5kGvZTK/oiDK3egXMDxA1iRWbCj4Ed20ocws/41FzxXp3PY9UfCwfSTBTeT1c | ||
X5tpHu01noc2qoHPff4Kt9Sfkxzq0Csq4BZLqkoqFc48/5s3GCcfa9wxSZKHhYNI | ||
hDrX52r3Jmss62JTl1aDmA41HhxYBpIOXBHy/ZwFAoGAEJnzQ1NzHH52MArcwrm5 | ||
Ku636x3PflPxP7aAYU3GbGJCLLsvS2V0ABxjk/VcHrHCbeIZxZ0YQX7BIAXOkO1a | ||
Dh2fubgnpqDNFIyeFt9ZHJ0Zg5KC3NXK75ErbVSC3WPht3T3va7hITvGVbMkXCRA | ||
Bducv2SfztUPms8SlB4GVIQ= | ||
-----END PRIVATE KEY----- |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2015", | ||
"module": "commonjs", | ||
"allowJs": false, | ||
"moduleResolution": "node", | ||
"noFallthroughCasesInSwitch": true, | ||
"allowSyntheticDefaultImports": true, | ||
"preserveConstEnums": true, | ||
"noEmitOnError": true, | ||
"declaration": false, | ||
"outDir": "dist", | ||
"sourceMap": false, | ||
"lib": ["es5", "es2015", "es2016", "es2017", "dom"], | ||
"typeRoots": ["./node_modules/@types", "../../node_modules/@types"], | ||
"noUnusedLocals": true, | ||
"strictNullChecks": true, | ||
"removeComments": false | ||
}, | ||
"include": ["*.ts", "src/**/*.ts", "src/**/*.json"], | ||
"exclude": ["./node_modules", "../../node_modules"] | ||
} |
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