Greasy Fork 还支持 简体中文。

foreignStorage

2/16/2022, 9:49:50 PM

  1. // ==UserScript==
  2. // @name foreignStorage
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @grant none
  6. // @version 1.0
  7. // @author -
  8. // @description 2/16/2022, 9:49:50 PM
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. {
  13. const url = localStorage.foreign_storage_dev
  14. ? 'http://localhost:3000/'
  15. : 'https://foreign-storage.herokuapp.com/'
  16. window.foreignStorage = {
  17. async setItem(key, str) {
  18. if (arguments.length < 2 || typeof key != 'string' || typeof str != 'string') {
  19. throw new Error('Exactly two arguments of type string required')
  20. }
  21. const options = { method: 'POST', headers: { key }, body: str }
  22. const answer = await fetch(url + 'setItem', options)
  23. .then(resp => resp.json())
  24.  
  25. if (answer.success) return true
  26. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  27. },
  28.  
  29. async getItem(key) {
  30. if (arguments.length < 1 || typeof key != 'string') {
  31. throw new Error('One argument of type string required')
  32. }
  33. const options = { method: 'GET', headers: { key } }
  34. const answer = await fetch(url + 'getItem', options)
  35. .then(resp => resp.json())
  36.  
  37. if (answer.errors?.[0] == 'undefined') return undefined
  38. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  39. return answer
  40. },
  41.  
  42. async removeItem(key) {
  43. if (arguments.length < 1 || typeof key != 'string') {
  44. throw new Error('One argument of type string required')
  45. }
  46. const options = { method: 'DELETE', headers: { key } }
  47. const answer = await fetch(url + 'removeItem', options)
  48. .then(resp => resp.json())
  49.  
  50. if (answer.success) return true
  51. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  52. },
  53.  
  54. async clear() {
  55. const answer = await fetch(url + 'clear', { method: 'DELETE' })
  56. .then(resp => resp.json())
  57.  
  58. if (answer.success) return true
  59. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  60. },
  61.  
  62. async getLength() {
  63. const answer = await fetch(url + 'getLength')
  64. .then(resp => resp.json())
  65.  
  66. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  67. return answer
  68. },
  69.  
  70. async listKeys() {
  71. const answer = await fetch(url + 'listKeys')
  72. .then(resp => resp.json())
  73.  
  74. if (answer.errors) throw new Error(answer.errors?.join('; ') || answer)
  75. return answer
  76. }
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83.