mws

自用工具库

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/445645/1124334/mws.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name            Mws Script Utils
// @namespace       https://rachpt.cn/
// @description     Custom Functions for Mws Scripts
// @version    	    1.0.3
// @author          rachpt (https://github.com/rachpt)
// @license         MIT
// @grant           none
// ==/UserScript==

var mws = new (class {
  sleep(milliseconds) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve();
      }, milliseconds);
    });
  }
  async _Step(selector, callback, need_content, timeout, milliseconds) {
    while (timeout--) {
      if (document.querySelector(selector) === null) {
        await this.sleep(milliseconds);
        continue;
      } else {
        if (need_content) {
          if (document.querySelector(selector).innerText.length == 0) {
            await this.sleep(milliseconds);
            continue;
          }
        }
      }
      break;
    }

    callback(selector);
  }
  wait(selector, timeout = Infinity, milliseconds = 300, need_content = false) {
    return new Promise(resolve => {
      this._Step(
        selector,
        function (selector) {
          resolve(document.querySelector(selector));
        },
        need_content,
        timeout,
        milliseconds,
      );
    });
  }

  _ReadFileAsync(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onload = () => {
        resolve(reader.result);
      };
      reader.onerror = reject;
      reader.readAsDataURL(file);
    });
  }

  async _Img2Base64(url) {
    try {
      const imgBlob = await fetch(url).then(res => res.blob());
      return await this._ReadFileAsync(imgBlob);
    } catch (error) {
      return 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzODQgNTEyIj4gPHBhdGggZmlsbD0iIzRFNTA1NSIgZD0iTTIyNCAxMzZWMEgyNEMxMC43IDAgMCAxMC43IDAgMjR2NDY0YzAgMTMuMyAxMC43IDI0IDI0IDI0aDMzNmMxMy4zIDAgMjQtMTAuNyAyNC0yNFYxNjBIMjQ4Yy0xMy4yIDAtMjQtMTAuOC0yNC0yNHptMTYwLTE0LjF2Ni4xSDI1NlYwaDYuMWM2LjQgMCAxMi41IDIuNSAxNyA3bDk3LjkgOThjNC41IDQuNSA3IDEwLjYgNyAxNi45eiI+PC9wYXRoPiA8L3N2Zz4=';
    }
  }
  /**
   * 生成带 Badge 的网站 Icon
   * @param {string} url Icon 地址
   * @param {string} env 环境文字
   * @param {string} color 文字颜色, 默认黑色
   * @param {string} bgColor Badge背景色, 默认金色
   * @returns
   */
  async genBadgedFavicon(url, env, color = '#fff', bgColor = '#ff9800') {
    const img = await this._Img2Base64(url);
    return `data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32">
    <foreignObject x="0" y="0" width="100%" height="100%">
        <body xmlns="http://www.w3.org/1999/xhtml">
            <style>
                html,
                body {
                    height: 100%;
                    margin: 0;
                    text-align: center;
                }
                img {
                    display: block;
                    width: 100%;
                    height: 100%;
                    object-fit: contain;
                }
                strong {
                    position: absolute;
                    bottom: 0;
                    left: 50%;
                    transform: translateX(-50%);
                    text-transform: uppercase;
                    background-color: ${bgColor};
                    color: ${color};
                    padding: 0px 1px;
                    border-radius: 2px;
                    font-size: 11px;
                    box-sizing: border-box;
                    max-width: 100%;
                    width: max-content;
                    height: 16px;
                    line-height: 16px;
                    word-break: break-all;
                    overflow: hidden;
                }
            </style>
            <strong>${env}</strong>
            <img src='${img}'></img>
        </body>
      </foreignObject>
    </svg>`
      .replace(/\n/g, '')
      .replace(/\t/g, '')
      .replace(/#/g, '%23');
  }
})();