Export and Import Cookies

Allows you to import and export cookies from any website.

  1. // ==UserScript==
  2. // @name Export and Import Cookies
  3. // @version 1.0.1
  4. // @description Allows you to import and export cookies from any website.
  5. // @author yodaluca23
  6. // @license GNU GPLv3
  7. // @match *://*/*
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_setClipboard
  10. // @namespace https://greasyfork.org/users/1315976
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const signatureKey = 'cookie_manager_by_yodaluca23';
  17.  
  18. // Function to export cookies and copy them to clipboard
  19. function exportCookies() {
  20.  
  21. const cookiesExport = document.cookie.split('; ').map(cookie => {
  22. const [name, value] = cookie.split('=');
  23. return { name, value };
  24. });
  25.  
  26. const cookiesWithSignature = {
  27. cookies: cookiesExport,
  28. signature: signatureKey
  29. };
  30.  
  31. GM_setClipboard(JSON.stringify(cookiesWithSignature));
  32. alert("Cookies copied to clipboard.");
  33. }
  34.  
  35. // Function to import cookies from clipboard
  36. function importCookies() {
  37.  
  38. var cookiesLoading;
  39. var userInput = prompt("Please paste in your cookies previously exported:");
  40. console.log(userInput);
  41. if (userInput !== null && userInput !== "") {
  42. cookiesLoading = userInput;
  43. } else {
  44. return;
  45. }
  46.  
  47. try {
  48. const cookiesWithSignature = JSON.parse(cookiesLoading);
  49.  
  50. if (cookiesWithSignature.signature !== signatureKey) {
  51. alert("These cookies were not exported with this tool.");
  52. return;
  53. }
  54.  
  55. const cookiesImport = cookiesWithSignature.cookies;
  56.  
  57. cookiesImport.forEach(({ name, value }) => {
  58. document.cookie = `${name}=${value}; path=/; domain=${window.location.hostname}`;
  59. });
  60.  
  61. let refresh = confirm("Cookies have been imported, would you like to refresh the page now?");
  62. if (refresh) {
  63. location.reload();
  64. }
  65.  
  66. } catch (error) {
  67. alert("Error importing cookies: " + error.message);
  68. }
  69. }
  70.  
  71. // Register menu commands for exporting and importing cookies
  72. GM_registerMenuCommand("Export Cookies", exportCookies);
  73. GM_registerMenuCommand("Import Cookies", importCookies);
  74.  
  75. })();