油猴1-IMMS提取批号并打开网页

发送POST显示返回的JSON中前20个ownBatch的值及其关联ID

// ==UserScript==
// @name 油猴1-IMMS提取批号并打开网页
// @namespace http://tampermonkey.net/
// @version 0.8
// @description 发送POST显示返回的JSON中前20个ownBatch的值及其关联ID
// @author Your Name
// @match http://192.168.100.113/pcis/a/index*
// @grant GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 初始化 container
    let container=null;// 初始化一个空的 container 变量

    // 创建主按钮
    const mainButton=document.createElement('button');// 创建一个按钮元素
    mainButton.textContent='获取前20批';// 设置按钮的文本内容
    mainButton.style.position='fixed';// 设置按钮的位置为固定
    mainButton.style.top='61px';// 设置按钮的顶部位置
    mainButton.style.left='58px';// 设置按钮的左侧位置
    mainButton.style.zIndex='3000';// 设置按钮的层级
    mainButton.classList.add("effect1");// 添加 CSS 类 effect1

    // 创建下拉列表
    const selectBox=document.createElement('select');// 创建一个下拉列表元素
    selectBox.style.position='fixed';// 设置下拉列表的位置为固定
    selectBox.style.top='61px';// 设置下拉列表的顶部位置
    selectBox.style.left='158px';// 设置下拉列表的左侧位置
    selectBox.style.zIndex='3000';// 设置下拉列表的层级
    selectBox.style.fontSize='13px';// 设置下拉列表的字体大小
    selectBox.style.width='50px'// 设置下拉列表的宽度
    selectBox.classList.add("effect1");// 添加 CSS 类 effect1
    // 添加选项到下拉列表
    const options=[// 定义下拉列表的选项
        {value:'00714',text:'水分'},
        {value:'00711',text:'外观'},
        {value:'00713',text:'PH'},
        {value:'00717',text:'牛残'},
        {value:'00710',text:'鉴别'},
        {value:'00718',text:'蛋残'},
        {value:'00721',text:'内毒素'}
    ];
    options.forEach(option=>{// 遍历选项数组
        const opt=document.createElement('option');// 创建一个选项元素
        opt.value=option.value;// 设置选项的值
        opt.textContent=option.text;// 设置选项的文本内容
        selectBox.appendChild(opt);// 将选项添加到下拉列表中
    });

    // 将悬浮按钮和下拉列表添加到页面上
    document.body.appendChild(mainButton);// 将按钮添加到页面 body 中
    document.body.appendChild(selectBox);// 将下拉列表添加到页面 body 中

    // 发送POST请求的函数
    function sendPostRequest(jianxiang){// 定义发送 POST 请求的函数
        const url='http://192.168.100.113/pcis/a/lims/sampling/limsTestOrderManual/listData';// 设置请求 URL
        const data='reqCode=vp&pageNo=1&reqOrgOffice.officeName=%E7%8B%82%E7%8A%AC%E8%8B%97%E5%88%B6%E5%89%82%E8%BD%A6%E9%97%B4&reqOrg=SDJN01002&orderBy=a.own_batch+desc';// 设置请求数据
        const head={ 'Content-Type':'application/x-www-form-urlencoded' };// 设置请求头
        GM_xmlhttpRequest({// 发送 POST 请求
            method:'POST',// 设置请求方法为 POST
            url:url,// 设置请求 URL
            data:data,// 设置请求数据
            headers:head,// 设置请求头
            onload:function(response){// 设置请求成功时的回调函数
                try{
                    const jsonResponse=JSON.parse(response.responseText);// 解析返回的 JSON 数据
                    if(typeof jsonResponse==='object'&&jsonResponse.list&&Array.isArray(jsonResponse.list)){// 检查 JSON 数据结构是否正确
                        const items=jsonResponse.list.slice(0,20);// 获取前20个项

                        // 确保在每次创建新的 container 之前移除旧的 container
                        if(container){// 如果 container 存在
                            document.body.removeChild(container);// 移除旧的 container
                        }

                        // 创建一个新的 container
                        container=document.createElement('div');// 创建一个新的 div 元素
                        container.style.position='fixed';// 设置 container 的位置为固定
                        container.style.top=`${parseInt(mainButton.style.top)+25}px`;// 设置 container 的顶部位置
                        container.style.left=mainButton.style.left;// 设置 container 的左侧位置
                        container.style.zIndex='3000';// 设置 container 的层级
                        container.style.display='flex';// 设置 container 的显示方式为弹性布局
                        container.style.flexDirection='column';// 设置 container 的弹性布局方向为垂直

                        items.forEach(item=>{// 遍历返回的项
                            const id=item.id;// 获取项的 ID
                            const button=document.createElement('a');// 创建一个链接元素
                            button.href=`http://192.168.100.113/pcis/a/dist/index.html#/lims/show?readonly=false&id=${id}&itemId=${jianxiang}`;// 设置链接的 URL
                            button.textContent=item.ownBatch;// 设置链接的文本内容
                            button.target='_blank';// 设置链接在新窗口中打开
                            button.classList.add("effect1");// 添加 CSS 类 effect1
                            container.appendChild(button);// 将链接添加到 container 中
                        });

                        document.body.appendChild(container);// 将 container 添加到页面 body 中
                    }else{
                        console.error('JSON响应结构不正确,无法找到list数组');// 记录错误信息
                        alert('JSON响应结构不正确,无法找到list数组');// 显示警告信息
                    }
                }catch(error){
                    console.error('解析JSON失败:',error);// 记录错误信息
                    alert('解析JSON失败');// 显示警告信息
                }
            },
            onerror:function(response){// 设置请求失败时的回调函数
                console.error('请求失败:',response);// 记录错误信息
                alert('请求失败');// 显示警告信息
            }
        });
    }

    // 为悬浮按钮添加点击事件监听器
    mainButton.addEventListener('click',()=>{// 为按钮添加点击事件监听器
        const selectedValue=selectBox.value;// 获取下拉列表的选定值
        sendPostRequest(selectedValue);// 调用发送 POST 请求的函数
    });

    // 为下拉列表添加改变事件监听器
    selectBox.addEventListener('change',()=>{// 为下拉列表添加改变事件监听器
        if(container){// 如果 container 存在
            document.body.removeChild(container);// 移除 container
            // 清除 container 后重置为 null,确保下次使用时重新创建
            container=null;// 将 container 重置为 null
        }
    });

    // 添加样式
    const style=document.createElement('style');// 创建一个样式元素
    style.textContent=`\
        .effect1 {\
            cursor:pointer;\
            width:99px;\
            height:25px;\
            display:flex;\
            justify-content:center;\
            align-items:center;\
            border:1px solid pink;\
            border-radius:5px;\
            box-shadow:2px 2px 2px rgba(255,192,203,.4);\
            background-color:rgba(250, 235, 235,1);\
        }\
    `;
    document.head.appendChild(style);// 将样式添加到页面 head 中
})();