Workflowy computable values

Compute things with sublists and display values on an item name.

目前為 2016-08-15 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/22313/141760/Workflowy%20computable%20values.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    var parseSelector = window.Parser.parse

    window.funCache = {}
    window.nodeCache = {}
    var observer

    // observe dom mutations to make it run every time an item is opened / closed
    function start () {
        GM_addStyle(`
.project .content {
  display: inline-block !important;
}

.project .result {
  display: inline;
  color: blue;
  margin-left: 10px;
}
        `)

        // observer = new MutationObserver(function (mutations) {
        //     itemOpenedOrClosed()
        // })

        document.body.addEventListener('click', function (e) {
            if (e.target.id === 'expandButton') {
                setTimeout(itemOpenedOrClosed, 1000)
            }
        })

        itemOpenedOrClosed()
    }

    function watchNodes () {
        var nodes = document.querySelectorAll('.project')
        for (let i = 0; i < nodes.length; i++) {
            let node = nodes[i]
            let projectid = node.getAttribute('projectid')
            if (!nodeCache[projectid]) {
                var list = node.querySelector('.children')
                observer.observe(list, {childList: true})
                console.log('watching ' + projectid + '.')
                nodeCache[projectid] = true
            }
        }
    }

    function itemOpenedOrClosed () {
        for (let id in funCache) {
            let script = funCache[id]
            execScript(script)
        }
        // watchNodes()
    }

    function execScript ({script, selector, id, color}) {
        let items = findItems(selector)
        for (let i = 0; i < items.length; i++) {
            execForItem(items[i], script, id, color)
        }
    }

    function execForItem(item, fun, id, color) {
        var children = []
        let par = item.querySelector('.children')
        for (let p = 0; p < par.children.length; p++) {
            if (par.children[p].classList.contains('project')) {
                children.push(par.children[p])
            }
        }

        var args = []
        for (let i = 0; i < children.length; i++) {
            let child = children[i]
            let name = child.querySelector('.name .content').innerText.trim()
            let note = child.querySelector('.notes').innerText.trim()

            var arg = {name, note}

            let results = child.querySelector('.name').querySelectorAll('.result')
            for (let r = 0; r < results.length; r++) {
                let result = results[r]
                arg[result.classList.item(1)] = result.innerText.trim()
            }

            args.push(arg)
        }

        var result = item.querySelector('.result.script-' + id)
        if (!result) {
            let content = item.querySelector('.name .content')
            result = document.createElement('span')
            result.className = 'result script-' + id
            result.title = id
            result.style.color = color
            content.parentNode.insertBefore(result, content.nextSibling)
        }
        let resultvalue = fun(args)
        result.innerHTML = (resultvalue || '').toString()
    }

    function findItems (selector) {
        let parsed = parseSelector(selector)
        var items = null
        for (let l = 0; l < parsed.length; l++) {
            let layer = parsed[l]
            items = select(layer, items)
        }
        return items || []
    }

    function select (layer, base) {
        if (!base) {
            base = document.querySelectorAll('.mainTreeRoot')
        }

        let [connector, selector] = layer
        return getChildren(base, selector, connector === 'directchild')
    }

    function getChildren (base, selector, onlydirect=false) {
        var filter
        switch (selector.type) {
            case 'id':
                filter = node =>
                node.getAttribute('projectid') === selector.val ||
                    node.querySelector('.bullet').href.split('#/')[1] === selector.val
                break
            case 'regex':
                filter = node =>
                node.querySelector('.name .content').innerText.search(selector.val) !== -1
                break
            case 'name':
                filter = node =>
                node.querySelector('.name .content').innerText.trim() === selector.val
                break
            case 'any':
                filter = () => true
                break
            default:
                throw new Error('INVALID SELECTOR: ', selector)
        }

        var children = []
        for (let i = 0; i < base.length; i++) {
            if (onlydirect) {
                let par = base[i].querySelector('.children')
                for (let p = 0; p < par.children.length; p++) {
                    if (par.children[p].classList.contains('project') && par.children[p].classList.contains('open')) {
                        children.push(par.children[p])
                    }
                }
            } else {
                let all = base[i].querySelectorAll('.children > .project.open')
                for (let i = 0; i < all.length; i++) {
                    children.push(all[i])
                }
            }
        }

        return children.filter(filter)
    }

    function registerScript (s) {
        funCache[s.id] = s
    }

    function waitFor (selector, callback) {
        let res = document.querySelector(selector)
        if (res) return callback()

        setTimeout(() => {
            waitFor(selector, callback)
        }, 1000)
    }