forked from nondanee/UnblockNeteaseMusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
34 lines (31 loc) · 1.44 KB
/
background.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
import match from './provider/match.js'
const self = chrome.runtime.id
chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
match(request.match, ['qq'])
.then(song => sendResponse(song))
.catch(console.log)
return true
})
chrome.webRequest.onBeforeSendHeaders.addListener(details => {
let headers = details.requestHeaders
if(details.url.includes('//music.163.com/')){
headers.push({name: 'X-Real-IP', value: '118.88.88.88'})
}
if(details.initiator == `chrome-extension://${self}`){
let index = headers.findIndex(item => item.name.toLowerCase() === 'additional-headers')
if(index === -1) return
Object.entries(JSON.parse(atob(headers[index].value))).forEach(entry => headers.push({name: entry[0], value: entry[1]}))
headers.splice(index, 1)
}
if(details.initiator == 'https://music.163.com' && (details.type == 'media' || details.url.includes('.mp3'))){
headers = headers.filter(item => !['referer', 'origin'].includes(item.name.toLowerCase()))
}
return {requestHeaders: headers}
}, {urls: ['*://*/*']}, ['blocking', 'requestHeaders', 'extraHeaders'])
chrome.webRequest.onHeadersReceived.addListener(details => {
let headers = details.responseHeaders
if(details.initiator == 'https://music.163.com' && (details.type == 'media' || details.url.includes('.mp3'))){
headers.push({name: 'Access-Control-Allow-Origin', value: '*'})
}
return {responseHeaders: headers}
}, {urls: ['*://*/*']}, ['blocking', 'responseHeaders'])