-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoi-wiki__copy-button.js
104 lines (97 loc) · 2.86 KB
/
oi-wiki__copy-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// ==UserScript==
// @name OI Wiki 複製程式碼按鈕
// @namespace https://github.com/zica87/self-made-userscipts
// @version 1.1.1
// @description 程式碼右上角新增「複製」按鈕
// @author zica
// @match https://oi-wiki.org/*
// @match http://oi-wiki.com/*
// @match https://demo.oi-wiki.org/*
// @match https://oi-wiki.net/*
// @match https://oi-wiki.wiki/*
// @match https://oi-wiki.win/*
// @match https://oi-wiki.xyz/*
// @match https://oiwiki.moe/*
// @match https://oiwiki.net/*
// @match https://oiwiki.org/*
// @match https://oiwiki.vx.st/*
// @match https://oiwiki.wiki/*
// @match https://oiwiki.win/*
// @match https://oiwiki.com/*
// @match https://oi.wiki/*
// @grant GM_addStyle
// @license GPL-3.0
// ==/UserScript==
(function () {
"use strict";
const cssString = `
.copy-button {
position: absolute;
right: 10px;
top: 10px;
border-radius: 5px;
padding: 3px;
}
.copy-mode {
opacity: 0.5;
cursor: pointer;
background-color: gray;
}
.copy-mode:hover {
opacity: 1;
}
.copied-mode {
opacity: 0.5;
cursor: unset;
background-color: unset;
}
`;
let path = undefined;
const observer = new MutationObserver(() => {
if (window.location.pathname === path) {
return;
}
path = window.location.pathname;
GM_addStyle(cssString);
add_buttons();
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
function toCopyMode(button) {
button.textContent = "copy";
button.classList.add("copy-mode");
button.classList.remove("copied-mode");
}
function toCopiedMode(button) {
button.textContent = "✅copied";
button.classList.add("copied-mode");
button.classList.remove("copy-mode");
}
function add_buttons() {
const code_blocks = document.getElementsByClassName("highlight");
for (const code_block of code_blocks) {
const code = code_block.getElementsByTagName("code")[0];
const button = document.createElement("button");
button.className = "copy-button";
button.onclick = async () => {
if (button.textContent[0] == "✅") {
return;
}
try {
await navigator.clipboard.writeText(code.textContent);
toCopiedMode(button);
setTimeout(() => {
toCopyMode(button);
}, 3000);
} catch (error) {
alert(error.message);
console.error(error);
}
};
toCopyMode(button);
code.before(button);
}
}
})();