Cookie Manager

For Developers Only. Control Cookies everywhere via DevTools

目前为 2023-05-18 提交的版本。查看 最新版本

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2022 CY Fung
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. */
  26. // ==UserScript==
  27. // @name Cookie Manager
  28. // @namespace http://tampermonkey.net/
  29. // @version 0.5
  30. // @description For Developers Only. Control Cookies everywhere via DevTools
  31. // @author CY Fung
  32. // @supportURL https://github.com/cyfung1031/userscript-supports
  33. // @match https://*/*
  34. // @match http://*/*
  35. // @icon https://github.com/cyfung1031/userscript-supports/blob/main/icons/cookie-manager.png?raw=true
  36. // @grant unsafeWindow
  37. // @license MIT
  38. // @require https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js#sha512=wT7uPE7tOP6w4o28u1DN775jYjHQApdBnib5Pho4RB0Pgd9y7eSkAV1BTqQydupYDB9GBhTcQQzyNMPMV3cAew==
  39. // ==/UserScript==
  40.  
  41. /* global Cookies */
  42.  
  43. /*
  44. usage:
  45. cook.set('hello-world',100)
  46. console.log(cook.get('hello-world'))
  47. cook.remove('hello-world')
  48. cook.myvar = 'abc'
  49. console.log(cook.myvar)
  50. cook.myvar = null
  51.  
  52. cook.get()
  53.  
  54. const api = cook.chef(null, { path: '/', domain: '.example.com' })
  55. const api = cook.chef({
  56. write: function (value, name) {
  57. return value.toUpperCase()
  58. }
  59. }, null)
  60. */
  61.  
  62. (function (Cookies) {
  63. 'use strict';
  64. // Your code here...
  65. if (unsafeWindow.cook) return
  66. const { get, set, remove } = Cookies
  67. function chefFunc(converter, attributes) {
  68. converter = converter ? Object.assign({}, this.converter, converter) : this.converter
  69. attributes = attributes ? Object.assign({}, this.attributes, attributes) : this.attributes
  70. return init(converter, attributes)
  71. }
  72. const target = {
  73. set: set.bind(Cookies),
  74. get: get.bind(Cookies),
  75. remove: remove.bind(Cookies),
  76. chef: chefFunc.bind(Cookies),
  77. replaceChef: (chef) => {
  78. Cookies = chef
  79. target.set = set.bind(Cookies)
  80. target.get = get.bind(Cookies)
  81. target.remove = remove.bind(Cookies)
  82. target.chef = chefFunc.bind(Cookies)
  83. }
  84. }
  85. unsafeWindow.cook = new Proxy(target, {
  86. get(target, prop) {
  87. if (prop in target) {
  88. return target[prop]
  89. }
  90. return Cookies.get(prop)
  91. },
  92. set(target, prop, val) {
  93. if (val === null) Cookies.remove(prop);
  94. else Cookies.set(prop, val);
  95. return true
  96. }
  97. })
  98. })(Cookies);