Skip to content

Commit 41fdb96

Browse files
authored
Refactor the code and improve the initialization of clipboard.js.
1 parent d3b6f95 commit 41fdb96

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

twins-insert-article-url-button.user.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ==UserScript==
2-
// @name twinsに「記事リンク」ボタンを追加するスクリプト
2+
// @name 筑波大学のtwinsに「記事リンク」ボタンを追加するスクリプト
33
// @namespace https://github.com/shuuji3/userscript-twins-insert-article-url-button
4-
// @version 0.1
5-
// @description twinsに、他人と共有可能な記事URLにリンクされた「記事リンク」ボタンを追加します
4+
// @version 0.2
5+
// @description 📋 筑波大学のtwinsに「個別のお知らせページを開くボタン」と「他人と共有できるURLをコピーするボタン」を追加します
66
// @author TAKAHASHI Shuuji <shuuji3@gmail.com>
77
// @match https://twins.tsukuba.ac.jp/campusweb/*
88
// @grant none
@@ -23,7 +23,8 @@
2323
}
2424

2525
/**
26-
* 記事名をクリックしたときに初めて、iframeが非同期に読み込まれるため、pollingを行っています。
26+
* ニュース記事へのURLを取得します。
27+
* 記事名をクリックしたときに初めてiframeが非同期に読み込まれるため、pollingを行っています。
2728
*/
2829
async function getArticleURL() {
2930
let buttonInserted = false;
@@ -69,6 +70,31 @@
6970
return button
7071
}
7172

73+
function isClipboardJSLoaded() {
74+
try {
75+
// Before loaded clipboard.js, instantiate Clipboard should be failed.
76+
new Clipboard();
77+
} catch (e) {
78+
if (e.name === 'TypeError') {
79+
return false;
80+
}
81+
}
82+
return true;
83+
}
84+
85+
/**
86+
* clipboard.jsを読み込んで初期化します。
87+
*/
88+
async function initClipboardJS() {
89+
const script = document.createElement('script');
90+
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js';
91+
document.body.appendChild(script);
92+
while (!isClipboardJSLoaded()) {
93+
await sleep(100);
94+
}
95+
new Clipboard('.btn');
96+
}
97+
7298
// main
7399
async function main() {
74100
const url = await getArticleURL();
@@ -79,14 +105,9 @@
79105
target.parentNode.insertBefore(linkButton, target);
80106
target.parentNode.insertBefore(copyButton, target);
81107

82-
// Initialize Clipboard.js
83-
const script = document.createElement('script');
84-
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js';
85-
document.body.appendChild(script);
86-
await sleep(1000); // darty...
87-
new Clipboard('.btn');
108+
initClipboardJS();
88109
}
89110

90111
await main();
91112

92-
})();
113+
})();

0 commit comments

Comments
 (0)