ChatGPT Tools Export/Import

Export and import localStorage keys rulesProfiles and lorebookEntries

  1. // ==UserScript==
  2. // @name ChatGPT Tools Export/Import
  3. // @namespace ChatGPT Tools by Vishanka
  4. // @version 1.0
  5. // @description Export and import localStorage keys rulesProfiles and lorebookEntries
  6. // @author Vishanka
  7. // @match https://chatgpt.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create an export button
  15. const exportButton = document.createElement('button');
  16. exportButton.innerText = 'Export Data';
  17. exportButton.style.position = 'fixed';
  18. exportButton.style.top = '10px';
  19. exportButton.style.right = '10px';
  20. exportButton.style.zIndex = '10000';
  21. document.body.appendChild(exportButton);
  22.  
  23. // Create an import button
  24. const importButton = document.createElement('button');
  25. importButton.innerText = 'Import Data';
  26. importButton.style.position = 'fixed';
  27. importButton.style.top = '50px';
  28. importButton.style.right = '10px';
  29. importButton.style.zIndex = '10000';
  30. document.body.appendChild(importButton);
  31.  
  32. // Function to export data
  33. exportButton.addEventListener('click', () => {
  34. const keysToExport = ['rulesProfiles', 'lorebookEntries'];
  35. const exportedData = {};
  36.  
  37. keysToExport.forEach(key => {
  38. if (localStorage.getItem(key)) {
  39. exportedData[key] = JSON.parse(localStorage.getItem(key));
  40. }
  41. });
  42.  
  43. const blob = new Blob([JSON.stringify(exportedData, null, 2)], { type: 'application/json' });
  44. const url = URL.createObjectURL(blob);
  45. const a = document.createElement('a');
  46. a.href = url;
  47. a.download = 'exported_data.json';
  48. a.click();
  49. URL.revokeObjectURL(url);
  50. });
  51.  
  52. // Function to import data
  53. importButton.addEventListener('click', () => {
  54. const input = document.createElement('input');
  55. input.type = 'file';
  56. input.accept = 'application/json';
  57. input.addEventListener('change', (event) => {
  58. const file = event.target.files[0];
  59. const reader = new FileReader();
  60.  
  61. reader.onload = function(e) {
  62. try {
  63. const importedData = JSON.parse(e.target.result);
  64.  
  65. Object.keys(importedData).forEach(key => {
  66. if (importedData[key]) {
  67. localStorage.setItem(key, JSON.stringify(importedData[key]));
  68. }
  69. });
  70.  
  71. alert('Data imported successfully!');
  72. } catch (error) {
  73. alert('Failed to import data: Invalid JSON file.');
  74. }
  75. };
  76.  
  77. reader.readAsText(file);
  78. });
  79.  
  80. input.click();
  81. });
  82. })();