diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 6746346b..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,20 +0,0 @@ -module.exports = { - env: { - es2021: true, - browser: true, - webextensions: true, - node: true, - jquery: true, - }, - extends: [ - 'google', - ], - parserOptions: { - ecmaVersion: 'latest', - sourceType: 'module', - }, - rules: { - 'require-jsdoc': 'off', - 'max-len': ['error', {'code': 100}], - }, -}; diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 00000000..f7f981f8 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,25 @@ +{ + "env": { + "browser": true, + "es2021": true, + "webextensions": true, + "jquery": true + }, + "extends": [ + "google" + ], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": { + "require-jsdoc": "off", + "no-multi-str": "off", + "max-len": [ + "error", + { + "code": 100 + } + ] + } +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index e568f042..e649908c 100644 --- a/manifest.json +++ b/manifest.json @@ -10,16 +10,6 @@ "48": "icons/icon48.png", "128": "icons/icon128.png" }, - "background": { - "service_worker": "src/background.mjs", - "type": "module" - }, - "action": { - "default_title": "Unblock Youku isn't working properly! Visit http://uku.im/faq for solutions.", - "default_icon": "icons/icon19gray.png", - "default_popup": "src/popup.html" - }, - "options_page": "src/options.html", "permissions": [ "proxy", "declarativeNetRequestWithHostAccess", @@ -28,5 +18,65 @@ "host_permissions": [ "http://*/*", "https://*/*" + ], + "background": { + "service_worker": "src/background.mjs", + "type": "module" + }, + "action": { + "default_popup": "src/popup.html", + "default_icon": "icons/icon19gray.png", + "default_title": "Unblock Youku isn't working properly! Visit http://uku.im/faq for solutions." + }, + "options_page": "src/options.html", + "content_scripts": [ + { + "matches": [ + "http://www.tudou.com/*" + ], + "js": [ + "src/content_scripts/tudou.js" + ], + "run_at": "document_end", + "all_frames": true + }, + { + "matches": [ + "http://play.baidu.com/*" + ], + "css": [ + "src/content_scripts/play.baidu.css" + ], + "all_frames": true + }, + { + "matches": [ + "http://music.163.com/*" + ], + "js": [ + "src/content_scripts/music.163.js" + ], + "run_at": "document_end", + "all_frames": true + }, + { + "matches": [ + "http://unblockcn.com/*", + "https://unblockcn.com/*", + "http://*.unblockcn.com/*", + "https://*.unblockcn.com/*", + "http://unblockyouku.cn/*", + "http://*.unblockyouku.cn/*", + "http://724sky.com/*", + "http://*.724sky.com/*", + "http://tieba.baidu.com/f?kw=unblockcn*", + "http://*.tieba.baidu.com/f?kw=unblockcn*" + ], + "js": [ + "src/content_scripts/unblockcn.js" + ], + "run_at": "document_end", + "all_frames": true + } ] } diff --git a/src/content_scripts/music.163.js b/src/content_scripts/music.163.js index ed4d17b3..95cb6c4a 100644 --- a/src/content_scripts/music.163.js +++ b/src/content_scripts/music.163.js @@ -1,24 +1,6 @@ -/* - * Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ +'use strict'; -/*jslint browser: true */ -"use strict"; - -var s = document.createElement('script'); +const s = document.createElement('script'); s.type = 'text/javascript'; s.innerText = ' \ (function() { \ diff --git a/src/content_scripts/play.baidu.css b/src/content_scripts/play.baidu.css index 9e762f33..aebb0356 100644 --- a/src/content_scripts/play.baidu.css +++ b/src/content_scripts/play.baidu.css @@ -1,21 +1,3 @@ -/* - * Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - - div#foreignIP, div.ui-widget-overlay { display: none !important; } diff --git a/src/content_scripts/play.baidu.js b/src/content_scripts/play.baidu.js index 1008191c..49ae03be 100644 --- a/src/content_scripts/play.baidu.js +++ b/src/content_scripts/play.baidu.js @@ -1,47 +1,27 @@ -/* - * Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ +'use strict'; -/*jslint browser: true */ -"use strict"; - -var s = document.createElement('script'); +const s = document.createElement('script'); s.type = 'text/javascript'; s.innerText = ' \ - (function() { \ - var i, list = document.getElementsByTagName("script"); \ - for (i = 0; i < list.length; i++) { \ - list[i].innerHTML = list[i].innerHTML.replace(/isForeign[^;]*/gi, "isForeign = \\\"\\\""); \ - } \ - mbox.isForeign = ""; \ - document.body.classList.remove("foreignIP"); \ - }());'; + (function() { \ + var i, list = document.getElementsByTagName("script"); \ + for (i = 0; i < list.length; i++) { \ + list[i].innerHTML = list[i].innerHTML.replace(/isForeign[^;]*/gi, "isForeign = \\\"\\\""); \ + } \ + mbox.isForeign = ""; \ + document.body.classList.remove("foreignIP"); \ + }());'; -var target = null; -var i, list = document.getElementsByTagName("script"); +let target = null; +let i; const list = document.getElementsByTagName('script'); for (i = 0; i < list.length; i++) { - if (list[i].innerHTML.indexOf('isForeign') !== -1) { - target = list[i]; - break; - } -} + if (list[i].innerHTML.indexOf('isForeign') !== -1) { + target = list[i]; + break; + } +} if (target !== null) { - target.parentNode.insertBefore(s, target.nextSibling); + target.parentNode.insertBefore(s, target.nextSibling); } else { - document.body.appendChild(s); + document.body.appendChild(s); } -// console.log(target); -// console.log('load successfuly'); diff --git a/src/content_scripts/tudou.js b/src/content_scripts/tudou.js index a7432886..0e47c62f 100644 --- a/src/content_scripts/tudou.js +++ b/src/content_scripts/tudou.js @@ -1,24 +1,6 @@ -/* - * Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ +'use strict'; -/*jslint browser: true */ -"use strict"; - -var s = document.createElement('script'); +const s = document.createElement('script'); s.type = 'text/javascript'; s.innerText = ' \ (function() { \ diff --git a/src/content_scripts/unblockcn.js b/src/content_scripts/unblockcn.js index 276734f6..6a44b671 100644 --- a/src/content_scripts/unblockcn.js +++ b/src/content_scripts/unblockcn.js @@ -7,13 +7,18 @@ * - https://github.com/Unblocker/malicious-unblockcn */ -/*jslint browser: true */ -"use strict"; +'use strict'; -document.body.innerHTML = - '
' + +document.body.innerHTML = + '
' + '请各位 Unblock Youku 用户不要上当受骗,Unblock Youku 并没有改名为 UnblockCN。
' + - '1. UnblockCN 在 Unblock Youku 服务器被攻击的同一时间散播 Unblock Youku 改名的恶意谣言
' + - '2. UnblockCN 会劫持用户页面,强迫用户付费购买 VIP,并伪造 Unblock Youku 网站。
' + - '如果不幸被骗安装 UnblockCN 软件,请阅读这里的解决办法。' + + '1. UnblockCN 在 Unblock Youku 服务器' + + '被攻击的同一时间散播 Unblock Youku 改名的恶意谣言
' + + '2. UnblockCN 会劫持用户页面,强迫用户付费购买 VIP,' + + '并伪造 Unblock Youku 网站。
' + + '如果不幸被骗安装 UnblockCN 软件,请阅读' + + '这里的解决办法。' + '
' + document.body.innerHTML; diff --git a/src/modules/_proxy.mjs b/src/modules/_proxy.mjs index e7ef5a81..24a5eab4 100644 --- a/src/modules/_proxy.mjs +++ b/src/modules/_proxy.mjs @@ -15,12 +15,11 @@ async function setPacScript( console.log('To set up proxy...'); const pacScript = urls2pac( - PROXY_BYPASS_URLS, - PROXY_URLS, - defaultProxyAddress, defaultProxyProtocol, - backupProxyAddress, backupProxyProtocol); + PROXY_BYPASS_URLS, PROXY_URLS, + defaultProxyProtocol, defaultProxyAddress, + backupProxyProtocol, backupProxyAddress); - console.group('Printing PAC script content:'); + console.groupCollapsed('Printing PAC script content...'); console.log(pacScript); console.groupEnd(); diff --git a/src/modules/_url_utils.mjs b/src/modules/_url_utils.mjs index 5fd6ae81..5b2dcc01 100644 --- a/src/modules/_url_utils.mjs +++ b/src/modules/_url_utils.mjs @@ -3,36 +3,36 @@ */ -function _parse_url(url_str) { +function _parseUrl(urlStr) { let protocol = null; - if (url_str.startsWith('http://')) { - url_str = url_str.slice('http://'.length); + if (urlStr.startsWith('http://')) { + urlStr = urlStr.slice('http://'.length); protocol = 'http'; - } else if (url_str.startsWith('https://')) { - url_str = url_str.slice('https://'.length); + } else if (urlStr.startsWith('https://')) { + urlStr = urlStr.slice('https://'.length); protocol = 'https'; } else { console.error('URL does not start with http:// or https://'); return null; } - let path_idx = url_str.indexOf('/'); - if (path_idx < 0) { - path_idx = url_str.length; - url_str += '/'; + let pathIdx = urlStr.indexOf('/'); + if (pathIdx < 0) { + pathIdx = urlStr.length; + urlStr += '/'; } - const colon_idx = url_str.indexOf(':'); // the colon before the optional port number + const colonIdx = urlStr.indexOf(':'); // the colon before the optional port number - let sep_idx = path_idx; - if (colon_idx >= 0 && colon_idx < path_idx) { - sep_idx = colon_idx; + let sepIdx = pathIdx; + if (colonIdx >= 0 && colonIdx < pathIdx) { + sepIdx = colonIdx; } return { protocol: protocol, // the parameter in FindProxyForURL only doesn't contain port numbers - hostname: url_str.slice(0, sep_idx), - portpath: url_str.slice(sep_idx), + hostname: urlStr.slice(0, sepIdx), + portpath: urlStr.slice(sepIdx), }; } // console.log(_parse_url('http://test.com')); @@ -41,9 +41,8 @@ function _parse_url(url_str) { // console.log(_parse_url('http://test.com:123/path')); -function gen_url_map(protocol, white_ulist, proxy_ulist) { - 'use strict'; - const url_map = { +function genUrlMap(protocol, whiteUrlList, proxyUrlList) { + const urlMap = { white: { any: [], }, @@ -52,61 +51,61 @@ function gen_url_map(protocol, white_ulist, proxy_ulist) { }, }; - function stringify(map_obj) { - const white_map = map_obj.white; - const proxy_map = map_obj.proxy; + function stringify(mapObj) { + const whiteMap = mapObj.white; + const proxyMap = mapObj.proxy; - let res_str = [ + let resStr = [ '{', ' \'white\': {', ].join('\n') + '\n'; - res_str += stringify_patterns(white_map); + resStr += stringifyPatterns(whiteMap); - res_str += [ + resStr += [ ' },', ' \'proxy\': {', ].join('\n') + '\n'; - res_str += stringify_patterns(proxy_map); + resStr += stringifyPatterns(proxyMap); - res_str += [ + resStr += [ ' }', '}', ].join('\n'); - return res_str; + return resStr; } - function stringify_patterns(hostname_map) { - let res_str = ''; + function stringifyPatterns(hostnameMap) { + let resStr = ''; let i; let patterns = null; - for (const hostname in hostname_map) { - if (hostname_map.hasOwnProperty(hostname)) { - res_str += ' \'' + hostname + '\': ['; - patterns = hostname_map[hostname]; + for (const hostname in hostnameMap) { + if (hostnameMap.hasOwnProperty(hostname)) { + resStr += ' \'' + hostname + '\': ['; + patterns = hostnameMap[hostname]; if (patterns.length === 0) { - res_str += '],\n'; + resStr += '],\n'; } else { - res_str += '\n'; + resStr += '\n'; for (i = 0; i < patterns.length; i++) { - res_str += ' ' + patterns[i] + ',\n'; + resStr += ' ' + patterns[i] + ',\n'; } - res_str = res_str.slice(0, -2) + '\n'; // remove the last , - res_str += ' ],\n'; + resStr = resStr.slice(0, -2) + '\n'; // remove the last , + resStr += ' ],\n'; } } } - res_str = res_str.slice(0, -2) + '\n'; // remove the last , + resStr = resStr.slice(0, -2) + '\n'; // remove the last , - return res_str; + return resStr; } - function add_patterns(map_obj, ulist) { + function addPatterns(mapObj, ulist) { let i; let uobj; let hostname; let portpath; let key; let val; for (i = 0; i < ulist.length; i++) { - uobj = _parse_url(ulist[i]); + uobj = _parseUrl(ulist[i]); if (uobj === null) { console.error('Invalid URL pattern: ' + ulist[i]); continue; @@ -124,8 +123,8 @@ function gen_url_map(protocol, white_ulist, proxy_ulist) { val = hostname + portpath; // host:port/path } } else { - if (!map_obj.hasOwnProperty(hostname)) { - map_obj[hostname] = []; + if (!mapObj.hasOwnProperty(hostname)) { + mapObj[hostname] = []; } key = hostname; val = portpath; // only :port/path @@ -142,60 +141,59 @@ function gen_url_map(protocol, white_ulist, proxy_ulist) { val = val.slice(0, -5) + '/i'; } - map_obj[key].push(val); + mapObj[key].push(val); } // if } // for } - add_patterns(url_map.white, white_ulist); - add_patterns(url_map.proxy, proxy_ulist); + addPatterns(urlMap.white, whiteUrlList); + addPatterns(urlMap.proxy, proxyUrlList); // console.log(stringify(url_map)); - return stringify(url_map); + return stringify(urlMap); } -export function urls2pac(url_whitelist, url_list, - proxy_server_1, proxy_protocol_1, - proxy_server_2, proxy_protocol_2) { - 'use strict'; - const http_map_str = gen_url_map('http', url_whitelist, url_list); - const https_map_str = gen_url_map('https', url_whitelist, url_list); +export function urls2pac(urlWhitelist, urlList, + proxyProtocol1, proxyAddress1, + proxyProtocol2, proxyAddress2) { + const httpMapStr = genUrlMap('http', urlWhitelist, urlList); + const httpsMapStr = genUrlMap('https', urlWhitelist, urlList); - if (typeof proxy_protocol_1 === 'undefined') { - proxy_protocol_1 = 'PROXY'; + if (typeof proxyProtocol1 === 'undefined') { + proxyProtocol1 = 'PROXY'; } else { - proxy_protocol_1 = proxy_protocol_1.replace(/:/g, ''); - proxy_protocol_1 = proxy_protocol_1.replace(/\//g, ''); - proxy_protocol_1 = proxy_protocol_1.toUpperCase(); - if (proxy_protocol_1 === 'HTTP') { - proxy_protocol_1 = 'PROXY'; + proxyProtocol1 = proxyProtocol1.replace(/:/g, ''); + proxyProtocol1 = proxyProtocol1.replace(/\//g, ''); + proxyProtocol1 = proxyProtocol1.toUpperCase(); + if (proxyProtocol1 === 'HTTP') { + proxyProtocol1 = 'PROXY'; } } - let _proxy_str = proxy_protocol_1 + ' ' + proxy_server_1 + '; '; + let _proxyStr = proxyProtocol1 + ' ' + proxyAddress1 + '; '; - if (typeof proxy_server_2 !== 'undefined') { - if (typeof proxy_protocol_2 === 'undefined') { - proxy_protocol_2 = 'PROXY'; + if (typeof proxyAddress2 !== 'undefined') { + if (typeof proxyProtocol2 === 'undefined') { + proxyProtocol2 = 'PROXY'; } - proxy_protocol_2 = proxy_protocol_2.replace(/:/g, ''); - proxy_protocol_2 = proxy_protocol_2.replace(/\//g, ''); - proxy_protocol_2 = proxy_protocol_2.toUpperCase(); - if (proxy_protocol_2 === 'HTTP') { - proxy_protocol_2 = 'PROXY'; + proxyProtocol2 = proxyProtocol2.replace(/:/g, ''); + proxyProtocol2 = proxyProtocol2.replace(/\//g, ''); + proxyProtocol2 = proxyProtocol2.toUpperCase(); + if (proxyProtocol2 === 'HTTP') { + proxyProtocol2 = 'PROXY'; } - _proxy_str += proxy_protocol_2 + ' ' + proxy_server_2 + '; '; + _proxyStr += proxyProtocol2 + ' ' + proxyAddress2 + '; '; } - _proxy_str += 'DIRECT;'; + _proxyStr += 'DIRECT;'; return [ - 'var _http_map = ' + http_map_str + ';', - 'var _https_map = ' + https_map_str + ';', - 'var _proxy_str = \'' + _proxy_str + '\';', + 'var _http_map = ' + httpMapStr + ';', + 'var _https_map = ' + httpsMapStr + ';', + 'var _proxy_str = \'' + _proxyStr + '\';', '', 'function _check_regex_list(regex_list, str) {', ' if (str.slice(0, 4) === \':80/\')', diff --git a/src/modules/settings.mjs b/src/modules/settings.mjs index a64e21cd..39a18e9c 100644 --- a/src/modules/settings.mjs +++ b/src/modules/settings.mjs @@ -47,16 +47,17 @@ async function applyModeSettings(mode) { const customProxy = await getCustomProxy(); if (typeof customProxy === 'undefined' || - typeof customProxy.protocol === 'undefined' || - typeof customProxy.address === 'undefined') { + typeof customProxy.proc === 'undefined' || + typeof customProxy.addr === 'undefined') { await Proxy.setDefaultProxy(); } else { - await Proxy.setCustomProxy(customProxy.protocol, customProxy.address); + await Proxy.setCustomProxy(customProxy.proc, customProxy.addr); } // TODO: Set header modifier Icon.setIcon(Modes.NORMAL); } + // ================================================================ // Function called by background.js: // * reloadCurrentSettings() @@ -101,11 +102,10 @@ export async function setNewMode(mode) { // 1. Remove the custom proxy server configs from storage // 2. Change mode to use proxy, and apply settings with the default proxy server - export async function setCustomProxy(customProxyProtocol, customProxyAddress) { await Storage.setItem(CUSTOM_PROXY_STORAGE_KEY, { - protocol: customProxyProtocol, - address: customProxyAddress, + proc: customProxyProtocol, + addr: customProxyAddress, }); await setNewMode(Modes.NORMAL); // Change the mode to use the proxy right away console.log( diff --git a/src/options.mjs b/src/options.mjs index bbd1e1b4..d11af113 100644 --- a/src/options.mjs +++ b/src/options.mjs @@ -1,5 +1,5 @@ import * as Settings from './modules/settings.mjs'; -import {DEFAULT_PROXY_PROTOCOL, DEFAULT_PROXY_ADDRESS} from '../configs/servers.mjs'; +import {DEFAULT_PROXY_PROTOCOL, DEFAULT_PROXY_ADDRESS} from './configs/servers.mjs'; function showProxyMessage(type, content) { @@ -15,94 +15,116 @@ function showProxyMessage(type, content) { '
'); } +function showProxyError(content) { + $('#proxy_message').html( + '
' + + '' + + content + + '
'); +} -function getCustomProxyServer(callback) { - background.get_storage('custom_proxy_server', function(serverInfo) { - if (typeof serverInfo === 'undefined' || - typeof serverInfo.proc === 'undefined' || - typeof serverInfo.addr === 'undefined') { - callback(/* customEnabled= */ false, DEFAULT_PROXY_PROTOCOL, DEFAULT_PROXY_ADDRESS); - } else { - callback(/* customEnabled= */ true, serverInfo.proc, serverInfo.addr); +function fillProxyInfoText(protocol, address) { + // 1. Select the proxy protocol from the dropdown + $('#custom_proxy_proc option').each(function(_, d) { + if (d.value === protocol) { + d.selected = true; } }); + // 2. Fill the proxy address + $('#custom_proxy_addr').val(address); } -$(document).ready(function() { - getCustomProxyServer(function(customEnabled, serverProtocol, serverAddress) { - $('#custom_proxy_proc option').each(function(idx, d) { - if (d.value === serverProtocol) { - d.selected = true; - } - }); - $('#custom_proxy_addr').val(serverAddress); - if (customEnabled === true) { - $('#custom_proxy_proc').attr('disabled', true); - $('#custom_proxy_addr').attr('disabled', true); - $('#custom_proxy_enable').attr('disabled', true); - background.get_mode_name(function(currentMode) { - if (currentMode === 'normal') { - showProxyMessage('info', 'Status: Enabled'); - } else { - showProxyMessage('warning', 'Status: Enabled. But current mode is not proxy mode.'); - } - }); - } else { - showProxyMessage('info', 'Status: NOT Enabled'); - $('#custom_proxy_reset').attr('disabled', true); - } - }); +Settings.getCustomProxy().then((serverInfo) => { + let customEnabled = false; + let serverProtocol = DEFAULT_PROXY_PROTOCOL; + let serverAddress = DEFAULT_PROXY_ADDRESS; + if (typeof serverInfo !== 'undefined' && + typeof serverInfo.proc !== 'undefined' && + typeof serverInfo.addr !== 'undefined') { + customEnabled = true; + serverProtocol = serverInfo.proc; + serverAddress = serverInfo.addr; + } - $('#custom_proxy_enable').click(function() { - const customProxyProtocol = $('#custom_proxy_proc').val(); - const customProxyAddress = $('#custom_proxy_addr').val(); + // Set UI texts, the button status, and info message + fillProxyInfoText(serverProtocol, serverAddress); + if (customEnabled === true) { $('#custom_proxy_proc').attr('disabled', true); $('#custom_proxy_addr').attr('disabled', true); $('#custom_proxy_enable').attr('disabled', true); - set_custom_proxy_server(customProxyProtocol, customProxyAddress, function() { - // switch to proxy mode and force refresh pac proxy - background.get_mode_name(function(currentMode) { - if (currentMode === 'normal') { - background.setup_proxy(); - $('#custom_proxy_reset').attr('disabled', false); - showProxyMessage('info', 'Enabled custom proxy server.'); - } else { - background.set_mode_name('normal', function() { - $('#custom_proxy_reset').attr('disabled', false); - showProxyMessage( - 'warning', 'Enabled custom proxy server, and changed to proxy mode.'); - }); - } - }); - }); - }); - $('#custom_proxy_reset').click(function() { - $('#custom_proxy_reset').attr('disabled', true); - remove_custom_proxy_server(function() { - // switch to proxy mode and force refresh pac proxy - background.get_mode_name(function(currentMode) { - if (currentMode === 'normal') { - background.setup_proxy(); - $('#custom_proxy_proc').attr('disabled', false); - $('#custom_proxy_addr').attr('disabled', false); - $('#custom_proxy_enable').attr('disabled', false); - showProxyMessage('warning', 'Reset custom proxy server.'); - } else { - background.set_mode_name('normal', function() { - $('#custom_proxy_proc').attr('disabled', false); - $('#custom_proxy_addr').attr('disabled', false); - $('#custom_proxy_enable').attr('disabled', false); - showProxyMessage('warning', 'Reset custom proxy server, and changed to proxy mode.'); - }); - } - }); + Settings.getCurrentMode().then((currentMode) => { + if (currentMode === 'normal') { + showProxyMessage('info', 'Status: Enabled'); + } else { + showProxyMessage('warning', 'Status: Enabled. But the current mode does not use proxy'); + } }); + } else { + $('#custom_proxy_reset').attr('disabled', true); + + showProxyMessage('info', 'Status: NOT Enabled. No custom proxy server is set'); + } +}); + + +// Set up actions for the "Enable" button +$('#custom_proxy_enable').click(function() { + console.group('Clicked on the button to enable custom proxy...'); + + const customProxyProtocol = $('#custom_proxy_proc').val(); + const customProxyAddress = $('#custom_proxy_addr').val(); + // Some sanity checks for the entered proxy address + if (typeof customProxyAddress === 'undefined' || customProxyAddress.length < 8) { + showProxyError('Please enter a valid proxy address'); + console.log('Entered invalid proxy address: ' + customProxyAddress); + console.groupEnd(); + return; + } + if (customProxyAddress === DEFAULT_PROXY_ADDRESS) { + showProxyError('Cannot set the custom proxy server as the built-in default one'); + console.log('Attempted to set the custom proxy server to the built-in default one'); + console.groupEnd(); + return; + } + + $('#custom_proxy_proc').attr('disabled', true); + $('#custom_proxy_addr').attr('disabled', true); + $('#custom_proxy_enable').attr('disabled', true); + + // Set custom proxy, switch to proxy mode, and force refresh the PAC script + Settings.setCustomProxy(customProxyProtocol, customProxyAddress).then(() => { + $('#custom_proxy_reset').attr('disabled', false); + showProxyMessage( + 'warning', 'Set the custom proxy server, and changed mode to use proxy'); + + console.log('Successfully set the custom proxy server to ' + + customProxyProtocol + ' ' + customProxyAddress); + console.groupEnd(); }); +}); + + +// Set up actions for the "Reset" button +$('#custom_proxy_reset').click(function() { + console.group('Clicked on the button to reset custom proxy...'); - $('#form_custom_proxy_server').submit(function(event) { - // prevent the default action of submitting a form - event.preventDefault(); + $('#custom_proxy_reset').attr('disabled', true); + Settings.clearCustomProxy().then(() => { + fillProxyInfoText(DEFAULT_PROXY_PROTOCOL, DEFAULT_PROXY_ADDRESS); + $('#custom_proxy_proc').attr('disabled', false); + $('#custom_proxy_addr').attr('disabled', false); + $('#custom_proxy_enable').attr('disabled', false); + showProxyMessage('warning', 'Reset custom proxy server, and changed mode to use proxy'); + + console.log('Successfully reset the custom proxy server'); + console.groupEnd(); }); }); + + +// Prevent the default action of submitting a form +$('#form_custom_proxy_server').submit(function(event) { + event.preventDefault(); +}); diff --git a/src/popup.mjs b/src/popup.mjs index 3aef4e38..40be35c3 100644 --- a/src/popup.mjs +++ b/src/popup.mjs @@ -3,7 +3,8 @@ import * as Settings from './modules/settings.mjs'; import * as Icon from './modules/_icon.mjs'; -function setTranslatedTexts() { +// Set the translated texts for the options page +(function() { const getMsg = chrome.i18n.getMessage; $('div#support strong').html(getMsg('support_title')); @@ -23,40 +24,39 @@ function setTranslatedTexts() { $('div#faq').html(getMsg('faq')); $('div#feedback').html(getMsg('feedback')); $('div#rating').html(getMsg('rating')); -} - - -$(document).ready(function() { - setTranslatedTexts(); - - // Set default button display - Settings.getCurrentMode().then((mode) => { - switch (mode) { - case Modes.OFF: - $('label#off').addClass('active'); - break; - default: - $('label#normal').addClass('active'); - break; - } - }); - - // Add version number to the footer - $('div#version small').html('Unblock Youku v' + chrome.runtime.getManifest().version); - // Clear the text on the browser icon after the user has clicked on the icon - Icon.clearIconText(); - - // Set up button actions - $('input#input_off').change(function() { - console.group('Clicked on the button to change the mode to OFF...'); - Settings.setNewMode(Modes.OFF).then(console.groupEnd); - }); - $('input#input_normal').change(function() { - console.group('Clicked on the button to change the mode to NORMAL...'); - Settings.setNewMode(Modes.NORMAL).then(console.groupEnd); - }); - - // Enable tooltip - $('#tooltip').tooltip(); +})(); + + +// Preselect the default button +Settings.getCurrentMode().then((mode) => { + switch (mode) { + case Modes.OFF: + $('label#off').addClass('active'); + break; + default: + $('label#normal').addClass('active'); + break; + } }); + +// Add version number to the footer +$('div#version small').html('Unblock Youku v' + chrome.runtime.getManifest().version); +// Clear the text on the browser icon after the user has clicked on the icon +Icon.clearIconText(); + + +// Set up button actions +$('input#input_off').change(function() { + console.group('Clicked on the button to change the mode to OFF...'); + Settings.setNewMode(Modes.OFF).then(console.groupEnd); +}); +$('input#input_normal').change(function() { + console.group('Clicked on the button to change the mode to NORMAL...'); + Settings.setNewMode(Modes.NORMAL).then(console.groupEnd); +}); + + +// Enable tooltip +$('#tooltip').tooltip(); +