Cookie/Localstorage Manager

Import or export localstorage and cookies of any site

  1. // ==UserScript==
  2. // @name Cookie/Localstorage Manager
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Import or export localstorage and cookies of any site
  6. // @grant GM_registerMenuCommand
  7. // @grant GM_setClipboard
  8. // @include *
  9. // @license MIT
  10. // ==/UserScript==
  11. function exportAll() {
  12. let data = {
  13. localStorage: {},
  14. cookies: {}
  15. };
  16.  
  17. for (let i = 0; i < localStorage.length; i++) {
  18. const key = localStorage.key(i);
  19. data.localStorage[key] = localStorage.getItem(key);
  20. }
  21.  
  22. const cookies = document.cookie.split('; ');
  23. cookies.forEach(cookie => {
  24. const [name, value] = cookie.split('=');
  25. const cookieName = name.trim();
  26. const cookieValue = value.trim();
  27.  
  28. const cookieAttributes = document.cookie.split('; ').find(c => c.startsWith(cookieName));
  29. const attributes = cookieAttributes ? cookieAttributes.split('; ').slice(1) : [];
  30.  
  31. let expires = attributes.find(attr => attr.trim().toLowerCase().startsWith('expires='));
  32.  
  33. data.cookies[cookieName] = {
  34. value: cookieValue,
  35. expires: expires ? expires.split('=')[1] : null
  36. };
  37. });
  38.  
  39. const dataString = JSON.stringify(data);
  40. GM_setClipboard(dataString);
  41. alert('localStorage and cookies copied to clipboard!');
  42. }
  43.  
  44. function importAll() {
  45. const dataString = prompt('Paste the stringified JSON data to import into localStorage and cookies:');
  46. if (dataString) {
  47. try {
  48. const data = JSON.parse(dataString);
  49.  
  50. if (data.localStorage) {
  51. Object.entries(data.localStorage).forEach(([k, v]) => {
  52. localStorage.setItem(k, v);
  53. });
  54. }
  55.  
  56. if (data.cookies) {
  57. Object.entries(data.cookies).forEach(([k, { value, expires }]) => {
  58. let cookieString = `${k}=${value}; path=/`;
  59. if (expires) {
  60. const expirationDate = new Date(expires);
  61. cookieString += `; expires=${expirationDate.toUTCString()}`;
  62. }
  63. document.cookie = cookieString;
  64. });
  65. }
  66.  
  67. alert('localStorage and cookies imported successfully!');
  68. } catch (e) {
  69. alert('Failed to import data. Please make sure the input is valid JSON. Error: ' + e.message);
  70. console.error(e);
  71. }
  72. } else {
  73. alert('No data was entered.');
  74. }
  75. }
  76.  
  77. GM_registerMenuCommand("Copy localStorage and Cookies to Clipboard", exportAll);
  78. GM_registerMenuCommand("Import localStorage and Cookies from Clipboard", importAll);