使用上次输入的值自动填写表格

对所有网站生效,支持动态插入的input和select。浏览器需要安装Tampermonkey或者Greasemonkey扩展,安卓手机浏览器推荐Yandex或者Kiwi浏览器。支持SPA(比如vue)

当前为 2022-02-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name fill the input and select of form as last time inputed automatically
  3. // @name:zh-CN 使用上次输入的值自动填写表格
  4. // @namespace http://tampermonkey.net/
  5. // @description This script supports SPA like vue。
  6. // @description:zh-CN 对所有网站生效,支持动态插入的input和select。浏览器需要安装Tampermonkey或者Greasemonkey扩展,安卓手机浏览器推荐Yandex或者Kiwi浏览器。支持SPA(比如vue)
  7. // @require https://greasyfork.org/scripts/440334-jquery-like-spa-operation-library/code/jQuery-like%20SPA%20operation%20library.js?version= 1020513
  8. // @include http://*
  9. // @include https://*
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @author yechenyin
  13. // @license MIT
  14. // @version 1.0.2
  15. // ==/UserScript==
  16.  
  17. jQuery.fn.saveChangedValue = function () {
  18. let that = this
  19. that.on('change', function () {
  20. let href = 'inputed_' + location.href.replace(/\?.*/, '')
  21. let inputs = ''
  22. if (typeof GM_getValue === 'undefined')
  23. inputs = localStorage[href]
  24. else if (GM_getValue(href)) {
  25. inputs = GM_getValue(href)
  26. }
  27. if (inputs)
  28. inputs = JSON.parse(inputs)
  29. else
  30. inputs = {}
  31. //console.log(this.constructor.name)
  32. //console.log(Object.keys(this))
  33. let name = ''
  34. if (this.id)
  35. name = '#' + this.id
  36. else if ($(this).attr('name'))
  37. name = $(this).attr('name')
  38. else {
  39. for (let i = 0; i < that.length; i++) {
  40. if (this == that[i])
  41. name = i
  42. }
  43. }
  44. inputs[name] = $(this).val()
  45. if (typeof GM_setValue === 'undefined')
  46. localStorage[href] = JSON.stringify(inputs)
  47. else
  48. GM_setValue(href, JSON.stringify(inputs))
  49. //console.log(GM_getValue(href))
  50. })
  51. }
  52. jQuery.fn.recoverSavedValue = function () {
  53. this.inserted(function () {
  54. let that = this
  55. let href = 'inputed_' + location.href.replace(/\?.*/, '')
  56. //console.log(GM_getValue(href))
  57. let inputs = ''
  58. if (typeof GM_getValue === 'undefined')
  59. inputs = localStorage[href]
  60. else if (GM_getValue(href)) {
  61. inputs = GM_getValue(href)
  62. }
  63. if (inputs)
  64. inputs = JSON.parse(inputs)
  65. else
  66. inputs = {}
  67. //console.log(inputs)
  68. if (Object.keys(inputs).length) {
  69. this.each(function () {
  70. let name = ''
  71. if (this.id)
  72. name = '#' + this.id
  73. else if ($(this).attr('name'))
  74. name = $(this).attr('name')
  75. else {
  76. for (let i = 0; i < that.length; i++) {
  77. if (this == that[i])
  78. name = i
  79. }
  80. }
  81. if (inputs.hasOwnProperty(name)) {
  82. $(this).val(inputs[name])
  83. }
  84. })
  85. }
  86. })
  87. }
  88.  
  89. window.onload = function () {
  90. $('input, select').recoverSavedValue()
  91. $('input, select').inserted(function () {
  92. $('input, select').saveChangedValue()
  93. })
  94. }