蓝湖

自动填充填写过的产品密码(不是蓝湖账户);查看产品页面窗口改变后帮助侧边栏更新高度

目前為 2020-09-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         蓝湖
// @version      1.0.0
// @description  自动填充填写过的产品密码(不是蓝湖账户);查看产品页面窗口改变后帮助侧边栏更新高度
// @author       sakura-flutter
// @namespace    https://github.com/sakura-flutter
// @compatible   chrome >= 80
// @compatible   firefox >= 75
// @run-at       document-start
// @match        https://lanhuapp.com/web/
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
  'use strict';
  const $ = document.querySelector.bind(document)

  /* 填充密码 */
  const oldPushState = unsafeWindow.history.pushState
  unsafeWindow.history.pushState = function(...args) {
      oldPushState.apply(unsafeWindow.history, args)
      console.warn('pushState')
      setTimeout(autofillPassword, 500)
  }

  unsafeWindow.addEventListener('DOMContentLoaded', () => {
      console.warn('DOMContentLoaded')
      autofillPassword()
  })
  unsafeWindow.addEventListener('hashchange', autofillPassword)

  function autofillPassword() {
      if (!location.hash.startsWith('#/item/project/door')) return
      const queryString = location.hash.includes('?') ? location.hash.split('?')[1] : ''
      if (!queryString) return
      const pid = new URLSearchParams(queryString).get('pid')
      // 确认登录按钮   密码框
      const [confirmEl, passwordEl] = [
          $('#project-door .mu-raised-button-wrapper'),
          $('#project-door .pass input'),
      ]
      if (!pid || !confirmEl || !passwordEl) return

      const pidPassword = GM_getValue('passwords', {})[pid]
      if (pidPassword) {
          passwordEl.value = pidPassword
          confirmEl.click()
      }

      // 点击后保存密码
      confirmEl.addEventListener('click', () => {
          const savedPassword = GM_getValue('passwords', {})
          const password = passwordEl.value
          GM_setValue('passwords', {
              ...savedPassword,
              [pid]: password,
          })
      })

  }

  /* 更新侧边栏高度 */
  window.addEventListener('resize', throttle(function() {
      const barEl = $('.flexible-bar')
      const modalEl = $('.flexible-modal')
      if (!barEl || !modalEl) return
      barEl.dispatchEvent(new MouseEvent('mousedown'))
      modalEl.dispatchEvent(new MouseEvent('mouseup'))
  }, 150))



  function throttle(fn, delay) {
      var t = null,
          begin = new Date().getTime();

      return function (...args) {
          var _self = this,
              cur = new Date().getTime();

          clearTimeout(t);

          if (cur - begin >= delay) {
              fn.apply(_self, args);
              begin = cur;
          } else {
              t = setTimeout(function () {
                  fn.apply(_self, args);
              }, delay);
          }
      }
  }
})();