TIX007

鼠标悬停密码框时显示明文密码

  1. // ==UserScript==
  2. // @license MIT
  3. // @name TIX007
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.0
  6. // @description 鼠标悬停密码框时显示明文密码
  7. // @author TIX007
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 处理密码框的函数
  16. function handlePasswordFields(passwordField) {
  17. let originalType = 'password';
  18. // 鼠标悬停时显示密码
  19. passwordField.addEventListener('mouseover', function() {
  20. if (this.type === 'password') {
  21. originalType = 'password';
  22. this.type = 'text';
  23. }
  24. });
  25.  
  26. // 鼠标离开时恢复
  27. passwordField.addEventListener('mouseout', function() {
  28. if (this.type === 'text') {
  29. this.type = originalType;
  30. }
  31. });
  32. }
  33.  
  34. // 初始处理现有密码框
  35. document.querySelectorAll('input[type="password"]').forEach(handlePasswordFields);
  36.  
  37. // 观察DOM变化处理动态加载的密码框
  38. const observer = new MutationObserver(mutations => {
  39. mutations.forEach(mutation => {
  40. mutation.addedNodes.forEach(node => {
  41. if (node.nodeType === 1) { // Element node
  42. const passwordFields = node.querySelectorAll('input[type="password"]');
  43. passwordFields.forEach(handlePasswordFields);
  44. }
  45. });
  46. });
  47. });
  48.  
  49. observer.observe(document.body, {
  50. childList: true,
  51. subtree: true
  52. });
  53. })();