forked from alex-shpak/hugo-book
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb2ad3e
commit c000a31
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 脚本来自 https://blowfish.page/ | ||
|
||
var scriptBundle = document.getElementById("script-bundle"); | ||
var copyText = | ||
scriptBundle && scriptBundle.getAttribute("data-copy") | ||
? scriptBundle.getAttribute("data-copy") | ||
: "Copy"; | ||
var copiedText = | ||
scriptBundle && scriptBundle.getAttribute("data-copied") | ||
? scriptBundle.getAttribute("data-copied") | ||
: "Copied"; | ||
|
||
function createCopyButton(highlightDiv) { | ||
const button = document.createElement("button"); | ||
button.className = "copy-button"; | ||
button.type = "button"; | ||
button.ariaLabel = copyText; | ||
button.innerText = copyText; | ||
button.addEventListener("click", () => | ||
copyCodeToClipboard(button, highlightDiv) | ||
); | ||
addCopyButtonToDom(button, highlightDiv); | ||
} | ||
|
||
async function copyCodeToClipboard(button, highlightDiv) { | ||
const codeToCopy = highlightDiv.querySelector(":last-child").innerText; | ||
try { | ||
result = await navigator.permissions.query({ name: "clipboard-write" }); | ||
if (result.state == "granted" || result.state == "prompt") { | ||
await navigator.clipboard.writeText(codeToCopy); | ||
} else { | ||
copyCodeBlockExecCommand(codeToCopy, highlightDiv); | ||
} | ||
} catch (_) { | ||
copyCodeBlockExecCommand(codeToCopy, highlightDiv); | ||
} finally { | ||
codeWasCopied(button); | ||
} | ||
} | ||
|
||
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) { | ||
const textArea = document.createElement("textArea"); | ||
textArea.contentEditable = "true"; | ||
textArea.readOnly = "false"; | ||
textArea.className = "copy-textarea"; | ||
textArea.value = codeToCopy; | ||
highlightDiv.insertBefore(textArea, highlightDiv.firstChild); | ||
const range = document.createRange(); | ||
range.selectNodeContents(textArea); | ||
const sel = window.getSelection(); | ||
sel.removeAllRanges(); | ||
sel.addRange(range); | ||
textArea.setSelectionRange(0, 999999); | ||
document.execCommand("copy"); | ||
highlightDiv.removeChild(textArea); | ||
} | ||
|
||
function codeWasCopied(button) { | ||
button.blur(); | ||
button.innerText = copiedText; | ||
setTimeout(function () { | ||
button.innerText = copyText; | ||
}, 2000); | ||
} | ||
|
||
function addCopyButtonToDom(button, highlightDiv) { | ||
highlightDiv.insertBefore(button, highlightDiv.firstChild); | ||
const wrapper = document.createElement("div"); | ||
wrapper.className = "highlight-wrapper"; | ||
highlightDiv.parentNode.insertBefore(wrapper, highlightDiv); | ||
wrapper.appendChild(highlightDiv); | ||
} | ||
|
||
|
||
export function initCopyButton() { | ||
document | ||
.querySelectorAll(".kg-code-card") | ||
.forEach((highlightDiv) => createCopyButton(highlightDiv)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters