Skip to content

Instantly share code, notes, and snippets.

@ardaorkin
Last active December 28, 2024 13:44
Show Gist options
  • Save ardaorkin/40400fd29e7381845fc420a6235bcdaa to your computer and use it in GitHub Desktop.
Save ardaorkin/40400fd29e7381845fc420a6235bcdaa to your computer and use it in GitHub Desktop.
Recursive script to delete all Chrome bookmarks via the browser's UI.
/**
* Script to recursively delete all bookmarks using the Chrome Bookmarks Manager UI.
* This script continues to run until no more bookmarks are available.
*
* Instructions:
* 1. Open Chrome Bookmarks Manager:
* - Go to `chrome://bookmarks` in your Chrome browser.
*
* 2. Open Developer Tools:
* - Right-click anywhere on the page and select "Inspect" OR
* - Press `Ctrl+Shift+I` (Windows/Linux) or `Cmd+Option+I` (Mac).
*
* 3. Go to the Console Tab:
* - Click on the "Console" tab in the Developer Tools.
*
* 4. Paste This Script:
* - Copy the entire script and paste it into the Console.
* - Press `Enter` to execute the script.
*
* 5. Let It Run:
* - The script will begin deleting bookmarks recursively.
* - **Do not close or navigate away** from the page until the script completes.
*
* 6. Important Notes:
* - **Backup Your Bookmarks First!**
* This script permanently deletes bookmarks and cannot be undone.
* - Delays are included to ensure Chrome processes each action properly. Do not interrupt the process.
*
* 7. After Completion:
* - Refresh the page (`Ctrl+R` or `Cmd+R`) to confirm all bookmarks are deleted.
*/
async function deleteAllBookmarks() {
const bookmarks = [...document.querySelector("body > bookmarks-app")
.shadowRoot.querySelector("#main-container > bookmarks-list")
.shadowRoot.querySelectorAll("[id*=bookmark]")];
if (bookmarks.length === 0) {
console.log("No more bookmarks to delete.");
return; // Exit when no bookmarks are left
}
for (const el of bookmarks) {
const btn = el.shadowRoot.querySelector("#menuButton");
if (!btn) continue; // Skip if there's no menu button
btn.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
await sleep();
btn.click();
await sleep();
const deleteButton = document.querySelector("body > bookmarks-app")
.shadowRoot.querySelector("bookmarks-command-manager")
.shadowRoot.querySelector("cr-action-menu > button:nth-child(5)");
if (deleteButton) {
deleteButton.click();
await sleep();
}
}
// Recursively call the function to check for remaining bookmarks
deleteAllBookmarks();
}
/**
* Utility function to introduce a delay in execution.
* @param {number} ms - Milliseconds to wait (default: 500ms).
* @returns {Promise<void>} A promise that resolves after the specified time.
*/
function sleep(ms = 500) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Start the deletion process
deleteAllBookmarks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment