OnePlusBBS QuickKey

一加社区快捷键

当前为 2020-01-07 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         OnePlusBBS QuickKey
// @description  一加社区快捷键
// @namespace    https://greasyfork.org/
// @author       choosezzz
// @version      1.9
// @match        https://www.oneplusbbs.com/forum*
// @match        https://www.oneplusbbs.com/thread*
// @match        https://www.oneplusbbs.com/homemod-space-uid*
// @icon         https://static.oneplus.cn/data/attachment/common/4c/common_121_icon.png
// @require      https://cdn.jsdelivr.net/npm/vue
// @grant        GM_addStyle
// ==/UserScript==

(function() {

    //settings
    appendSettingDiv();
    //css
    GM_addStyle("#settingMountParent{font-size:15px;width:400px;z-index:999;background-color:white;position: fixed;top:50%;left:50%;transform:translateX(-50%) translateY(-50%);-moz-transform:translateX(-50%) translateY(-50%);-webkit-transform:translateX(-50%) translateY(-50%);-ms-transform:translateX(-50%) translateY(-50%);border-radius:5px;box-shadow:3px 3px 10px red}");
    GM_addStyle("#setting_content{padding:2px;border-top:2px solid black;border-bottom:2px solid black}");
    GM_addStyle(".setting_op{background:#34aff4;width:50px;padding:3px 20px;}.setting_op:hover{background:#EEDA0A;color:#ffffff;}");

    autoReflash();

    document.onkeydown = function(event) {
        var href = window.location.href;
        var e = event || window.event;
        if (!e) {
            return;
        }
        if (e.ctrlKey && e.keyCode == 83) {
            stopDefault(e);
            settingApp.ctrlShow();
        }
        //个人主页
        if (href.search("homemod-space-uid*") != -1) {
            //Ctrl+Alt
            if (e.altKey && e.ctrlKey) {
                stopDefault(e);
                var uid = href.replace(/https:\/\/www.oneplusbbs.com\/homemod-space-uid-/g, "").replace(/.html/g, "");
                $("a_sendpm_" + uid).click();
            }
        }

        //Ctrl+number
        if (((e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >= 97 && e.keyCode <= 105)) && e.ctrlKey) {
            stopDefault(e);
            var msgId = e.keyCode - 96 < 0 ? e.keyCode - 48 : e.keyCode - 96;
            quickPaste(msgId);
        }

        //普通帖子页
        if (href.indexOf("thread-") != -1 || href.indexOf("viewthread") != -1) {
            handlePost(e);
        }

        //列表页
        if (href.indexOf("forum-") != -1 || href.indexOf("forumdisplay") != -1) {
            handleList(e);
        }

    }
})();

function appendSettingDiv() {
    var settingMount = document.createElement("div");
    settingMount.id = "settingMountParent";
    settingMount.innerHTML = '<div id="setting_mount" v-show="isShow">' +
        '<div style="padding: 3px;background: #aadcdf;text-align: center;">' +
        '<span>【使用 Ctrl+S 打开/关闭 设置选项】</span></div>' +
        '<div v-show="tips != null && tips.length!=0" style="border-top:2px solid black;text-align: center;background: red;font-size: 16px;color:yellow;">{{tips}}</div>' +
        '<div id="setting_content"><div v-for="item of 9" :key="item" style="padding-left: 12px;padding-top: 2px;">' +
        '<span>Ctrl+{{item}}:</span>' +
        '<input type="text" style="width: 77%" v-model="quickMsg[item-1]"><span v-if="item==1" style="font-size:11px">✔️</span></div></div>' +
        '<div style="padding-left: 12px;padding-top: 4px;">定时刷新[秒]:<input type="number" v-model="reflashTime" style="width: 25%" min="300"></div>' +
        '<div style="padding-left: 12px;padding-top: 4px;">删除时自动勾选:<input type="checkbox" v-model="autoRecord" :checked="autoRecord" style="zoom:130%"> 违规登记 <input type="checkbox" v-model="autoSend" :checked="autoSend" style="zoom:130%"> 通知作者</div>' +
        '<div style="text-align: center;padding: 4px;border-top:2px solid black;"><span class="setting_op" @click="ctrlShow">关闭</span>' +
        '<span style="width:50px;padding:3px 50px;"> | </span><span class="setting_op" @click="saveSettings">{{saveTips}}</span></div></div>';
    document.body.appendChild(settingMount);

    dragFunc("settingMountParent");
}

function dragFunc(id) {
    var Drag = document.getElementById(id);
    Drag.onmousedown = function(event) {
        var ev = event || window.event;
        event.stopPropagation();
        var disX = ev.clientX - Drag.offsetLeft;
        var disY = ev.clientY - Drag.offsetTop;
        document.onmousemove = function(event) {
            var ev = event || window.event;
            Drag.style.left = ev.clientX - disX + "px";
            Drag.style.top = ev.clientY - disY + "px";
            Drag.style.cursor = "move";
        };
    };
    Drag.onmouseup = function() {
        document.onmousemove = null;
        this.style.cursor = "default";
    };
}
var settingApp = new Vue({
    el: "#setting_mount",
    data: {
        isShow: false,
        tips: "",
        saveTips: "保存",
        quickMsg: ["请勿灌水或回复与帖子无关的内容", "请勿发布纯标题无内容贴"],
        reflashTime: 0,
        autoRecord: true,
        autoSend: false,
        timeOut: 500

    },
    methods: {
        ctrlShow: function() {
            this.isShow = !this.isShow;
            if (this.isShow) {
                var msg = getQuickMsg();
                if (null != msg) {
                    this.quickMsg = msg;
                }
                this.reflashTime = localStorage.reflashTime;
                this.autoRecord = JSON.parse(localStorage.autoRecord);
                this.autoSend = JSON.parse(localStorage.autoSend);
            }
        },
        saveSettings: function() {
            var result = this.quickMsg.join("$");
            localStorage.quickMsg = result;
            if (this.reflashTime > 0) {
                localStorage.reflashTime = this.reflashTime;
            } else {
                localStorage.reflashTime = 0;
                this.tips = "定时刷新时间配置无效,将不会启用自动刷新功能。";
                this.timeOut = 3000;
            }
            localStorage.autoSend = this.autoSend;
            localStorage.autoRecord = this.autoRecord;
            this.saveTips = "保存成功";
            setTimeout(() => {
                this.isShow = false;
                this.tips = "";
                this.saveTips = "保存";
            }, this.timeOut);
        }
    }
});

function getQuickMsg() {

    var msg = localStorage.quickMsg;
    if (!msg || null == msg || msg.split("$").length == 0) {
        settingApp.isShow = true;
        settingApp.tips = "请先设置快捷键对应的内容";
        setTimeout(function() {
            settingApp.tips = "";
        }, 2000);
        return null;
    } else {
        return msg.split("$");
    }
}

function pasteMsg(element, msgArr, id) {
    if (msgArr == null || msgArr.length < id) {
        settingApp.isShow = true;
        settingApp.tips = "当前快捷键暂未定义任何内容,请自定义后使用";
        setTimeout(function() {
            settingApp.tips = "";
        }, 2000);
        return;
    }
    if (element && msgArr.length >= id) {
        if (msgArr[id - 1] == "") {
            settingApp.isShow = true;
            settingApp.quickMsg = msgArr;
            settingApp.tips = "当前快捷键暂未定义任何内容,请自定义后使用";
            setTimeout(function() {
                settingApp.tips = "";
            }, 2000);
            return;
        }
        element.value = msgArr[id - 1];
        return;
    }

}
//屏蔽默认快捷键
function stopDefault(e) {

    // W3C标准
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        //IE
        window.event.returnValue = false;
    }
    return false;
}

function defaultQuickPaste() {
    setTimeout(function() {
        var doc = quickPaste(1);
        if (doc) {
            doc.select();
            autoChecked();
        }
    }, 900);
}

function quickPaste(id) {

    var href = window.location.href;
    var quickMsg = getQuickMsg();
    //操作说明输入
    var mods = $("fwin_mods");
    if (mods && mods.style.display != "none") {
        pasteMsg($("reason"), quickMsg, id);
        return $("reason");
    }
    //列表页
    var ftadmin = $("floatlayout_topicadmin");
    if (ftadmin && ftadmin.style.display != "none") {
        pasteMsg($("reason"), quickMsg, id);
        return $("reason");
    }
    //回帖输入
    var reply = $("fwin_reply");
    if (reply && reply.style.display != "none") {
        pasteMsg($("postmessage"), quickMsg, id);
        return $("postmessage");
    }
    //消息
    var showMsgBox = $("fwin_showMsgBox");
    if (showMsgBox && showMsgBox.style.display != "none") {
        pasteMsg($("pmmessage"), quickMsg, id);
        return $("pmmessage");
    }
    //管理页面
    if (href.search("op=ban&uid=") != -1) {
        pasteMsg($("ct").getElementsByClassName("pt")[0], quickMsg, id);
    }
}

//判断是否已显示操作选项
var cateShow = false;
//分类选项索引
var cateIndex = 0;

//移动操作
var moveShow = false;
//游乐园
var moveIndex = 21;

function handlePost(e) {

    // Ctrl + z 快速回复
    if (e.keyCode == 90 && e.ctrlKey) {
        stopDefault(e);
        $("post_reply").click();
        defaultQuickPaste();
        return;
    }

    //获取操作列表
    var modMenu = $("modmenu");
    if (!modMenu) {
        console.log("当前板块无权限");
        return;
    }
    var links = modMenu.getElementsByTagName("a");
    //Ctrl+D 快速删除
    if (e.keyCode == 68 && e.ctrlKey) {
        stopDefault(e);
        var mdly = $("mdly");
        if (mdly && mdly.style.display != "none") {
            var aList = mdly.getElementsByClassName("c")[0].getElementsByTagName("a");
            if (aList.length >= 3) {
                if (aList[2].innerHTML == "删除") {
                    aList[2].click();
                }
            }
        } else {
            links[0].click();
        }
        defaultQuickPaste();
        return;
    }
    //Ctrl+Alt 快速分类功能
    if (e.altKey && e.ctrlKey) {
        stopDefault(e);
        quickCate(links[10]);
        return;
    }
    //Ctrl+X 快速移动功能
    if (e.ctrlKey && e.keyCode == 88) {
        stopDefault(e);
        quickMove(links[9]);
        return;
    }
    resetCateAndMove();
}

function handleList(e) {
    var checkBoxs = document.getElementsByClassName("o");
    if (!checkBoxs || checkBoxs.length < 5) {
        console.log("此版块权限");
    }
    var mdly = $("mdly");
    if (mdly) {
        var ops = mdly.getElementsByTagName("p")[0].getElementsByTagName("a");
        if (!ops || ops.length < 3) {
            return;
        }
        // ctrl + alt
        if (e.altKey && e.ctrlKey) {
            stopDefault(e);
            quickCate(ops[2]);
            return;
        }
        // ctrl + x
        if (e.ctrlKey && e.keyCode == 88) {
            stopDefault(e);
            quickMove(ops[1]);
            return;
        }
        // ctrl + d
        if (e.keyCode == 68 && e.ctrlKey) {
            stopDefault(e);
            ops[0].click();
            defaultQuickPaste();
            return;
        }
        resetCateAndMove();
    }
}

//自动刷新
function autoReflash() {
    var reflashTime = 0;
    if (!localStorage.reflashTime) {
        return;
    } else {
        reflashTime = parseInt(localStorage.getItem("reflashTime"));
    }
    if (isNaN(reflashTime) || reflashTime <= 0) {
        return;
    }
    var title = document.title;
    var loopInterval = setInterval(function() {
        document.title = "【" + formatTime(reflashTime) + "】" + title;
        if (reflashTime === 0) {
            clearInterval(loopInterval);
            location.reload();
            return;
        }
        reflashTime--;
    }, 1000);
}

function formatTime(t) {
    if (isNaN(t)) return "";
    var s = "";
    var h = parseInt(t / 3600);
    s += (pad(h) + ":");
    t -= (3600 * h);
    var m = parseInt(t / 60);
    s += (pad(m) + ":");
    t -= (60 * m);
    s += pad(t);
    return s;
}

function pad(n) {
    return ("00" + n).slice(-2);
}

function autoChecked() {
    document.getElementById("crimerecord").checked = JSON.parse(localStorage.autoRecord);
    document.getElementById("sendreasonpm").checked = JSON.parse(localStorage.autoSend);
}
//快捷点击分类链接
function quickCate(ele) {
    if (!cateShow) {
        if (ele) {
            ele.click();
            cateShow = true;
        }
    } else {
        let types = $("typeid");
        if (types && types.options.length > 0) {
            types.options[cateIndex].selected = "true";
            cateIndex++;
        }
        if (cateIndex == types.options.length) {
            cateIndex = 0;
        }
    }
}
//快捷点击移动链接
function quickMove(ele) {
    if (!moveShow) {
        if (ele) {
            ele.click();
            moveShow = true;
        }
    } else {
        var moveTo = $("moveto");
        if (moveTo && moveTo.options.length > 0) {
            moveTo.options[moveIndex].selected = "true";
            moveTo.onchange();
            //游乐园 <--> 轻摄影
            moveIndex = moveIndex == 21 ? 13 : 21;
        }

    }
}
//重置
function resetCateAndMove() {
    moveIndex = 21;
    cateIndex = 0;
    moveShow = false;
    cateShow = false;
}