Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Navigate to chrome://settings/searchEngines, then run the following script in the Console tab of Developer Tools.
- (function importSiteSearches() {
- // Function to process JSON data and import site searches
- function importFromJSON(jsonData) {
- let searchEngines;
- // Parse JSON data if it's a string
- try {
- searchEngines = typeof jsonData === 'string' ? JSON.parse(jsonData) : jsonData;
- } catch (error) {
- console.error('Invalid JSON format:', error);
- return;
- }
- // Validate that we have an array
- if (!Array.isArray(searchEngines)) {
- console.error('JSON data must be an array of search engines');
- return;
- }
- console.log(`Importing ${searchEngines.length} site searches...`);
- // Import each search engine with a delay to prevent issues
- searchEngines.forEach((engine, index) => {
- setTimeout(() => {
- const { name, shortcut, url } = engine;
- // Validate required fields
- if (!name || !shortcut || !url) {
- console.warn(`Skipping invalid entry at index ${index}:`, engine);
- return;
- }
- // Ensure URL contains %s placeholder
- if (!url.includes('%s')) {
- console.warn(`URL for "${name}" doesn't contain %s placeholder, skipping`);
- return;
- }
- try {
- // Use Chrome's internal API to add the search engine
- chrome.send('searchEngineEditStarted', [-1]);
- chrome.send('searchEngineEditCompleted', [name, shortcut, url]);
- console.log(`✓ Added: ${name} (${shortcut})`);
- } catch (error) {
- console.error(`Failed to add "${name}":`, error);
- }
- }, index * 200); // 200ms delay between each addition
- });
- console.log('Import process initiated. Check for completion messages above.');
- }
- // Create file input for JSON upload
- const fileInput = document.createElement('input');
- fileInput.type = 'file';
- fileInput.accept = '.json';
- fileInput.style.display = 'none';
- fileInput.onchange = function(event) {
- const file = event.target.files[0];
- if (!file) return;
- const reader = new FileReader();
- reader.onload = function(e) {
- try {
- const jsonData = e.target.result;
- importFromJSON(jsonData);
- } catch (error) {
- console.error('Error reading file:', error);
- }
- };
- reader.readAsText(file);
- // Clean up
- document.body.removeChild(fileInput);
- };
- // Add to DOM and trigger click
- document.body.appendChild(fileInput);
- fileInput.click();
- console.log('File dialog opened. Select your JSON file to import site searches.');
- })();
Advertisement
Add Comment
Please, Sign In to add comment