cmagnuson

Import site searches into Chrome

Jun 7th, 2025
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | Software | 0 0
  1. //Navigate to chrome://settings/searchEngines, then run the following script in the Console tab of Developer Tools.
  2.  
  3. (function importSiteSearches() {
  4. // Function to process JSON data and import site searches
  5. function importFromJSON(jsonData) {
  6. let searchEngines;
  7.  
  8. // Parse JSON data if it's a string
  9. try {
  10. searchEngines = typeof jsonData === 'string' ? JSON.parse(jsonData) : jsonData;
  11. } catch (error) {
  12. console.error('Invalid JSON format:', error);
  13. return;
  14. }
  15.  
  16. // Validate that we have an array
  17. if (!Array.isArray(searchEngines)) {
  18. console.error('JSON data must be an array of search engines');
  19. return;
  20. }
  21.  
  22. console.log(`Importing ${searchEngines.length} site searches...`);
  23.  
  24. // Import each search engine with a delay to prevent issues
  25. searchEngines.forEach((engine, index) => {
  26. setTimeout(() => {
  27. const { name, shortcut, url } = engine;
  28.  
  29. // Validate required fields
  30. if (!name || !shortcut || !url) {
  31. console.warn(`Skipping invalid entry at index ${index}:`, engine);
  32. return;
  33. }
  34.  
  35. // Ensure URL contains %s placeholder
  36. if (!url.includes('%s')) {
  37. console.warn(`URL for "${name}" doesn't contain %s placeholder, skipping`);
  38. return;
  39. }
  40.  
  41. try {
  42. // Use Chrome's internal API to add the search engine
  43. chrome.send('searchEngineEditStarted', [-1]);
  44. chrome.send('searchEngineEditCompleted', [name, shortcut, url]);
  45. console.log(`✓ Added: ${name} (${shortcut})`);
  46. } catch (error) {
  47. console.error(`Failed to add "${name}":`, error);
  48. }
  49. }, index * 200); // 200ms delay between each addition
  50. });
  51.  
  52. console.log('Import process initiated. Check for completion messages above.');
  53. }
  54.  
  55. // Create file input for JSON upload
  56. const fileInput = document.createElement('input');
  57. fileInput.type = 'file';
  58. fileInput.accept = '.json';
  59. fileInput.style.display = 'none';
  60.  
  61. fileInput.onchange = function(event) {
  62. const file = event.target.files[0];
  63. if (!file) return;
  64.  
  65. const reader = new FileReader();
  66. reader.onload = function(e) {
  67. try {
  68. const jsonData = e.target.result;
  69. importFromJSON(jsonData);
  70. } catch (error) {
  71. console.error('Error reading file:', error);
  72. }
  73. };
  74. reader.readAsText(file);
  75.  
  76. // Clean up
  77. document.body.removeChild(fileInput);
  78. };
  79.  
  80. // Add to DOM and trigger click
  81. document.body.appendChild(fileInput);
  82. fileInput.click();
  83.  
  84. console.log('File dialog opened. Select your JSON file to import site searches.');
  85. })();
Tags: chrome
Advertisement
Add Comment
Please, Sign In to add comment