Sibbay Github Quick Reply

小白社区开发者实用工具,快速在issue中插入申请开发/变更deadline等操作

目前為 2018-09-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Sibbay Github Quick Reply
// @namespace    https://github.com/sibbay-ai/public
// @version      0.12
// @description  小白社区开发者实用工具,快速在issue中插入申请开发/变更deadline等操作
// @author       github.com/Yidadaa
// @include      https://github.com*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 由于github使用pjax加载页面,需要对github全站进行匹配,然后排除掉不需要运行脚本的页面
    // 工具函数
    const $$ = s => Array.from(document.querySelectorAll(s))
    const $ = s => document.querySelector(s)

    // 生成一个结点
    const createNode = (nodeType, content, className, id) => {
        const node = document.createElement(nodeType)
        className ? node.className = className : null
        id ? node.id = id : null
        const contentFn = {
            'string': (node, content) => { node.innerHTML = content },
            'object': (node, content) => { node.appendChild(content) }
        }
        const fn = contentFn[typeof content]
        fn && fn(node, content)
        return node
    }
    // 检查是否已经标记过ddl
    const checkDDL = () => {
        return $$('.timeline-comment-group .edit-comment-hide').some(node => {
            return /申请开发\ deadline/.test(node.innerText)
        })
    }

    // 生成模板
    const generateText = () => {
        const hasDDL = checkDDL()
        const date = new Date()
        const day = date.getDate()
        const month = day <= 15 ? date.getMonth() + 1 : date.getMonth() + 2
        const year = date.getFullYear()
        let text = `申请开发 deadline: ${year}-${month}-15 size: 0.1`
        if (hasDDL) text = `变更 deadline: ${year}-${month}-15`
        return text
    }

    // 生成按钮
    const applyBtn = createNode('div', checkDDL() ? 'Change deadline' : 'Wanna develop', 'btn')
    applyBtn.onclick = () => {
        const textarea = document.getElementById('new_comment_field')
        textarea.value += `${textarea.value && '\n'}` + generateText()
    }

    const appendBtn = () => {
        if (!location.href.includes('sibbay-ai')) return
        const buttons = document.getElementById('partial-new-comment-form-actions')
        if (!buttons) {
            return
        }
        buttons.appendChild(applyBtn)
    }

    document.addEventListener('pjax:complete', appendBtn)
    appendBtn()
})();