Fix: lazy load regexp tester

This commit is contained in:
eight 2017-10-30 01:07:08 +08:00
parent 71013623c6
commit adc0c8025a

View File

@ -8,21 +8,38 @@ var regExpTester = (function () {
const OWN_ICON = chrome.runtime.getManifest().icons['16']; const OWN_ICON = chrome.runtime.getManifest().icons['16'];
const cachedRegexps = new Map(); const cachedRegexps = new Map();
let currentRegexps = []; let currentRegexps = [];
let isInit = false;
chrome.tabs.onUpdated.addListener(function _(tabId, info) { function init() {
isInit = true;
chrome.tabs.onUpdated.addListener(onTabUpdate);
}
function uninit() {
chrome.tabs.onUpdated.removeListener(onTabUpdate);
isInit = false;
}
function onTabUpdate(tabId, info) {
if (info.url) { if (info.url) {
update(); update();
} }
}); }
function isShowed() { function isShowed() {
return $('.regexp-report'); return Boolean($('.regexp-report'));
} }
function toggle(state = !isShowed()) { function toggle(state = !isShowed()) {
if (state && !isShowed()) { if (state && !isShowed()) {
if (!isInit) {
init();
}
showHelp('', $element({className: 'regexp-report'})); showHelp('', $element({className: 'regexp-report'}));
} else if (!state && isShowed()) { } else if (!state && isShowed()) {
if (isInit) {
uninit();
}
// TODO: need a closeHelp function // TODO: need a closeHelp function
$('#help-popup .dismiss').onclick(); $('#help-popup .dismiss').onclick();
} }
@ -30,6 +47,9 @@ var regExpTester = (function () {
function update(newRegexps) { function update(newRegexps) {
if (!isShowed()) { if (!isShowed()) {
if (isInit) {
uninit();
}
return; return;
} }
if (newRegexps) { if (newRegexps) {