forked from meganz/web-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mega.js
132 lines (113 loc) · 4.22 KB
/
mega.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function(chrome) {
'use strict';
const manifest = chrome.runtime.getManifest();
const production = manifest.version !== '109101.103.97';
const baseURL = chrome.extension.getURL(production ? 'mega/secure.html' : 'webclient/index.html');
const extPassTh = {
'.xml': 1,
'.crx': 1,
'.xpi': 1,
'.exe': 1,
'.dmg': 1,
'.deb': 1,
'.rpm': 1,
'.zip': 1,
'.txt': 1,
'.pdf': 1,
'.js': 1,
'.gz': 1
};
const qsMap = v => String(v || '').replace(/[&?]/g, '/');
const getPathHash = (aUrl, aDefValue) => {
try {
const uri = new URL(aUrl);
return uri.pathname.substr(1) + qsMap(uri.search) + uri.hash;
}
catch (ex) {}
return qsMap(aDefValue);
};
const onBeforeRequest = (aRequest) => {
const url = aRequest.url;
const ext3 = url.substr(-3);
const ext4 = url.substr(-4);
if (extPassTh[ext3] || extPassTh[ext4] || url.indexOf('mega.nz/linux') > 0) {
return {cancel: false};
}
let hash = '';
const domain = (url.match(/:\/\/[^/]+\//) || [''])[0];
const cleanDomain = domain.replace(/www\.|co\./g, '');
if (cleanDomain === '://mega.nz/' || cleanDomain === '://mega.io/') {
const path = url.split(domain)[1];
if (/^\/*(?:chat|file|folder|help)\//.test(path)) {
hash = '#' + getPathHash(url, path);
}
else if (url.indexOf('#') > -1) {
const nlfe = url.match(/\/\/mega\.nz\/+(embed|drop)[!#/]+([\w-]{8,11})(?:[#!](.*))?/i);
if (nlfe) {
let type = nlfe[1];
let node = nlfe[2];
let pkey = nlfe[3];
let lpfx = ({embed: 'E', drop: 'D'})[type] || '';
hash = '#' + lpfx + '!' + node + (pkey ? '!' + pkey : '');
}
else {
hash = '#' + url.split('#')[1];
}
}
else if (path) {
hash = '#' + (path.indexOf('?') !== -1 ? getPathHash(url, path) : path);
}
}
return {redirectUrl: baseURL + hash};
};
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest,
{
urls: [
"http://mega.co.nz/*",
"https://mega.co.nz/*",
"http://www.mega.co.nz/*",
"https://www.mega.co.nz/*",
"http://mega.nz/*",
"https://mega.nz/*",
"http://www.mega.nz/*",
"https://www.mega.nz/*"
],
types: ["main_frame", "sub_frame"]
},
["blocking"]
);
if (!production) {
const extURL = chrome.runtime.getURL('/');
chrome.webRequest.onBeforeRequest.addListener(
(aRequest) => {
let target = false;
let path = aRequest.url.replace(extURL, '');
if (path === 'secureboot.js') {
target = extURL + 'webclient/secureboot.js';
}
else if (path === 'mega/secure.html') {
target = baseURL;
}
else if (path.substr(0, 5) === 'mega/') {
if (path.substr(0, 10) === 'mega/lang/') {
path = path.replace('.json', '_prod.json');
}
target = extURL + path.replace('mega/', 'webclient/');
}
// console.log('redirectUrl', aRequest.url, target);
return target ? {redirectUrl: target} : {cancel: false};
},
{
urls: [chrome.extension.getURL('/*')],
types: ['main_frame', 'sub_frame', 'image', 'script', 'xmlhttprequest', 'other']
},
["blocking"]
);
}
chrome.browserAction.onClicked.addListener(() => {
chrome.tabs.create({url: manifest.homepage_url + (production ? '' : '#debug')});
});
chrome.browserAction.setTitle({
title: '' + manifest.name + ' v' + manifest.version + (production ? '' : ' (development)')
});
})(self.browser || self.chrome);