diff --git a/src/platforms/GitHub/CopyFileButton.tsx b/src/platforms/GitHub/CopyFileButton.tsx index 753191b..47d26c0 100644 --- a/src/platforms/GitHub/CopyFileButton.tsx +++ b/src/platforms/GitHub/CopyFileButton.tsx @@ -43,7 +43,7 @@ export function CopyFileButton(props: React.PropsWithChildren) { }, []) return ( - + {content} ) diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index c5d2b99..146812a 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -139,6 +139,13 @@ export function getCodeElement() { * click these buttons will copy file content to clipboard */ export function attachCopyFileBtn() { + const removeButtons = () => { + const buttons = document.querySelectorAll(`.${copyFileButtonClassName}`) + buttons.forEach(button => { + button.parentElement?.removeChild(button) + }) + } + if (getCurrentPageType() === PAGE_TYPES.RAW_TEXT) { // the button group in file content header const buttonGroupSelector = '.repository-content .Box-header .BtnGroup' @@ -148,6 +155,8 @@ export function attachCopyFileBtn() { raiseError(new Error(`No button groups found`)) } + removeButtons() // prevent duplicated buttons + buttonGroups.forEach(async buttonGroup => { if (!buttonGroup.lastElementChild) return const button = await renderReact(React.createElement(CopyFileButton)) @@ -158,12 +167,7 @@ export function attachCopyFileBtn() { } // return callback so that disabling after redirecting from file page to non-page works properly - return () => { - const buttons = document.querySelectorAll(`.${copyFileButtonClassName}`) - buttons.forEach(button => { - button.parentElement?.removeChild(button) - }) - } + return removeButtons } export function attachCopySnippet() {