|
1 | | -/* GitHub mutations observer library script v0.4.4 |
| 1 | +/* GitHub mutations observer library script v0.6.0 |
2 | 2 | * Detect changes to various elements and trigger an event |
3 | 3 | * This script is meant to be used as a library for GitHub-based userscripts |
4 | | - * Copyright © 2021 Rob Garrison |
| 4 | + * Copyright © 2022 Rob Garrison |
5 | 5 | * License: MIT |
6 | 6 | */ |
7 | 7 | (() => { |
8 | 8 | "use strict"; |
9 | 9 |
|
10 | 10 | // prefix for event & document body class name, e.g. "ghmo:container" |
11 | | - const prefix = "ghmo", |
12 | | - disableAttr = `data-${prefix}-disable`, |
13 | | - debounceInterval = 200, |
14 | | - targets = { |
15 | | - // pjax container (covers general, repo & gists) |
16 | | - // .news = newsfeed layout |
17 | | - // .repository-content = file code (code folding) |
18 | | - "[data-pjax-container], .news, .repository-content": { |
19 | | - count: 0, |
20 | | - name: "container" |
21 | | - }, |
22 | | - // comment preview active |
23 | | - ".js-preview-body": { |
24 | | - count: 0, |
25 | | - name: "preview" |
26 | | - }, |
27 | | - // .js-discussion = wrapper for progressively loaded comments; |
28 | | - // "# items not shown" example: https://github.com/isaacs/github/issues/18 |
29 | | - // .discussion-item = issue status changed (github-issue-show-status) |
30 | | - // #progressive-timeline-item-container = load hidden items (old?) |
31 | | - // #js-progressive-timeline-item-container = load hidden items |
32 | | - ".js-discussion, .discussion-item, .toolbar-item, #progressive-timeline-item-container, #js-progressive-timeline-item-container": { |
33 | | - count: 0, |
34 | | - name: "comments" |
35 | | - }, |
36 | | - // progressively loaded content (diff files) |
37 | | - ".js-diff-progressive-container, .data.blob-wrapper, .js-diff-load-container, .diff-table tbody": { |
38 | | - count: 0, |
39 | | - name: "diff" |
40 | | - }, |
41 | | - // issues/pr sidebar & timeline sections: e.g. form actions, commit |
42 | | - // references, deployment state & PR checks container |
43 | | - ".js-updatable-content, .js-updatable-content-preserve-scroll-position": { |
44 | | - count: 0, |
45 | | - name: "updatable" |
46 | | - }, |
47 | | - // user profile menu (loads on hover) |
48 | | - "details-menu": { |
49 | | - count: 0, |
50 | | - name: "menu" |
51 | | - } |
| 11 | + const prefix = "ghmo"; |
| 12 | + const disableAttr = `data-${prefix}-disable`; |
| 13 | + const debounceInterval = 250; |
| 14 | + const targets = { |
| 15 | + // pjax container (covers general, repo & gists) |
| 16 | + // .news = newsfeed layout |
| 17 | + // .repository-content = file code (code folding) |
| 18 | + // body = global changes |
| 19 | + "[data-pjax-container], .news, .repository-content, body": { |
| 20 | + count: 0, |
| 21 | + name: "container" |
| 22 | + }, |
| 23 | + // comment preview active |
| 24 | + ".js-preview-body": { |
| 25 | + count: 0, |
| 26 | + name: "preview" |
| 27 | + }, |
| 28 | + // .js-discussion = wrapper for progressively loaded comments; |
| 29 | + // "# items not shown" example: https://github.com/isaacs/github/issues/18 |
| 30 | + // .discussion-item = issue status changed (github-issue-show-status) |
| 31 | + // #progressive-timeline-item-container = load hidden items (old?) |
| 32 | + // #js-progressive-timeline-item-container = load hidden items |
| 33 | + // markdown-toolbar = issue comments |
| 34 | + ".js-discussion, .discussion-item, .toolbar-item, #progressive-timeline-item-container, #js-progressive-timeline-item-container, markdown-toolbar": { |
| 35 | + count: 0, |
| 36 | + name: "comments" |
| 37 | + }, |
| 38 | + // progressively loaded content (diff files) |
| 39 | + ".js-diff-progressive-container, .data.blob-wrapper, .js-diff-load-container, .diff-table tbody, .diff-progressive-loader": { |
| 40 | + count: 0, |
| 41 | + name: "diff" |
52 | 42 | }, |
53 | | - list = Object.keys(targets); |
| 43 | + // issues/pr sidebar & timeline sections: e.g. form actions, commit |
| 44 | + // references, deployment state & PR checks container |
| 45 | + ".js-updatable-content, .js-updatable-content-preserve-scroll-position": { |
| 46 | + count: 0, |
| 47 | + name: "updatable" |
| 48 | + }, |
| 49 | + // user profile menu (loads on hover) |
| 50 | + "details-menu": { |
| 51 | + count: 0, |
| 52 | + name: "menu" |
| 53 | + } |
| 54 | + }; |
| 55 | + const list = Object.keys(targets); |
| 56 | + let queue = new Set(); |
| 57 | + let timer; |
54 | 58 |
|
55 | 59 | function fireEvents() { |
56 | 60 | list.forEach(selector => { |
57 | 61 | if (targets[selector].count > 0) { |
58 | 62 | // event => "ghmo:container", "ghmo:comments" |
59 | 63 | const event = new Event(prefix + ":" + targets[selector].name); |
60 | 64 | document.dispatchEvent(event); |
| 65 | + targets[selector].count = 0; |
61 | 66 | } |
62 | | - targets[selector].count = 0; |
63 | 67 | }); |
64 | 68 | } |
65 | 69 |
|
| 70 | + function process() { |
| 71 | + clearTimeout(timer); |
| 72 | + // avoiding use of forEach loops for performance reasons |
| 73 | + for (const target of queue) { |
| 74 | + if (target) { |
| 75 | + for (const item of list) { |
| 76 | + if (target.matches(item)) { |
| 77 | + targets[item].count++; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + timer = setTimeout(() => fireEvents(), debounceInterval); |
| 83 | + queue.clear(); |
| 84 | + } |
| 85 | + |
66 | 86 | function init() { |
67 | 87 | // prevent error when library is loaded at document-start & no body exists |
68 | 88 | const container = document.querySelector("body"); |
69 | | - let timer; |
| 89 | + /* document.body attribute used to disable updates; it *should not* |
| 90 | + * be used regularly as multiple scripts may enable or disable the |
| 91 | + * observers at inappropriate times. It is best that each script handles |
| 92 | + * the mutation events triggered by this library on its own |
| 93 | + */ |
| 94 | + if (container.getAttribute(disableAttr)) { |
| 95 | + return; |
| 96 | + } |
70 | 97 | // prevent script from installing more than once |
71 | 98 | if (container && !container.classList.contains(prefix + "-enabled")) { |
72 | 99 | container.classList.add(prefix + "-enabled"); |
73 | | - // bound to document.body... this may be bad for performance |
74 | 100 | // http://stackoverflow.com/a/39332340/145346 |
75 | 101 | new MutationObserver(mutations => { |
76 | | - clearTimeout(timer); |
77 | | - /* document.body attribute used to disable updates; it *should not* |
78 | | - * be used regularly as multiple scripts may enable or disable the |
79 | | - * observers at inappropriate times. It is best that each script handles |
80 | | - * the mutation events triggered by this library on its own |
81 | | - */ |
82 | | - if (container.getAttribute(disableAttr)) { |
83 | | - return; |
| 102 | + if (!queue.size) { |
| 103 | + requestAnimationFrame(process); |
84 | 104 | } |
85 | | - let mindx, target, lindx, |
86 | | - llen = list.length, |
87 | | - mlen = mutations.length; |
88 | | - // avoiding use of forEach loops for performance reasons |
89 | | - for (mindx = 0; mindx < mlen; mindx++) { |
90 | | - target = mutations[mindx].target; |
91 | | - if (target) { |
92 | | - for (lindx = 0; lindx < llen; lindx++) { |
93 | | - if (target.matches(list[lindx])) { |
94 | | - targets[list[lindx]].count++; |
95 | | - } |
96 | | - } |
97 | | - } |
98 | | - timer = setTimeout(() => { |
99 | | - fireEvents(); |
100 | | - }, debounceInterval); |
| 105 | + for (const mutation of mutations) { |
| 106 | + queue.add(mutation.target); |
101 | 107 | } |
102 | | - }).observe(container, { |
| 108 | + }).observe(document, { |
103 | 109 | childList: true, |
104 | | - subtree: true |
| 110 | + subtree: true, |
105 | 111 | }); |
106 | 112 | } |
107 | 113 | } |
|
111 | 117 | } else { |
112 | 118 | init(); |
113 | 119 | } |
114 | | - |
115 | 120 | })(); |
0 commit comments