MGit

Adds some features on github.com & gitlab.com to integrate it with Moduon Team

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

// ==UserScript==
// @name           MGit
// @original       https://gf.qytechs.cn/es/scripts/390531-tgithub
// @author         Eduardo de Miguel
// @version        1.03
// @grant          none
// @run-at         document-idle
// @namespace      moduon
// @include        /^https?:\/\/(?:www\.)?github\.com\/?.*$/
// @include        /^https?:\/\/(?:www\.)?gitlab\.com\/?.*$/
// @description    Adds some features on github.com & gitlab.com to integrate it with Moduon Team
// ==/UserScript==
 
(function (window) {
    "use strict";
 
    var MTGithub = {
        ODOO_SERVER: 'https://www.moduon.team',
        COMPANY_NAME: 'Moduon',
 
        REGEX_TEMPLATES: {},
 
        init: function () {
            this._addRegexTemplate('MT', new RegExp(/\bMT-(\d+)\s*/gi), `<a target='_blank' href='${this.ODOO_SERVER}/web#id=$1&model=project.task&view_type=form'>${this.COMPANY_NAME}-Task #$1</a>`);
 
            this._replaceTask();
            if (this._isLocationHost('github')) {
                this._ghAddNavbarOptions();
            }
        },
 
        /* CORE FUNCTIONS */
        _isLocationHost: function (host) {
            return document.location.host.toLowerCase().includes(host);
        },
        _addRegexTemplate: function (templateName, regex, html) {
            this.REGEX_TEMPLATES[templateName] = { regex: regex, html: html };
        },
        _executeRegexReplace: function (templateName, text) {
            if (templateName in this.REGEX_TEMPLATES && text.match(this.REGEX_TEMPLATES[templateName].regex)) {
                return text.replace(this.REGEX_TEMPLATES[templateName].regex, this.REGEX_TEMPLATES[templateName].html);
            }
            return false;
        },
 
        /* COMMON FUNCTIONS */
        _replaceTask: function () {
            const searchAndParse = () => {
                document.querySelectorAll('.comment-body,.note-text,.description,.commit-description').forEach((elm) => {
                    const htmlTemplate = this._executeRegexReplace('TT', elm.innerHTML);
                    if (htmlTemplate) {
                        elm.innerHTML = htmlTemplate;
                    }
                });
            };
            // Mutation Observer
            if (typeof this.observer === 'undefined') {
                let targetNode = undefined;
                if (this._isLocationHost('github')) {
                    targetNode = document.getElementsByTagName('main')[0];
                } else if (this._isLocationHost('gitlab')) {
                    targetNode = document.getElementById('notes-list');
                }
                if (typeof targetNode !== 'undefined') {
                    this.observer = new MutationObserver(searchAndParse);
                    this.observer.observe(targetNode, { childList: true, subtree: true });
                }
            }
            searchAndParse();
        },
 
        /* GITHUB FUNCTIONS */
        _ghAddNavbarOptions: function () {
            const targetNode = document.getElementsByTagName('nav')[0];
            if (typeof targetNode !== 'undefined') {
                const exampleItem = targetNode.querySelector("a[href^='/pulls']");
                const menuItem = document.createElement("A");
                menuItem.className = exampleItem.className;
                menuItem.style.cssText = exampleItem.style.cssText
                menuItem.textContent = this.COMPANY_NAME;
                menuItem.href = `/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+involves%3A${this.COMPANY_NAME}`;
                targetNode.insertAdjacentElement('afterbegin', menuItem);
            }
        },
    };
 
    MTGithub.init();
 
})(window)

QingJ © 2025

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