Greasy Fork

Greasy Fork is available in English.

minerva-online assistant

1.全局:右下角添加置顶置底按钮;2.问卷管理:点击↓加载附件列表,点击√可一键下载全部附件,点击附件名下载单个附件,如弹出窗口被拦截请允许后再进行操作

当前为 2021-09-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         minerva-online assistant
// @namespace    http://greasyfork.icu/scripts/431414
// @version      1.0
// @description  1.全局:右下角添加置顶置底按钮;2.问卷管理:点击↓加载附件列表,点击√可一键下载全部附件,点击附件名下载单个附件,如弹出窗口被拦截请允许后再进行操作
// @author       inoki
// @match        https://www.minerva-online.com/*
// @resource     /lib/jquery/jquery-1.11.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /*置顶置底功能参考 http://greasyfork.icu/scripts/370556 */
    var clickScrollTime = 500; //点击按钮时,网页滚动到顶部或底部需要的时间,单位是毫秒,可修改
    var initialHeight = 0; //网页向底部滚动时,需要滚动的距离
    var scrollAction = 'undefined';
    var scrollDirection = "down"; //网页滚动方向,down 为向下,up 为向上
    var goTopBottomButton = document.createElement("div");
    goTopBottomButton.className = "goTopBottomButton";
    goTopBottomButton.innerHTML = "<img class='toggleButton' style='width:30px;height:30px;display:block;cursor:pointer;'></img>"; //图片的宽和高可修改,原始图片宽高均为 30px
    goTopBottomButton.style.position = "fixed";
    goTopBottomButton.style.zIndex = 10000;
    goTopBottomButton.style.bottom = "50px"; //距离网页底部 50px,可修改
    goTopBottomButton.style.right = "30px"; //距离网页右边 30px,可修改
    var toggleButton = goTopBottomButton.lastChild;
    toggleButton.style.opacity = 0.5; //按钮初始不透明度
    toggleButton.src = "/knowledgebase/images/arrow_back_to_top.svg"; //按钮初始显示向下的图片
    toggleButton.style.transform = "rotate(0deg)";//按钮初始旋转角度向下
    toggleButton.style.backgroundColor = "#4C5157";//背景颜色
    document.getElementsByTagName("body")[0].appendChild(goTopBottomButton);

    //按钮事件开始
    toggleButton.addEventListener("click",function() { //点击按钮时,网页滚动到顶部或底部
        if (scrollDirection == "up") {
            $('html,body').animate({scrollTop:'0px'},clickScrollTime);
        } else {
            initialHeight = $(document).height();
            $('html,body').animate({scrollTop:initialHeight},clickScrollTime);
        }
    });

    //页面滚动监听
    document.onscroll = function() {
        if (scrollAction == 'undefined') {
            scrollAction = window.pageYOffset;
        }
        var diffY = scrollAction - window.pageYOffset;
        scrollAction = window.pageYOffset;
        if (diffY < 0) {
            changeDirection("down");
        } else if (diffY > 0) {
            changeDirection("up");
        }
        if (getScrollTop() == 0) {
            changeDirection("down");
        }
        if (getScrollTop() + $(window).height() + 20 >= $(document).height()) {
            changeDirection("up");
        }
    };

    //改变按钮方向
    function changeDirection(direction) {
        scrollDirection = direction;
        if (direction == "down") {
            toggleButton.style.transform = "rotate(0deg)";
        }
        if (direction == "up") {
            toggleButton.style.transform = "rotate(180deg)";
        }
    }

    //获取垂直方向滑动距离
    function getScrollTop() {
        var scrollTop = 0;
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop;
        } else if (document.body) {
            scrollTop = document.body.scrollTop;
        }
        return scrollTop;
    }


    /*问卷管理界面生效的附件下载功能*/
    if (document.location.href.indexOf("alias=smngr.surveyexplorer")>=0){
        $("tr.persist-header").each(function(){
            $(this).children().first().after( $(this).children().first().clone(true));
        });
        $("div.sticky-wrap").find(":checkbox").each(function(){//checkbox后添加下载按钮
            var surveyid=$(this).val();
            $(this).parent().after('<td><button type=button id='+surveyid+' class=download><b>↓</td>');
            $("#"+surveyid+".download").on("click",download_button);
        });

        //获取附件列表
        function download_button(){
            var surveyid=$(this).attr("id");
            $("#"+surveyid+".download").after('<p id='+surveyid+' class=loading><b>......');
            $.get("/document.asp?alias=survey.view&InstanceID="+surveyid,function(data,status){//获取当前survey内容
                if (status=="success"){
                    var fileno=$(data).find("td.attachLeftCell").size();
                    $("p#"+surveyid+".loading").after('<ol id='+surveyid+' class=filelist>\n#='+fileno+'');
                    if (fileno>0){
                        $(data).find("td.attachLeftCell").each(function(){
                            $('<li><a id='+surveyid+' class=file href='+file_attr(this)[1]+'>'+file_attr(this)[0]+'</li>').appendTo("ol#"+surveyid+".filelist");
                        });
                        $("ol#"+surveyid+".filelist").prepend('<button type=button id='+surveyid+' class=yes><b>√');
                        $("button#"+surveyid+".yes").on("click",download_yes);
                        download_button0(surveyid);
                    }
                    else {
                        download_button0(surveyid);
                    }
                }
                else {
                    download_button0(surveyid);
                }
            });
        }

        //判断附件类型并获取名称和地址
        function file_attr(file){
            if ($(file).children().first().is("img")){
                var fileurl=$(file).find("img.attachedImg").attr("src");
                if (fileurl.indexOf("Visual.asp?")>=0){
                    fileurl=$(file).next().find("a.downloadLinkBtn").attr("href");
                }
            }
            else {
                fileurl=$(file).children().first().attr("data-source");
            }
            var filename=$(file).next().find("div.propValueContent.propValueFileName").text();
            if (fileurl.indexOf("getImage")>=0){
                fileurl=fileurl.replace("Image.asp?","Attachment.asp?Attachment");
            }
            return [filename,fileurl];
        }

        //按钮变为关闭
        function download_button0(surveyid){
            $("p#"+surveyid+".loading").remove();
            $("button#"+surveyid+".download").unbind();
            $("button#"+surveyid+".download").on("click",download_button1);
            $("button#"+surveyid+".download").text("×");
        }

        //按钮重置为初始
        function download_button1(){
            var surveyid=$(this).attr("id");
            $("ol").remove("#"+surveyid);
            $("button#"+surveyid+".download").unbind();
            $("button#"+surveyid+".download").on("click",download_button);
            $("button#"+surveyid+".download").text("↓");
        }

        //确认下载
        function download_yes(){
            var surveyid=$(this).attr("id");
            var url=$("a#"+surveyid+".file");
            for(var i=0;i<url.length;i++){
                window.open($(url[i]).attr("href"));
            }
            $("button#"+surveyid+".yes").text("〇");
        }
    }


     /*WIP单店报告界面生效的若最后一项或n/a被勾选则标红*/
/*    if (document.location.href.indexOf("alias=survey.view")>=0){
        $("span.surveyansweroption").each(function(){
            if($(this).prev("input").is(':checked')){
                var qidsplit=$(this).prev("input").attr("id").split("R");
                var nextqid=qidsplit[0]+"R"+(parseInt(qidsplit[1])+1);
                if(($("#"+nextqid).length==0)||($(this).prev("input").val()=="__na__")){
                    $(this).css("color","red");
                }
            }
        })
    }
*/


})();