Almascript - Alma Start Process List Helper

Show what isn't done and display uploaded files.

当前为 2019-08-02 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Almascript - Alma Start Process List Helper
// @namespace    https://greasyfork.org/en/users/8332-sreyemnayr
// @version      2019.08.02.3
// @description  Show what isn't done and display uploaded files.
// @author       Ryan Meyers
// @match        https://sges.getalma.com/workflows/processes/*/review
// @require https://greasyfork.org/scripts/388114-pdf-js/code/PDFjs.js?version=721820
// @grant unsafeWindow
// ==/UserScript==

// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];

// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = '//greasyfork.org/scripts/388115-pdf-js-worker/code/PDFjs%20Worker.js?version=721821';

function fetchAndUpdate(node) {
    const updateNode = node;

    fetch(node.href).then(function(response) { return response.text(); }).then(function(body) {
            //console.log(body);
            var parser = new DOMParser();
            var doc = parser.parseFromString(body, "text/html");
            var xpath = "//li[contains(@class,'task')][not(contains(@class,'task-complete'))]";
            var result = document.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
            //console.log(result);
            var node, nodes = [];
            while (node = result.iterateNext()) {
                //console.log(node.textContent.trim());
                var newNode = document.createElement('div');
                newNode.classList.add("pill");

                newNode.innerHTML = "<i class=\"far fa-times-circle\" style=\"color:#eb6841;\"></i>"+node.textContent.trim();

                updateNode.parentElement.parentElement.children[4].append(newNode);
            }

        });

}

function fetchHealthForm(node) {
    const updateNode = node;

    fetch(node.href).then(function(response) { return response.text(); }).then(function(body) {
            //console.log(body);
            var parser = new DOMParser();
            var doc = parser.parseFromString(body, "text/html");
            var xpath = "//li[contains(@class,'task')]";
            var result = document.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
            //console.log(result);
            var node, nodes = [];
            while (node = result.iterateNext()) {
                var taskUri = node.dataset.href;
                var formUri = taskUri.replace("task-details","form");
                let headers = new Headers({
                    "Accept"       : "application/json",
                    "Content-Type" : "application/json",
                    "X-Requested-With": "XMLHttpRequest"
                });

                fetch(formUri, {method: "GET", headers: headers})
                    .then(function(response) {
                    return response.json();
                }).then(function(myJson) {
            //console.log(myJson);
            var jsonHTML = myJson.Message.html;
            jsonHTML = jsonHTML.replace(/form-section/g,"form-section-off");
            jsonHTML = jsonHTML.replace(/<ul class/g,"<ul style=\"display:none;\" class");
            //console.log(jsonHTML);
            //var files = jsonHTML.match(/<a href="(\/workflows\/processes\/.*\/get-file\?id=[a-zA-z0-9]*)">/g);
            var files = jsonHTML.match(/\/workflows\/processes\/.*\/get-file\?id=[a-zA-z0-9]*/g);
            if (files) {
                for (var file of files) {
                    fetch(file).then(function(response) {


                        return response.blob(); }
                                     ).then(async function(blob) {

                        console.log(blob.type);
                        let reader = new FileReader();
                        reader.readAsArrayBuffer(blob);
                        reader.onload = async function() {
                            var newImg;

                        //blob.arrayBuffer().then(async function(myBuffer){
                            if (blob.type === "application/pdf") {
                                newImg = document.createElement('canvas');

                                var loadingTask = pdfjsLib.getDocument(file);
                                loadingTask.promise.then(function(pdf) {
                                    console.log('PDF loaded');

                                    // Fetch the first page
                                    var pageNumber = 1;
                                    pdf.getPage(pageNumber).then(function(page) {
                                        console.log('Page loaded');

                                        var scale = 0.25;
                                        var viewport = page.getViewport(scale);

                                        // Prepare canvas using PDF page dimensions
                                        var canvas = newImg;
                                        var context = canvas.getContext('2d');
                                        canvas.height = 230;
                                        canvas.width = 160;

                                        // Render PDF page into canvas context
                                        var renderContext = {
                                            canvasContext: context,
                                            viewport: viewport
                                        };
                                        var renderTask = page.render(renderContext);
                                        renderTask.promise.then(function () {
                                            console.log('Page rendered');
                                        });
                                    });
                                }, function (reason) {
                                    // PDF loading error
                                    console.error(reason);
                                });

                                //newImg = document.createElement('a');
                                //newImg.href = file;
                                //newImg.innerHTML = "Download";
                                updateNode.append(newImg);
                            }
                            else {
                                 newImg = document.createElement('img');
                                newImg.src = file;
                                newImg.width = 200;
                                updateNode.append(newImg);

                            }


                        };

                    });
                }
            }




        });
            }
    });
}

function doIncomplete() {
    var xpath = "//tr[td[text()='Active (in progress)']]/td[2]/a";
    var result = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
    var node, nodes = [];
    while (node = result.iterateNext()) {
        nodes.push(node);
        //console.log(node.href);
        fetchAndUpdate(node);
    }
}

function doComplete() {
    var xpath = "//tr[td[text()='Active (complete)']]/td[2]/a";
    var result = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
    var node, nodes = [];
    while (node = result.iterateNext()) {
        nodes.push(node);
        //console.log(node.href);
        fetchHealthForm(node);
    }
}

(function() {
    'use strict';
    var newStyle = document.createElement('style');
    newStyle.innerHTML = `
.pill {
background-color: #fff;
padding: .5em;
border-radius: 5px;
display: inline-block;
cursor: default;
margin-top: 1em;
font-size: 8pt;
}
    `;
    document.getElementsByTagName('head')[0].append(newStyle);

    var showFormsButton = document.createElement('button');
    showFormsButton.onclick = doComplete;
    showFormsButton.innerHTML = "Show Uploads";
    document.getElementById('page-header').append(showFormsButton);




    doIncomplete();

})();