Skip to content

Instantly share code, notes, and snippets.

@Hiryuto-oecu
Last active June 2, 2026 06:04
Show Gist options
  • Select an option

  • Save Hiryuto-oecu/10661cac099d6670a5a909b1c6c83546 to your computer and use it in GitHub Desktop.

Select an option

Save Hiryuto-oecu/10661cac099d6670a5a909b1c6c83546 to your computer and use it in GitHub Desktop.
Moodleの問題をコピーするプログラム
// ==UserScript==
// @name Moodle 問題コピー
// @namespace https://gist.github.com/Hiryuto-oecu/10661cac099d6670a5a909b1c6c83546
// @version 1.0
// @description Moodleの問題要素の生のHTMLをクリップボードにコピーします。
// @author Hiryuto
// @match https://moodle2026.mc2.osakac.ac.jp/2026/mod/quiz/*
// @grant GM_setClipboard
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 指定された要素に「コピーリンク」を埋め込む処理
function injectCopyLinks() {
const targets = document.querySelectorAll('.formulation.clearfix');
targets.forEach(target => {
// すでにリンクが追加されている場合はスキップ
if (target.querySelector('.moodle-question-copy-link')) return;
// aタグ(リンク)を作成
const link = document.createElement('a');
link.className = 'moodle-question-copy-link';
link.innerText = 'コピィ';
link.href = '#';
// スタイルの設定 (指定された色)
link.style.color = '#cce6ea';
link.style.textDecoration = 'underline';
link.style.margin = '0 0 12px 0';
link.style.cursor = 'pointer';
link.style.fontSize = '14px';
link.style.fontWeight = 'bold';
link.style.display = 'inline-block';
// クリック時のコピー処理
link.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
// コピーデータに「コピーリンク」自身を含めないよう、クローンしてリンクを削除
const clone = target.cloneNode(true);
const linkElement = clone.querySelector('.moodle-question-copy-link');
if (linkElement) {
linkElement.remove();
}
// 生のHTMLを取得
const rawHtml = clone.outerHTML;
// クリップボードにテキストとしてコピー
GM_setClipboard(rawHtml, 'text');
// コピー完了のフィードバック(2秒後に元に戻す)
const originalText = link.innerText;
link.innerText = '✅';
link.style.color = '#a8e6cf'; // 成功時の色
setTimeout(() => {
link.innerText = originalText;
link.style.color = '#cce6ea';
}, 2000);
});
// 要素の先頭にリンクを追加
target.prepend(link);
});
}
// 1秒ごとに監視して自動配置
setInterval(injectCopyLinks, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment