Autocomplete On

Searches for autocomplete attributes in the page and sets the value to on.

当前为 2014-08-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Autocomplete On
  3. // @namespace http://userscripts.org/scripts/show/7347
  4. // @namespace https://greasyfork.org/scripts/4256-autocomplete-on
  5. // @description Searches for autocomplete attributes in the page and sets the value to on.
  6. // @include *
  7. // @version 10.0
  8. // ==/UserScript==
  9. location.replace('javascript:(' + function() {
  10. function enableAutocomplete()
  11. {
  12. /* Get all autocomplete attributes */
  13. var nodeSnapshot = document.evaluate('//*[@autocomplete]', document, null,
  14. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null),
  15. numNodeValues = nodeSnapshot.snapshotLength - 1,
  16. focusedAndBlurred = false,
  17. formNode = null,
  18. numFormElements = 0,
  19. scrollX = window.pageXOffset,
  20. scrollY = window.pageYOffset,
  21. activeElement = document.activeElement;
  22. /* Loop through all the attributes found in the snapshot */
  23. for (var i = numNodeValues; i >= 0; i--) {
  24. /*
  25. Get the attribute snapshot object and
  26. Change the attribute value to "on"
  27. */
  28. nodeSnapshot.snapshotItem(i).setAttribute('autocomplete', 'on');
  29. /* If element is of type text and autocomplete is set, it's probably the username */
  30. if (nodeSnapshot.snapshotItem(i).getAttribute('type') == 'text') {
  31. /*
  32. Blurring and focusing allows gecko to make suggestions
  33. immediately when double clicking username
  34. */
  35. nodeSnapshot.snapshotItem(i).focus();
  36. nodeSnapshot.snapshotItem(i).blur();
  37. focusedAndBlurred = true;
  38. }
  39. /* Remember the form node if autocomplete was set on it */
  40. if (nodeSnapshot.snapshotItem(i).nodeName.toLowerCase() == 'form') {
  41. formNode = nodeSnapshot.snapshotItem(i);
  42. numFormElements = formNode.elements.length - 1;
  43. }
  44. }
  45. /*
  46. If formNode was set and username was not focused and blurred,
  47. then autocomplete was only set on the form node
  48. */
  49. if (formNode != null && ! focusedAndBlurred) {
  50. for (var i = numFormElements; i >= 0; i--) {
  51. /* Blur and focus every text field in the form ending with the first field */
  52. if (formNode.elements[i].getAttribute('type') == 'text') {
  53. formNode.elements[i].focus();
  54. formNode.elements[i].blur();
  55. focusedAndBlurred = true;
  56. }
  57. }
  58. }
  59. /*
  60. If form fields were focused and blurred, return to the original
  61. scroll position and refocus the active element
  62. */
  63. if (focusedAndBlurred) {
  64. activeElement.focus();
  65. window.scrollTo(scrollX, scrollY);
  66. }
  67. }
  68. function newSubmit()
  69. {
  70. enableAutocomplete();
  71. if (this._submit !== undefined) {
  72. this._submit();
  73. }
  74. }
  75. /* Override DOM submit */
  76. HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
  77. HTMLFormElement.prototype.submit = newSubmit;
  78. /* Run enable on form submit and page load */
  79. window.addEventListener('submit', newSubmit, true);
  80. window.addEventListener('load', enableAutocomplete, false);
  81. } + ')()');