あぷにアップ

書き込み欄にあぷ/あぷ小へのアップロードボタンを追加します。

目前为 2022-01-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         あぷにアップ
// @description  書き込み欄にあぷ/あぷ小へのアップロードボタンを追加します。
// @version      1.0.0
// @match        *://*.2chan.net/*
// @icon         https://www.google.com/s2/favicons?domain=2chan.net
// @namespace    https://gf.qytechs.cn/users/809755
// @grant        GM_xmlhttpRequest
// @connect      2chan.net
// ==/UserScript==

/* jshint esversion: 8 */

(function () {
    'use strict';
    setTimeout(setup, 1000);
})();

function setup() {
    const skip = !location.pathname.includes('.htm') || location.pathname.includes('futaba.') || !document.getElementById('ftbl');
    if (skip) {
        return;
    }
    const tbody = document.querySelector('#ftbl tbody');
    var tr = document.createElement('tr');
    tr.innerHTML = '<td class="ftdc"><b>あぷ</b></td><td><label style="font-size:10px;"><input type="file" id="upup" size="35"><br>ファイル選択後に「OK」を押すとアップロードが始まります</label></td>';
    tbody.appendChild(tr);
    document.querySelector('#upup').addEventListener('change', upload);
}

function upload() {
    const files = document.querySelector('#upup');
    if (!files.files[0]) {
        return;
    }

    const file = files.files[0];
    const filename = file.name;
    const filesize = file.size;

    const MAX_FILE_SIZE_UP = 10000000;
    const MAX_FILE_SIZE_UP2 = 3000000;
    const MAX_FILE_SIZE = {
        "up": MAX_FILE_SIZE_UP,
        "up2": MAX_FILE_SIZE_UP2,
    }

    const server = filesize <= MAX_FILE_SIZE_UP2 ? "up2" : (filesize <= MAX_FILE_SIZE_UP ? "up" : "tooBig");

    if (server === "tooBig") {
        clear();
        alert(`${filesize} バイトの ${filename} はアップロードできません。
${MAX_FILE_SIZE_UP} バイトがファイルサイズの上限です。`);
        return;
    }

    if (!window.confirm(`${filename} をアップロードします`)) {
        clear();
        alert(`${filename} のアップロードを中止しました`);
        return;
    }

    const formdata = new FormData();
    const com = uuidv4();
    const pass = document.getElementsByName('pwd')[0].value || '5463';

    formdata.set('up', file);
    formdata.set('MAX_FILE_SIZE', MAX_FILE_SIZE[server]);
    formdata.set('mode', 'reg');
    formdata.set('com', com);
    formdata.set('pass', pass);
    GM_xmlhttpRequest({
        method: "POST",
        url: `https://dec.2chan.net/${server}/up.php`,
        data: formdata,
        headers: {
            "Origin": "https://dec.2chan.net",
            "Referer": `https://dec.2chan.net/${server}/up.htm`,
        },
        onload: async function (response) {
            const uploaded = await getFilename(com, server);
            // console.log(`filename: ${filename}`);
            if (uploaded) {
                const textarea = document.querySelector('#ftxa');
                textarea.value = `${textarea.value.trim()}\n${uploaded}`;
            } else {
                alert('アップロード失敗');
            }
            clear();
        }
    });
}

function getFilename(key, server) {
    return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            method: "GET",
            url: `https://dec.2chan.net/${server}/up.htm`,
            onload: function (response) {
                const res = response.responseText;
                const reg = new RegExp(`>([^>]+?)</a></td><td class="fco">${key}`);
                const found = res.match(reg);
                // console.log(found);
                if (found) {
                    resolve(found[1]);
                }
                reject(undefined);
            },
            onerror: function (error) {
                reject(undefined);
            }
        });
    });
}

function clear() {
    document.querySelector('#upup').value = '';
}

// https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid/2117523#2117523
function uuidv4() {
    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
        (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    );
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址