Basecamp Keyword Highlighter

Highlights predefined list of keywords in the Basecamp task list at the beginning of task description

当前为 2016-08-12 提交的版本,查看 最新版本

// ==UserScript==
// @name        Basecamp Keyword Highlighter
// @description Highlights predefined list of keywords in the Basecamp task list at the beginning of task description
// @include     https://*.basecamphq.com/projects/*/todo_lists
// @grant       none
// @version     0.2.3
// @namespace https://greasyfork.org/users/32141
// ==/UserScript==

// Here you can add your own keywords:
var taskKeywords = {
    error: {
        color: '#bf0303'
    },
    bug: {
        color: '#bf0303'
    },
    todo: {
        color: '#008c00'
    },
    feature: {
        color: '#008c00'
    },
    postponed: {
        color: '#5a00b3'
    },
    high: {
        color: '#cc00ba'
    }
};

function createKeywordStyles() {
    var style = document.createElement('style'),
        innerHtml = '';

    style.type = 'text/css';

    for (var j in taskKeywords) {
        var className = 'keyword-' + j;

        innerHtml += '.' + className + ' { ' +
                'color: #fff; ' +
                'border-radius: 3px; ' +
                'padding: 0px 2px 0px 2px; ' +
                'background-color: ' + taskKeywords[j].color + '; ' +
                '}\n';
        taskKeywords[j].className = className;
    }

    style.innerHTML = innerHtml;

    document.getElementsByTagName('head')[0].appendChild(style);
}

function processTodoLists(elementId) {
    var tasksContent;

    function getType(obj) {
        return Object.prototype.toString.call(obj).replace('[object ', '').replace(']', '').toLowerCase();
    }

    function findElements(tagName, param) {
        var result = [],
                elements = document.getElementsByTagName(tagName),
                paramType = getType(param);

        for (var i = 0; i < elements.length; i++) {
            if (paramType == 'string' && elements[i].id.indexOf(param) == 0) {
                result.push(elements[i]);
            } else if (paramType == 'regexp' && param.test(elements[i].id)) {
                result.push(elements[i]);
            }
        }

        return result;
    }

    function formatTask(el, keyword, capturedWord) {
        el.innerHTML = el.innerHTML.replace(capturedWord, '<span class="' + keyword.className + '">' + capturedWord + '</span>');
    }

    tasksContent = (elementId !== undefined)
            ? [document.getElementById(elementId)]
            : findElements('span', /^item_wrap_(\d+)$/);

    for (var i = 0; i < tasksContent.length; i++) {
        if (tasksContent[i].childElementCount > 0) {
            continue;
        }

        var text = tasksContent[i].textContent;

        for (var j in taskKeywords) {
            if (!taskKeywords[j].regexp) {
                taskKeywords[j].regexp = new RegExp('^(.*' + j + '[^:^\s]*).*', 'i');
            }

            var regexp = taskKeywords[j].regexp;
            if (regexp.test(text)) {
                formatTask(tasksContent[i], taskKeywords[j], regexp.exec(text)[1]);
                break;
            }
        }
    }
}

createKeywordStyles();
processTodoLists();