From adc0c8025aae16b223ef4faacdcc20f3c35e62d3 Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 30 Oct 2017 01:07:08 +0800 Subject: [PATCH] Fix: lazy load regexp tester --- edit/regexp-tester.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/edit/regexp-tester.js b/edit/regexp-tester.js index ee89d3a3..60542552 100644 --- a/edit/regexp-tester.js +++ b/edit/regexp-tester.js @@ -8,21 +8,38 @@ var regExpTester = (function () { const OWN_ICON = chrome.runtime.getManifest().icons['16']; const cachedRegexps = new Map(); 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) { update(); } - }); + } function isShowed() { - return $('.regexp-report'); + return Boolean($('.regexp-report')); } function toggle(state = !isShowed()) { if (state && !isShowed()) { + if (!isInit) { + init(); + } showHelp('', $element({className: 'regexp-report'})); } else if (!state && isShowed()) { + if (isInit) { + uninit(); + } // TODO: need a closeHelp function $('#help-popup .dismiss').onclick(); } @@ -30,6 +47,9 @@ var regExpTester = (function () { function update(newRegexps) { if (!isShowed()) { + if (isInit) { + uninit(); + } return; } if (newRegexps) {