fill the input and select of form as last time inputed automatically

This script supports SPA like vue。

目前為 2022-02-20 提交的版本,檢視 最新版本

  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)// // @require https://greasyfork.org/scripts/440334-jquery-like-spa-operation-library/code/jQuery-like%20SPA%20operation%20library.js?version= 1020492
  7. // @include http://*
  8. // @include https://*
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @author yechenyin
  12. // @license MIT
  13. // @version 1.0
  14. // ==/UserScript==
  15.  
  16. jQuery.fn.saveChangedValue = function () {
  17. let that = this
  18. that.on('change', function () {
  19. let href = 'inputed_' + location.href.replace(/\?.*/, '')
  20. let inputs = ''
  21. if (typeof GM_getValue === 'undefined')
  22. inputs = localStorage[href]
  23. else if (GM_getValue(href)) {
  24. inputs = GM_getValue(href)
  25. }
  26. if (inputs)
  27. inputs = JSON.parse(inputs)
  28. else
  29. inputs = {}
  30. //console.log(this.constructor.name)
  31. //console.log(Object.keys(this))
  32. let name = ''
  33. if (this.id)
  34. name = '#' + this.id
  35. else if ($(this).attr('name'))
  36. name = $(this).attr('name')
  37. else {
  38. for (let i = 0; i < that.length; i++) {
  39. if (this == that[i])
  40. name = i
  41. }
  42. }
  43. inputs[name] = $(this).val()
  44. if (typeof GM_setValue === 'undefined')
  45. localStorage[href] = JSON.stringify(inputs)
  46. else
  47. GM_setValue(href, JSON.stringify(inputs))
  48. //console.log(GM_getValue(href))
  49. })
  50. }
  51. jQuery.fn.recoverSavedValue = function () {
  52. this.inserted(function () {
  53. let that = this
  54. let href = 'inputed_' + location.href.replace(/\?.*/, '')
  55. console.log(GM_getValue(href))
  56. let inputs = ''
  57. if (typeof GM_getValue === 'undefined')
  58. inputs = localStorage[href]
  59. else if (GM_getValue(href)) {
  60. inputs = GM_getValue(href)
  61. }
  62. if (inputs)
  63. inputs = JSON.parse(inputs)
  64. else
  65. inputs = {}
  66. //console.log(inputs)
  67. if (Object.keys(inputs).length) {
  68. this.each(function () {
  69. let name = ''
  70. if (this.id)
  71. name = '#' + this.id
  72. else if ($(this).attr('name'))
  73. name = $(this).attr('name')
  74. else {
  75. for (let i = 0; i < that.length; i++) {
  76. if (this == that[i])
  77. name = i
  78. }
  79. }
  80. if (inputs.hasOwnProperty(name)) {
  81. $(this).val(inputs[name])
  82. }
  83. })
  84. }
  85. })
  86. }
  87.  
  88. window.onload = function () {
  89. $('input, select').recoverSavedValue()
  90. $('input, select').inserted(function () {
  91. $('input, select').saveChangedValue()
  92. })
  93. }