Autocomplete On

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

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