Password Revealer

Shows passwords on mouse hover or focus

目前为 2014-10-25 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Password Revealer
  3. // @namespace http://userscripts.org/users/23652
  4. // @description Shows passwords on mouse hover or focus
  5. // @include http://*
  6. // @include https://*
  7. // @include file:///*
  8. // @exclude file:///*/perf.html*
  9. // @copyright JoeSimmons
  10. // @version 1.0.51
  11. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  12. // ==/UserScript==
  13. (function() {
  14.  
  15.  
  16.  
  17.  
  18. var show_only_on_click = false; // only show passwords when you click on the field
  19.  
  20.  
  21.  
  22.  
  23.  
  24. var handlers = [];
  25.  
  26. function toText(e) {
  27. e = e.target;
  28.  
  29. if (e.tagName === 'INPUT' && e.type === 'password') {
  30. // save the cursor position
  31. e.caretStart = e.selectionStart;
  32. e.caretEnd = e.selectionEnd;
  33.  
  34. // switch it to text, but make it remember it was a password type
  35. e.type = 'text';
  36. e.oldType = 'password';
  37.  
  38. // the selection range gets reset when we switch its type, so let's fix it
  39. e.setSelectionRange(e.caretStart, e.caretEnd);
  40. }
  41. }
  42.  
  43. function toPassword(e) {
  44. e = e.target;
  45.  
  46. if (e.tagName === 'INPUT' && e.oldType === 'password') {
  47. // save the cursor position
  48. e.caretStart = e.selectionStart;
  49. e.caretEnd = e.selectionEnd;
  50.  
  51. // switch it to a password field
  52. e.type = 'password';
  53.  
  54. // the selection range gets reset when we switch its type, so let's fix it
  55. e.setSelectionRange(e.caretStart, e.caretEnd);
  56. e.setSelectionRange(e.caretStart, e.caretEnd);
  57.  
  58. // let's clean up our now-unused properties
  59. delete e.caretStart;
  60. delete e.caretEnd;
  61. delete e.oldType;
  62. }
  63. }
  64.  
  65. function addHandlers() {
  66. var fields = document.querySelectorAll('input[type="password"]'), i, field;
  67.  
  68. for (i = 0; field = fields[i]; i += 1) {
  69. if (handlers.indexOf(field) === -1) {
  70. field.addEventListener('focus', toText, false);
  71. field.addEventListener('blur', toPassword, false);
  72. handlers.push(field);
  73. }
  74. }
  75. }
  76.  
  77. if (show_only_on_click === false) {
  78. window.addEventListener('mouseover', toText, false);
  79. window.addEventListener('mouseout', toPassword, false);
  80. } else {
  81. window.setInterval(addHandlers, 1000);
  82. }
  83.  
  84. }());