|
764 | 764 | if ( selector === '' || selector === '{{2}}' ) {
|
765 | 765 | selector = '.' + tokens.map(a => CSS.escape(a)).join(',.');
|
766 | 766 | }
|
| 767 | + let behavior = '{{3}}'; |
| 768 | + let timer; |
767 | 769 | const rmclass = function() {
|
| 770 | + timer = undefined; |
768 | 771 | try {
|
769 | 772 | const nodes = document.querySelectorAll(selector);
|
770 | 773 | for ( const node of nodes ) {
|
|
773 | 776 | } catch(ex) {
|
774 | 777 | }
|
775 | 778 | };
|
776 |
| - if ( document.readyState === 'loading' ) { |
777 |
| - window.addEventListener( |
778 |
| - 'DOMContentLoaded', |
779 |
| - rmclass, |
780 |
| - { capture: true, once: true } |
781 |
| - ); |
782 |
| - } else { |
| 779 | + const mutationHandler = mutations => { |
| 780 | + if ( timer !== undefined ) { return; } |
| 781 | + let skip = true; |
| 782 | + for ( let i = 0; i < mutations.length && skip; i++ ) { |
| 783 | + const { type, addedNodes, removedNodes } = mutations[i]; |
| 784 | + if ( type === 'attributes' ) { skip = false; } |
| 785 | + for ( let j = 0; j < addedNodes.length && skip; j++ ) { |
| 786 | + if ( addedNodes[j].nodeType === 1 ) { skip = false; break; } |
| 787 | + } |
| 788 | + for ( let j = 0; j < removedNodes.length && skip; j++ ) { |
| 789 | + if ( removedNodes[j].nodeType === 1 ) { skip = false; break; } |
| 790 | + } |
| 791 | + } |
| 792 | + if ( skip ) { return; } |
| 793 | + timer = self.requestIdleCallback(rmclass, { timeout: 67 }); |
| 794 | + }; |
| 795 | + const start = ( ) => { |
783 | 796 | rmclass();
|
| 797 | + if ( /\bstay\b/.test(behavior) === false ) { return; } |
| 798 | + const observer = new MutationObserver(mutationHandler); |
| 799 | + observer.observe(document.documentElement, { |
| 800 | + attributes: true, |
| 801 | + attributeFilter: [ 'class' ], |
| 802 | + childList: true, |
| 803 | + subtree: true, |
| 804 | + }); |
| 805 | + }; |
| 806 | + if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) { |
| 807 | + self.addEventListener('load', start, { once: true }); |
| 808 | + } else if ( document.readyState === 'loading' ) { |
| 809 | + self.addEventListener('DOMContentLoaded', start, { once: true }); |
| 810 | + } else { |
| 811 | + start(); |
784 | 812 | }
|
785 | 813 | })();
|
786 | 814 |
|
|
0 commit comments