MEST Sidebar

fast switch account cookies

当前为 2022-11-26 提交的版本,查看 最新版本

// ==UserScript==
// @name         MEST Sidebar
// @namespace    joyings.com.cn
// @version      0.1.3
// @description  fast switch account cookies
// @author       zmz125000
// @match        http://*/mest/*
// @icon         http://www.google.com/s2/favicons?domain=openwrt.org
// @grant        none
// @license      MIT
// @run-at       document-end
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js
// ==/UserScript==

(function () {
  'use strict';

  // Your code here...
  function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
  window.mini = true

  async function insertDiv() {
    var div = document.createElement('div');
    div.id = "mySidebar";
    div.className = "sidebar";
    div.setAttribute("onmouseover", "toggleSidebar");
    div.setAttribute("onmouseout", "toggleSidebar");

    var radio = document.createElement('div');
    radio.innerHTML = `
                <div class="InputContainer">
                    <input placeholder="Search..." />
                </div>
                <div class="radio">
                    <input label="原料" type="radio" name="materialType" value="material">
                    <input label="包装物料" type="radio" name="materialType" value="packaging">
                </div>
                `
    div.appendChild(radio)
    while (!$('#app')) {
      await sleep(200);
    }
    $('#app')[0].appendChild(div);

    var data = loadData();
    if (data && data['record']['orders']) {
      updateSidebar(data);
    }
    getLatestMat();
  }

  function getLatestMat() {
    fetch('https://api.jsonbin.io/v3/b/637eceb165b57a31e6c15f19/latest')
      .then((response) =>
        response.json()
      )
      .then((data) => {
        updateSidebar(data);
        saveData(data);
        return data;
      })
      .catch(console.error);
  }

  function updateSidebar(data) {
    if ($('ul', $('#mySidebar')[0])[0]) {
      $('#mySidebar')[0].removeChild($('ul', $('#mySidebar')[0])[0]);
    }
    var chkcount = 0;
    var topUL = document.createElement('ul');
    $('#mySidebar')[0].appendChild(topUL);
    console.log(data);
    var orderArray = data['record']['orders'];
    for (let order of orderArray) {
      let oElm = document.createElement('li');
      oElm.className = "order";
      topUL.appendChild(oElm);

      let ielm = document.createElement("input");
      ielm.className = "toggle";
      ielm.type = "radio";
      ielm.name = "order";
      ielm.id = "collapsible" + chkcount;
      oElm.appendChild(ielm);
      let lelm = document.createElement("label");
      lelm.htmlFor = "collapsible" + chkcount++;
      lelm.className = "lbl-toggle";
      lelm.innerText = order['order'];
      oElm.appendChild(lelm);
      // osElm customer 下的 orders
      let gElm = document.createElement('div');
      gElm.className = "grid-container";
      oElm.appendChild(gElm);
      for (let item of order['material']) {
        let iElm = document.createElement('div');
        iElm.className = "item";
        gElm.appendChild(iElm);
        for (let property in item) {
          if (property != 'code') {
            let selm = document.createElement('span');
            selm.innerText = item[property];
            iElm.appendChild(selm);
          }
        }
      }
    }
  }

  // 保存 读取 导入 切换 删除cookie
  // cookieData={ name:cookie,...}
  function loadData() {
    try {
      return JSON.parse(localStorage["matjson"]);
    } catch (e) {
      localStorage["matjson"] = null;
      return {};
    }
  }

  function saveData(data) {
    localStorage["matjson"] = JSON.stringify(data);
  }

  function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) {
      return;
    }
    style = document.createElement('style');
    // style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
  }

  // init
  insertDiv();

  addGlobalStyle(`
        .sidebar {
            height: 100%;
            position: fixed;
            z-index: 5000;
            top: 0;
            right: 0;
            background-color: #111;
            max-width: 0px;
            transition: all 0.5s;
            overflow-x: hidden;
            white-space: nowrap;
            color: #ffffff;
            background: #009578;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.35);
            padding: 5px;
        }

        #app .sidebar {
            position: absolute;
            top: 0;
            right: 0px;
        }

        #app {
            margin-right: 5px;
            transition: margin-right 0.5s;
        }

        .sidebar .grid-container {
            max-height: 0px;
            transition: all 0.3s ease-in-out;
        }

        .sidebar .grid-container div {
            margin: 5px;
            display: grid;
            grid-template-columns: auto auto auto auto;
            grid-gap: 10px;
            background-color: #2196F3;
            padding: 8px;
            border-radius: 5px;
            color: aliceblue;
        }

        .sidebar .grid-container div:hover {
            background-color: rgb(0, 102, 255);
        }

        .order {
            width: auto;
            background-color: azure;
            border-radius: 5px;
            color: #111;
            overflow-y: hidden;
            font-size: smaller;
        }

        .order::-webkit-scrollbar {
            display: none;
        }

        .orderNo {
            padding: 5px;
            font-size: larger;
        }

        .sidebar:hover {
            max-width: 600px;
        }

        .order:hover {
            background-color: beige;
        }

        .sidebar li {
            list-style: none;
            margin-left: 15px;
            margin-right: 15px;
            margin: 10px;
        }

        .sidebar ul {
            padding: 0;
            list-style-type: none;
        }

        .sidebar label {
            margin: 10px;
        }

        .sidebar span {
            margin-left: 2px;
        }

        .sidebar::-webkit-scrollbar {
            display: none;
        }

        .radio {
            margin: 10px;
            padding: 0;
            position: relative;
        }

        .radio input {
            width: auto;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            outline: none;
            cursor: pointer;
            border-radius: 5px;
            padding: 4px 8px;
            background: #1e1f28;
            color: white;
            font-size: 14px;
            transition: all 100ms linear;
        }

        .radio input:checked {
            background-image: linear-gradient(180deg, #95d891, #74bbad);
            color: #fff;
            box-shadow: 0 1px 1px #0000002e;
            text-shadow: 0 1px 0px #79485f7a;
        }

        .radio input:before {
            content: attr(label);
            display: inline-block;
            text-align: center;
            width: 100%;
        }

        .InputContainer input {
            background-color: #e3edf7;
            padding: 8px 16px;
            border: none;
            display: block;
            font-family: 'Orbitron', sans-serif;
            font-weight: 600;
            color: #a9b8c9;
            transition: all 240ms ease-out;
            width: 100%;
        }

        .InputContainer input::placeholder {
            color: #6d7f8f;
        }

        .InputContainer input:focus {
            outline: none;
            color: #6d7f8f;
            background-color: #eff5fa;
        }

        .InputContainer {
            margin: 5px;
            --top-shadow: inset 1px 1px 3px #c5d4e3, inset 2px 2px 6px #c5d4e3;
            --bottom-shadow: inset -2px -2px 4px rgba(255, 255, 255, 0.7);
            position: relative;
            border-radius: var(--border-radius);
            overflow: hidden;
        }

        .InputContainer:before,
        .InputContainer:after {
            pointer-events: none;
            width: 100%;
            height: 100%;
            position: absolute;
        }

        .InputContainer:before {
            box-shadow: var(--bottom-shadow);
        }

        .InputContainer:after {
            box-shadow: var(--top-shadow);
        }

        .order input[type="radio"] {
            display: none;
        }

        .lbl-toggle {
            display: block;
            cursor: pointer;
        }

        .toggle:checked+.lbl-toggle+.grid-container {
            max-height: 900px;
        }
    `);
})();