Skip to content

Commit

Permalink
small bug fixes and performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhuor committed Jul 3, 2022
1 parent 0212c9e commit adb617d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 43 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"code": 100
}
]
}
},
"ignorePatterns": [
"**/third_party/**"
]
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ Update URL and server configs under the folder `src/configs/`.

Run `npm ci` to install dependencies exactly as they are listed in the package-lock.json file.

Run `npm run test` to create a zip file for uploading and testing.
Run `npm run lint` to check the coding style of all source code.

Run `npm run test` to _create a zip file for uploading to Chrome Web Store_ and run tests against it.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"npm": "8.x"
},
"scripts": {
"lint": "eslint ./ --ext .js,.mjs,.ts,.tsx",
"clean": "rm -rf dist",
"test": "npm run clean && node tools/create_zip.js && unzip -q dist/upload_to_chrome_store.zip -d dist/unzipped_chrome_extension && jest --verbose"
},
Expand Down
2 changes: 1 addition & 1 deletion src/modules/_icon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function setIcon(mode) {
}

if (isSpring()) {
chrome.action.setIcon({path: ICON_RELATIVE_PATH + 'icon19xmas.png'});
chrome.action.setIcon({path: ICON_RELATIVE_PATH + 'icon19spring.png'});
chrome.action.setTitle(
{title: 'Happy Spring Festival! (Unblock Youku ' +
chrome.runtime.getManifest().version + ')'});
Expand Down
32 changes: 18 additions & 14 deletions src/modules/settings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,29 @@ export function getCustomProxy() {
// Note:
// * The function should be idempotent so it can be called multiple times.
// * Make sure a Promise is returned so that the caller can wait for it.
async function applyModeSettings(mode) {
function applyModeSettings(mode) {
if (mode === Modes.OFF) {
return Promise.all([
Proxy.clearProxy(),
Header.clearHeaderModifier(),
Icon.setIcon(Modes.OFF),
]);
} else {
// 1. Set proxy
const customProxy = await getCustomProxy();
if (typeof customProxy === 'undefined' ||
const setProxy = async () => {
const customProxy = await getCustomProxy();
if (typeof customProxy === 'undefined' ||
typeof customProxy.proc === 'undefined' ||
typeof customProxy.addr === 'undefined') {
await Proxy.setDefaultProxy();
} else {
await Proxy.setCustomProxy(customProxy.proc, customProxy.addr);
}
// 2. Set header modifier
await Header.setHeaderModifier();
// 3. Set icon
Icon.setIcon(Modes.NORMAL);
return Proxy.setDefaultProxy();
} else {
return Proxy.setCustomProxy(customProxy.proc, customProxy.addr);
}
};
return Promise.all([
setProxy(),
Header.setHeaderModifier(),
Icon.setIcon(Modes.NORMAL),
]);
}
}

Expand All @@ -78,8 +80,10 @@ export async function loadCurrentSettings() {
// 2. Save the new mode into storage

export async function setNewMode(mode) {
await applyModeSettings(mode);
await Storage.setItem(MODE_STORAGE_KEY, mode);
await Promise.all([
applyModeSettings(mode),
Storage.setItem(MODE_STORAGE_KEY, mode),
]);

// Set this one-time text when the user changes the mode
if (mode == Modes.OFF) {
Expand Down
2 changes: 2 additions & 0 deletions src/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ $('#custom_proxy_enable').click(function() {
'warning', 'Set the custom proxy server, and changed mode to use proxy');

console.groupEnd();
console.log('Finished setting the custom proxy');
});
});

Expand All @@ -119,6 +120,7 @@ $('#custom_proxy_reset').click(function() {
showProxyMessage('warning', 'Reset custom proxy server, and changed mode to use proxy');

console.groupEnd();
console.log('Finished clearing the custom proxy');
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/popup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ Icon.clearIconText();
$('input#input_off').change(function() {
console.group('Clicked on the button to change the mode to OFF...');
Settings.setNewMode(Modes.OFF).then(() => {
console.log('Finished changing the mode to OFF');
console.groupEnd();
console.log('Finished changing the mode to OFF');
});
});
$('input#input_normal').change(function() {
console.group('Clicked on the button to change the mode to NORMAL...');
Settings.setNewMode(Modes.NORMAL).then(() => {
console.log('Finished changing the mode to NORMAL');
console.groupEnd();
console.log('Finished changing the mode to NORMAL');
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/service_worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as Settings from './modules/settings.mjs';
function initializeExtension() {
console.group('To intialize the extension...');
Settings.loadCurrentSettings().then(() => {
console.log('Finished initializing the chrome extension');
console.groupEnd();
console.log('Finished initializing the chrome extension');
});
}

Expand Down
2 changes: 1 addition & 1 deletion tools/create_zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function getAllFilesFromList(folder, fileList) {
if (shouldBeZipped(filePath)) {
result.push(filePath);
} else {
console.log('Excluding file: ' + filePath);
console.log('Excluding file from zipping: ' + filePath);
}
} else {
result.push(...getAllFilesFromList(filePath, fs.readdirSync(filePath)));
Expand Down
23 changes: 1 addition & 22 deletions tools/produce_regex.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import {PROXY_URLS} from '../src/configs/urls.mjs';
import {produceSquidRegexList} from './_regex_utils.mjs';

const TEST_URL_LIST = [
'http://*/*',
'https://*/*',
'http://*.video.qq.com/*',
'https://*.video.qq.com/*',
'http://vd.l.qq.com/*',
'https://vd.l.qq.com/*',
'http://example.com',
'https://example.com',
'http://example.com/',
'https://example.com/',
'http://*.example.com/*',
'https://*.example.com/*',
'http://*.example.com/path.json?aaa=bbb',
'https://*.example.com/path.json?aaa=bbb',
'http://*.example.com/path.json?aaa=bbb*',
'https://*.example.com/path.json?aaa=bbb*',
'http://122.72.82.31/*',
];

// console.log(produceSquidRegexList(PROXY_URLS).join('\n') + '\n');
console.log(produceSquidRegexList(TEST_URL_LIST).join('\n') + '\n');
console.log(produceSquidRegexList(PROXY_URLS).join('\n') + '\n');
process.exit(0);

0 comments on commit adb617d

Please sign in to comment.