Skip to content

Commit

Permalink
experimental work for extension #54
Browse files Browse the repository at this point in the history
  • Loading branch information
nondanee committed May 12, 2019
1 parent 2f38725 commit 639b69f
Show file tree
Hide file tree
Showing 12 changed files with 1,385 additions and 0 deletions.
25 changes: 25 additions & 0 deletions browser/README.md
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/)
4 changes: 4 additions & 0 deletions browser/background.html
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>
22 changes: 22 additions & 0 deletions browser/background.js
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'])
43 changes: 43 additions & 0 deletions browser/convert.js
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
})
Loading

0 comments on commit 639b69f

Please sign in to comment.