添加手动操作指标窗口

try to take over the world!

目前为 2016-06-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         添加手动操作指标窗口
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  try to take over the world!
// @author       You
// @match        *://*.growingio.com/projects/*
// @grant        none
// ==/UserScript==

function fetchTags() {
    fetch(location.href.replace(/.*\/projects/, "/v2/projects"), {}).then(function(res) {
        if (res.ok) {
            res.json().then(function(tags) {
                window._vds_tags = tags;
                window._vds_tag_map = {};
                tags.forEach(function(tag) {
                    window._vds_tag_map[tag.id] = tag;
                });
            });
        }
    });
}
var container = document.createElement("div");
container.style.width = '500px';
container.style.position = 'absolute';
container.style.fontFamily = 'monospace';
container.style.right = 0;
container.style.bottom = 0;
container.style.backgroundColor = 'gray';
container.style.border = 'solid gray 2px';
var tagContent = document.createElement("textarea");
tagContent.style.height = '400px';
tagContent.style.width = '100%';
tagContent.style.display = 'none';
container.appendChild(tagContent);
var fetchBtn = document.createElement('button');
var putBtn = document.createElement('button');
putBtn.textContent = 'update tag';
putBtn.className = 'ant-btn';
container.appendChild(putBtn);
putBtn.addEventListener('click', function(e) {
    var targetTagJson = JSON.parse(tagContent.value);
    if (targetTagJson.id) {
        fetch(location.href.replace(/.*\/projects/, "/projects") + '/' + targetTagJson.id, {
            method: 'PUT',
            body: JSON.stringify(targetTagJson)
        }).then(function(res) {
            if (res.ok) {
                result.textContent = 'updated!';
            }
        });
    }
});
var miniBtn = document.createElement('button');
miniBtn.className = 'ant-btn';
miniBtn.textContent = 'hide';
miniBtn.style.float = 'right';
miniBtn.addEventListener('click', function(e) {
    tagContent.style.display = 'none';
    result.style.display = 'none';
});
var maxBtn = document.createElement('button');
maxBtn.textContent = 'show';
maxBtn.className = 'ant-btn';
maxBtn.style.float = 'right';
maxBtn.addEventListener('click', function(e) {
    tagContent.style.display = 'block';
    result.style.display = 'block';
});
container.appendChild(miniBtn);
container.appendChild(maxBtn);

fetchBtn.className = 'ant-btn';
fetchBtn.textContent = 'fetch realtime data';
container.appendChild(fetchBtn);
var result = document.createElement('div');
result.style.backgroundColor = 'white';
result.style.fontFamily = 'monospace';
result.style.wordBreak = 'break-all';
container.appendChild(result);
document.body.appendChild(container);
console.log('start');
fetchBtn.addEventListener('click', function() {
    console.log('fetching');
    fetch(location.href.replace('events', 'realtime'), {
        method: "POST",
        body: tagContent.value
    }).then(function(res) {
        if (res.ok) {
            result.textContent = res.json().then(function(data) {
                result.textContent = JSON.stringify(data, 0, 2);
            });
        }
    }, function(e) {
        alert("Error submitting form!");
    });
});
document.addEventListener('click', function(e) {
    if (e.target == tagContent || e.target == putBtn || e.target == fetchBtn) return;
    var showingTag = document.querySelector("div.row.metric-row.opened-item");
    if (!showingTag) return;
    var tagId = showingTag.getAttribute('data-reactid').replace(/[\\.0-9]+\$tag-/, '');
    var selectedTag = window._vds_tag_map[tagId];
    if (selectedTag) {
        tagContent.value = JSON.stringify(selectedTag, 0, 2);
    }
});
window.fetchTags = fetchTags;
fetchTags();