-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,385 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Web Extension Port | ||
|
||
For test | ||
|
||
## Implementation | ||
|
||
- Convert node module to ES6 module which can be directly executed in Chrome 61+ without Babel | ||
- Rewrite crypto module (using CryptoJS) and request (using XMLHttpRequest) module for browser environment | ||
- Do matching in background and transfer result with chrome runtime communication | ||
- Inject content script for hijacking Netease Music Web Ajax response | ||
|
||
## Build | ||
|
||
``` | ||
$ node convert.js | ||
``` | ||
|
||
|
||
##Install | ||
|
||
Load unpacked extension in Developer mode | ||
|
||
## Reference | ||
|
||
- [brix/crypto-js](https://github.com/) |
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 @@ | ||
<script src="./crypto-js/core.js"></script> | ||
<script src="./crypto-js/md5.js"></script> | ||
<script src="./crypto-js/enc-base64.js"></script> | ||
<script type="module" src="background.js"></script> |
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 @@ | ||
import match from './provider/match.js' | ||
|
||
chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => { | ||
match(request.match, ['netease', 'qq', 'xiami']) | ||
.then(song => sendResponse(song)) | ||
.catch(e => console.log(e)) | ||
return true | ||
}) | ||
|
||
chrome.webRequest.onBeforeSendHeaders.addListener(details => { | ||
let headers = details.responseHeaders | ||
headers.push({name: 'X-Real-IP', value: '118.88.88.88'}) | ||
return {requestHeaders: headers} | ||
}, { urls: ['*://music.163.com/*'] }, ['blocking', 'requestHeaders']) | ||
|
||
chrome.webRequest.onHeadersReceived.addListener(details => { | ||
let headers = details.responseHeaders | ||
if(details.initiator == "https://music.163.com" && details.type == 'media'){ | ||
headers.push({name: 'Access-Control-Allow-Origin', value: '*'}) | ||
} | ||
return {responseHeaders: headers} | ||
}, {urls: ['*://*/*']}, ['blocking', 'responseHeaders']) |
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,43 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const importReplacer = (match, state, alias, file) => { | ||
file = file.endsWith('.js') ? file : file + '.js' | ||
return `import ${alias} from '${file}'` | ||
} | ||
|
||
const converter = (input, output, processor) => { | ||
let data = fs.readFileSync(input).toString() | ||
if(processor){ | ||
data = processor(data) | ||
} | ||
else{ | ||
data = data.replace(/global\./g, 'window.') | ||
data = data.replace(/(const|let|var)\s+(\w+)\s*=\s*require\(\s*['|"](.+)['|"]\s*\)/g, importReplacer) | ||
data = data.replace(/module\.exports\s*=\s*/g, 'export default ') | ||
} | ||
fs.writeFileSync(output, data) | ||
} | ||
|
||
converter(path.resolve('..', 'cache.js'), path.resolve('.', 'cache.js')) | ||
|
||
fs.mkdirSync(path.resolve(__dirname, 'provider')) | ||
|
||
fs.readdirSync(path.resolve(__dirname, '..', 'provider')).filter(file => !file.includes('test')).forEach(file => { | ||
converter(path.resolve(__dirname, '..', 'provider', file), path.resolve(__dirname, 'provider', file)) | ||
}) | ||
|
||
const providerReplacer = (match, state, data) => { | ||
let provider = [] | ||
let imports = data.match(/\w+\s*:\s*require\(['|"].+['|"]\)/g).map(line => { | ||
line = line.match(/(\w+)\s*:\s*require\(['|"](.+)['|"]\)/) | ||
provider.push(line[1]) | ||
return importReplacer(null, null, line[1], line[2]) | ||
}) | ||
return imports.join('\n') + '\n\n' + `${state} provider = {${provider.join(', ')}}` | ||
} | ||
|
||
converter(path.resolve(__dirname, 'provider', 'match.js'), path.resolve(__dirname, 'provider', 'match.js'), data => { | ||
data = data.replace(/(const|let|var)\s+provider\s*=\s*{([^}]+)}/g, providerReplacer) | ||
return data | ||
}) |
Oops, something went wrong.