Skip to content

Commit fc62b45

Browse files
authored
Fix clone button disappearing on Github PR pages when logged in (#11)
1 parent a6e9640 commit fc62b45

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/lib/integrations/github.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export const Github: Integration = {
3333
},
3434
getButtonTarget(document: Document) {
3535
const node =
36-
findDOMNodeByContent("Code")?.parentElement ??
37-
document.querySelector<HTMLElement>(".gh-header-actions");
36+
document.querySelector<HTMLElement>(".gh-header-actions") ??
37+
findDOMNodeByContent("Code")?.parentElement;
3838
if (!node) {
3939
throw new EIntegrationTargetError({
4040
message: "Unable to find button target",

src/pages/Content.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ import { UpdateMessage } from "@lib/utils/messages";
55
import { getSupportedIntegration } from "@lib/integrations";
66
import { attachShadow } from "@lib/utils/dom/shadow";
77
import { CloneButton } from "./content/CloneButton";
8+
import _ from "lodash";
89

910
const MAX_INIT_ATTEMPTS = 12;
1011

1112
let buttonContainer: HTMLDivElement | null = null;
1213

14+
const debouncedInit = _.debounce(() => init(), 250);
15+
16+
function isElementVisible(el: HTMLElement) {
17+
return el.offsetWidth > 0 && el.offsetHeight > 0;
18+
}
19+
1320
function init(attempts: number = 0) {
1421
if (attempts > MAX_INIT_ATTEMPTS) {
1522
// Too many attempts, aborting
1623
return;
1724
}
18-
if (document.contains(buttonContainer)) {
19-
// ALready initialized
25+
if (buttonContainer && document.contains(buttonContainer)) {
26+
// Already initialized
27+
if (!isElementVisible(buttonContainer)) {
28+
// Button is no longer visible, we need to try to place it again
29+
buttonContainer?.remove();
30+
debouncedInit();
31+
}
2032
return;
2133
}
2234
try {
@@ -40,6 +52,15 @@ function init(attempts: number = 0) {
4052
portal={portalContainer}
4153
/>,
4254
);
55+
56+
// Try to catch if the button gets removed dynamically and reinitialize
57+
const observer = new MutationObserver(debouncedInit);
58+
observer.observe(buttonTarget, {
59+
childList: true,
60+
subtree: true,
61+
});
62+
window.removeEventListener("focus", debouncedInit);
63+
window.addEventListener("focus", debouncedInit);
4364
} catch (error) {
4465
if (error instanceof ENoIntegrationError) {
4566
// Ignore, expected error when the site is not supported.

0 commit comments

Comments
 (0)