forked from nondanee/NetEaseMusicWorldPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
40 lines (35 loc) · 1.3 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
35
36
37
38
39
40
let mode
// hook request headers
chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
const header = {
name: 'X-Real-IP',
value: '118.88.88.88'
}
if (mode > 0) details.requestHeaders.push(header)
return { requestHeaders: details.requestHeaders }
}, { urls: ['*://music.163.com/*'] }, ['blocking', 'requestHeaders'])
// redirect cdn access
chrome.webRequest.onBeforeRequest.addListener(function (details) {
const redirectUrl = details.url.replace(
/(m\d+?)(?!c)\.music\.126\.net/, '$1c.music.126.net'
)
if (redirectUrl != details.url && mode > 1) return { redirectUrl: redirectUrl }
}, { urls: ['*://*.music.126.net/*'] }, ['blocking'])
// mode switch
chrome.browserAction.onClicked.addListener(function (tab) {
mode = (mode + 1) % 3
setMode()
})
// initialize
chrome.storage.local.get('mode', function (data) {
mode = (typeof(data.mode) === "undefined") ? 2 : data.mode
setMode()
})
// sync ui & storage
function setMode(){
chrome.storage.local.set({ mode: mode })
const titles = ['closed','normal','enhanced']
const icons = ['images/grey.svg','images/red.svg','images/blue.svg']
chrome.browserAction.setIcon({ path: icons[mode] })
chrome.browserAction.setTitle({ title: chrome.i18n.getMessage("name") + ' ' + '[' + chrome.i18n.getMessage(titles[mode]) + ']' })
}