byr-signature

add signature for byr-forum

当前为 2016-06-09 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         byr-signature
// @namespace    flowmemo
// @version      0.1
// @description  add signature for byr-forum
// @author       flowmemo
// @match        https://bbs.byr.cn/*
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var defaultSig = '\n' +
        '————\n' +
        '示例签名:' +
        '微博 [url=http://weibo.com/flowmemo]@flowmemo[/url] 现在主要写JavaScript. 关注广泛, 欢迎交流.';
    var id; // timeout id
    var div;
    function addSignature (sig) {
        var signature = GM_getValue('sig', defaultSig);
        console.log('/post');
        div = document.getElementById('post_content');
        if(!div) return;
        console.dir(div);
        console.log('change value');
        div.value += signature;
        return true;
    }
    function polling() {
        window.clearTimeout(id); // clear timeout if it exist
        if (window.location.href.indexOf('/post') === -1) return;
        function cb () {
            if (addSignature()) {
                sigPanel();
                return;
            }
            id = setTimeout(cb, 300);
        }
        cb();
    }
    function sigPanel() {
        var div = document.createElement('div');
        var referNode = document.getElementsByClassName('post-list-item')[2];
        referNode.appendChild(div);

        console.log(div.outerHTML);
        div.outerHTML = '<br><div class="byr-signature">' +
            '<div class="post-m">byr-signature</div>' +
            '<textarea class="post-textarea"name="sig-content" placeholder="在此输入你的签名, 保存后刷新页面生效"></textarea><br>' +
            '<input name="saveSig" type="button" value="保存"></div>';
        var userSig = document.getElementsByName('sig-content')[0];
        userSig.value = GM_getValue('sig', defaultSig);
        var saveButton = document.getElementsByName('saveSig')[0];
        saveButton.onclick = function() {
            console.log(userSig.value);
            GM_setValue('sig', (userSig.value));
        };
    }
    if (window.location.href.indexOf('/post') > -1) polling();
    console.log('add change');
    window.addEventListener('hashchange', polling);
})();