Export and Import Cookies

Allows you to import and export cookies from any website.

当前为 2025-01-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Export and Import Cookies
  3. // @version 1.0
  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. if (userInput !== null) {
  41. cookiesLoading = userInput;
  42. }
  43.  
  44. try {
  45. const cookiesWithSignature = JSON.parse(cookiesLoading);
  46.  
  47. if (cookiesWithSignature.signature !== signatureKey) {
  48. alert("These cookies were not exported with this tool.");
  49. return;
  50. }
  51.  
  52. const cookiesImport = cookiesWithSignature.cookies;
  53.  
  54. cookiesImport.forEach(({ name, value }) => {
  55. document.cookie = `${name}=${value}; path=/; domain=${window.location.hostname}`;
  56. });
  57.  
  58. let refresh = confirm("Cookies have been imported, would you like to refresh the page now?");
  59. if (refresh) {
  60. location.reload();
  61. }
  62.  
  63. } catch (error) {
  64. alert("Error importing cookies: " + error.message);
  65. }
  66. }
  67.  
  68. // Register menu commands for exporting and importing cookies
  69. GM_registerMenuCommand("Export Cookies", exportCookies);
  70. GM_registerMenuCommand("Import Cookies", importCookies);
  71.  
  72. })();