MPGH Code Enhance

Add select, copy and download function to code sections.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         MPGH Code Enhance
// @namespace    mpgh8otto
// @version      1.0
// @description  Add select, copy and download function to code sections.
// @author       8otto
// @match        https://www.mpgh.net/forum/showthread.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mpgh.net
// @grant        none
// @license MIT
// ==/UserScript==

window.selectText = (element, copy = false) => {
    var range = document.createRange();
    range.selectNodeContents(element);
    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
    if (copy) document.execCommand('copy');
    return false;
}

window.downloadText = (element) => {
    const fileContent = element.textContent;
    const blob = new Blob([fileContent], {type: 'text/plain'});
    const tmp = document.createElement('a');
    tmp.href = window.URL.createObjectURL(blob);
    tmp.setAttribute('download', 'Code.txt');
    tmp.style.display = 'none';
    document.body.appendChild(tmp);
    tmp.click();
    document.body.removeChild(tmp);
    return false;
}

(function() {
    'use strict';
    const select = document.createElement("a");
    select.href = "#select";
    select.setAttribute("onclick", "return selectText(this.parentElement.nextElementSibling);");
    select.appendChild(document.createTextNode("[SELECT]"));
    const copy = document.createElement("a");
    copy.href = "#copy";
    copy.setAttribute("onclick", "return selectText(this.parentElement.nextElementSibling, true);");
    copy.appendChild(document.createTextNode("[COPY]"));
    const download = document.createElement("a");
    download.href = "#download";
    download.setAttribute("onclick", "return downloadText(this.parentElement.nextElementSibling);");
    download.appendChild(document.createTextNode("[DOWNLOAD]"));
    var bbdesc = document.getElementsByClassName("bbcode_description");
    for(var desc of bbdesc) if(desc.textContent=="Code:") desc.innerHTML += " " + select.outerHTML + "  " + copy.outerHTML + "  " + download.outerHTML;
})();