Skip to content
This repository was archived by the owner on Jan 24, 2023. It is now read-only.

Commit 0885573

Browse files
committed
Try to make squashed-merge-message better at its job
* Change when content script is run from document_end to default document_idle. * Wait for comments element instead of assuming it exists. * Add debug logs to find when squashed-merge-message does not copy the PR description.
1 parent 555a3de commit 0885573

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

content.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ function warn(message) {
22
console.warn(`squashed-merge-message: ${message}`);
33
}
44

5-
function copyPrDescription(event) {
5+
function debug(message) {
6+
console.debug(`squashed-merge-message: ${message}`);
7+
}
8+
9+
function copyPrDescription() {
10+
debug('copy PR description');
611
const prTitleEl = document.getElementById('issue_title');
712
if (!prTitleEl) {
813
warn('failed to find PR title element');
@@ -54,13 +59,16 @@ function copyPrDescription(event) {
5459
}
5560

5661
function waitForElement(selector) {
62+
debug(`wait for element ${selector}`);
5763
return new Promise(resolve => {
5864
if (document.querySelector(selector)) {
65+
debug(`found element ${selector}`);
5966
return resolve(document.querySelector(selector));
6067
}
6168

6269
const observer = new MutationObserver(() => {
6370
if (document.querySelector(selector)) {
71+
debug(`found element ${selector}`);
6472
resolve(document.querySelector(selector));
6573
observer.disconnect();
6674
}
@@ -73,7 +81,8 @@ function waitForElement(selector) {
7381
});
7482
}
7583

76-
async function addMergeListener(event) {
84+
async function addMergeListener() {
85+
debug('add merge listener');
7786
const prMergePanel = await waitForElement('.js-merge-pr:not(.is-rebasing)');
7887
if (!prMergePanel) {
7988
warn('failed to find PR merge panel');
@@ -83,7 +92,24 @@ async function addMergeListener(event) {
8392
prMergePanel.addEventListener('details:toggled', copyPrDescription);
8493
}
8594

95+
async function addPjaxEndListener() {
96+
debug('add pjax:end listener');
97+
document.addEventListener('pjax:end', addMergeListener);
98+
}
99+
100+
async function addCommentsListener() {
101+
debug('add comments listener');
102+
const comments = await waitForElement('.js-discussion');
103+
if (!comments) {
104+
warn('failed to find comments');
105+
return;
106+
}
107+
const observer = new MutationObserver(addMergeListener);
108+
observer.observe(comments, { childList: true });
109+
}
110+
86111
function main() {
112+
debug('main');
87113
// Only run on PR pages
88114
if (!window.location.pathname.match('/pull/[0-9]+$')) return;
89115

@@ -92,15 +118,11 @@ function main() {
92118

93119
// And on AJAX events
94120
// (Happens when you switch from PR diff or commits back to merge)
95-
document.addEventListener('pjax:end', addMergeListener);
121+
addPjaxEndListener();
96122

97123
// And when new comments are added, removed, edited, etc.
98124
// (Something about how GitHub refreshes the comments discards all events ¯\_(ツ)_/¯)
99-
const comments = document.querySelector('.js-discussion');
100-
if (comments) {
101-
const observer = new MutationObserver(addMergeListener);
102-
observer.observe(comments, { childList: true });
103-
}
125+
addCommentsListener();
104126
}
105127

106128
main();

manifest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"matches": [
4040
"https://github.com/*"
4141
],
42-
"run_at": "document_end",
4342
"js": [
4443
"content.js"
4544
]

0 commit comments

Comments
 (0)