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

This script supports SPA like vue。

Από την 22/02/2022. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name            fill the input and select of form as last time inputed automatically
// @name:zh-CN 使用上次输入的值自动填写表格
// @namespace  http://tampermonkey.net/
// @description  This script supports SPA like vue。
// @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= 1020513
// @include         http://*
// @include         https://*
// @grant           GM_setValue
// @grant           GM_getValue
// @author         yechenyin
// @license         MIT
// @version       1.0.2
// ==/UserScript==

jQuery.fn.saveChangedValue = function () {
  let that = this
  that.on('change', function () {
    let href = 'inputed_' + location.href.replace(/\?.*/, '')
    let inputs = ''
    if (typeof GM_getValue === 'undefined')
      inputs = localStorage[href]
    else if (GM_getValue(href)) {
      inputs = GM_getValue(href)
    }
    if (inputs)
      inputs = JSON.parse(inputs)
    else
      inputs = {}
    //console.log(this.constructor.name)
    //console.log(Object.keys(this))
    let name = ''
    if (this.id)
      name = '#' + this.id
    else if ($(this).attr('name'))
      name = $(this).attr('name')
    else {
      for (let i = 0; i < that.length; i++) {
        if (this == that[i])
          name = i
      }
    }
    inputs[name] = $(this).val()
    if (typeof GM_setValue === 'undefined')
      localStorage[href] = JSON.stringify(inputs)
    else
      GM_setValue(href, JSON.stringify(inputs))
    //console.log(GM_getValue(href))
  })
}
jQuery.fn.recoverSavedValue = function () {
  this.inserted(function () {
    let that = this
    let href = 'inputed_' + location.href.replace(/\?.*/, '')
    //console.log(GM_getValue(href))
    let inputs = ''
    if (typeof GM_getValue === 'undefined')
      inputs = localStorage[href]
    else if (GM_getValue(href)) {
      inputs = GM_getValue(href)
    }
    if (inputs)
      inputs = JSON.parse(inputs)
    else
      inputs = {}
    //console.log(inputs)
    if (Object.keys(inputs).length) {
      this.each(function () {
        let name = ''
        if (this.id)
          name = '#' + this.id
        else if ($(this).attr('name'))
          name = $(this).attr('name')
        else {
          for (let i = 0; i < that.length; i++) {
            if (this == that[i])
              name = i
          }
        }
        if (inputs.hasOwnProperty(name)) {
          $(this).val(inputs[name])
        }
      })
    }
  })
}

window.onload = function () {
  $('input, select').recoverSavedValue()
  $('input, select').inserted(function () {
    $('input, select').saveChangedValue()
  })
}