Feat(fixit-decryptor): add reset event and hook (#123)

This commit is contained in:
Cell
2022-05-27 21:19:00 +08:00
parent c855841130
commit 6a8f52e5e2
4 changed files with 40 additions and 13 deletions
+30 -9
View File
@@ -2,6 +2,7 @@
* FixIt decryptor for encrypted pages
* @param {Object} options
* @param {Function} [options.decrypted] handler after decrypting
* @param {Function} [options.reset] handler after encrypting again
* @param {Number} [options.duration=86400] number of seconds to cache decryption statistics. unit: s
* @author @Lruihao https://lruihao.cn
* @since v0.2.15
@@ -11,6 +12,7 @@ FixItDecryptor = function (options = {}) {
this.options = options || {};
this.options.duration = this.options.duration || 24 * 60 * 60; // default cache one day
this.decryptedEventSet = new Set();
this.resetEventSet = new Set();
this.$el = document.querySelector('.fixit-decryptor-container');
/**
@@ -25,25 +27,24 @@ FixItDecryptor = function (options = {}) {
'afterbegin',
CryptoJS.enc.Base64.parse(base64EncodeContent).toString(CryptoJS.enc.Utf8)
);
// decrypted hook
for (const event of this.decryptedEventSet) {
event();
}
} catch (err) {
alert(err);
return console.error(err);
}
// decrypted hook
for (const event of this.decryptedEventSet) {
event();
}
};
/**
* initialize FixIt decryptor
*/
_proto.init = () => {
const _decryptor = this;
this.options.decrypted && this.addEventListener('decrypted', this.options.decrypted);
this.addEventListener('decrypted', this.options?.decrypted);
this.addEventListener('reset', this.options?.reset);
this.validateCache();
const _decryptor = this;
this.$el.querySelector('#fixit-decryptor-input')?.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
e.preventDefault();
@@ -86,6 +87,10 @@ FixItDecryptor = function (options = {}) {
_decryptor.$el
);
window.localStorage.removeItem(`fixit-decryptor/#${location.pathname}`);
// reset hook
for (const event of _decryptor.resetEventSet) {
event();
}
});
};
@@ -112,31 +117,47 @@ FixItDecryptor = function (options = {}) {
* add event listener for FixIt Decryptor
* @param {String} event
* @param {Function} listener event handler
* @returns {FixItDecryptor}
*/
_proto.addEventListener = (event, listener) => {
if (typeof listener !== 'function') {
return this;
}
switch (event) {
case 'decrypted':
this.decryptedEventSet.add(listener);
break;
case 'reset':
this.resetEventSet.add(listener);
break;
default:
console.warn(`Event ${event} not found in FixIt Decryptor!`);
break;
}
return this;
};
/**
* remove event listener for FixIt Decryptor
* @param {String} event
* @param {Function} listener event handler
* @returns {FixItDecryptor}
*/
_proto.removeEventListener = (event, listener) => {
if (typeof listener !== 'function') {
return this;
}
switch (event) {
case 'decrypted':
this.decryptedEventSet.delete(listener);
break;
case 'reset':
this.resetEventSet.delete(listener);
break;
default:
console.warn(`Event ${event} not found in FixIt Decryptor!`);
break;
}
return this;
};
};
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+8 -2
View File
@@ -900,6 +900,7 @@ class FixIt {
}
initFixItDecryptor() {
const $tocNodes = document.querySelectorAll('#toc-auto>.d-none, #toc-static.d-none');
this.decryptor = new FixItDecryptor({
decrypted: () => {
this.initTwemoji();
@@ -912,13 +913,18 @@ class FixIt {
this.initMermaid();
this.initEcharts();
this.initTypeit();
this.initMapbox();
this.util.forEach(document.querySelectorAll('#toc-auto>.d-none, #toc-static.d-none'), ($element) => {
// this.initMapbox();
this.util.forEach($tocNodes, ($element) => {
$element.classList.remove('d-none');
});
this.initToc();
this.initTocListener();
this.initPangu();
},
reset: () => {
this.util.forEach($tocNodes, ($element) => {
$element.classList.add('d-none');
});
}
});
this.decryptor.init();