Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions extension/background.js

This file was deleted.

15 changes: 7 additions & 8 deletions extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,
"version": "4",
"name": "Bookmark All",
"author": "Andrew Sutherland",
Expand All @@ -12,17 +12,16 @@
"128": "star_icons_128.png",
"48": "star_icons_48.png"
},
"browser_action": {
"background": {
"service_worker": "service_worker.js"
},
"action": {
"default_icon": {
"19": "star_icons_19.png",
"38": "star_icons_38.png"
},
"default_popup": "popup.html",
"default_title": "Bookmark All"
},
"background": {
"scripts": [
"background.js"
]
}
}
"content_security_policy": {}
}
54 changes: 33 additions & 21 deletions extension/script.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
function toShortISODateString(d) {
function pad(n) {
return n<10 ? '0'+n : n
}
return d.getFullYear()+'-'
+ pad(d.getMonth()+1)+'-'
+ pad(d.getDate());
const pad = (n) => (n < 10 ? "0" + n : n);
return (
d.getFullYear() +
"-" +
pad(d.getMonth() + 1) +
"-" +
pad(d.getDate())
);
}
$(document).ready(function(){
var folderTitle = toShortISODateString(new Date());
// Set default value to current date
$('#folderName').val(folderTitle);
console.log("Set placeholder to " + folderTitle);
// Handle button click
$('form').submit(function(){
// This seems the stupidest way to get the value
var folderName = $('#folderName').val();
window.close();
// Create bookmarks from background page
// I couldn't get it to work from here
// and there seemed to be a race condition with window.close()
chrome.extension.getBackgroundPage().bookmarkAll(folderName);
return false;

$(function () {
const defaultTitle = toShortISODateString(new Date());
$("#folderName").val(defaultTitle);
console.log("Default folder name:", defaultTitle);

$("form").on("submit", function (e) {
e.preventDefault();

const folderTitle = $("#folderName").val().trim() || defaultTitle;

chrome.runtime.sendMessage(
{ type: "BOOKMARK_ALL", folderTitle },
(res) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else if (!res?.ok) {
console.error(res?.error || "Unknown error");
} else {
console.log(`Bookmarked ${res.count} tabs into folder ${folderTitle}`);
}
// Close AFTER we get a response to avoid the race
window.close();
}
);
});
});
50 changes: 50 additions & 0 deletions extension/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const p = {
bookmarksCreate: (data) =>
new Promise((resolve, reject) =>
chrome.bookmarks.create(data, (res) =>
chrome.runtime.lastError ? reject(chrome.runtime.lastError) : resolve(res)
)
),
windowsGetAll: (opts) =>
new Promise((resolve, reject) =>
chrome.windows.getAll(opts, (res) =>
chrome.runtime.lastError ? reject(chrome.runtime.lastError) : resolve(res)
)
)
};

async function bookmarkAll(folderTitle) {
console.log("Creating new bookmarks in", folderTitle);

// Create the folder
const folder = await p.bookmarksCreate({ title: folderTitle });

// Get all windows & tabs
const windows = await p.windowsGetAll({ populate: true });

let count = 0;
for (const win of windows) {
for (const tab of win.tabs) {
// Skip chrome://, edge://, etc.
if (!tab.url || !/^https?:/i.test(tab.url)) continue;

await p.bookmarksCreate({
parentId: folder.id,
title: tab.title || tab.url,
url: tab.url
});
count++;
}
}
return { folderId: folder.id, count };
}

// Message router
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg?.type === "BOOKMARK_ALL") {
bookmarkAll(msg.folderTitle)
.then((result) => sendResponse({ ok: true, ...result }))
.catch((err) => sendResponse({ ok: false, error: String(err) }));
return true; // keep channel open for async
}
});