自动填充填写过的产品密码(不是蓝湖账户);查看产品页面窗口改变后帮助侧边栏更新高度
目前為
// ==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);
}
}
}
})();