蓝湖 lanhu

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         蓝湖 lanhu
// @version      1.0.1
// @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')
      setTimeout(autofillPassword, 500)
  })

  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()
      }

      // 蓝湖刷新会重新跳转到输入密码页 pushState与DOMContentLoaded 可能会导致重复注册事件
      // 标记一下事件
      if (confirmEl.dataset.addedHandler) return
      confirmEl.dataset.addedHandler = '1'

      // 点击后保存密码
      confirmEl.addEventListener('mousedown', savePassword)
      // 回车键保存密码
      passwordEl.addEventListener('keydown', event => {
          if (event.keyCode !== 13) return
          savePassword()
      })

      function savePassword() {
          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);
          }
      }
  }
})();