import { makeRemotelyCallable } from 'webextension-rpc'; // Format time as M:SS, or H:MM:SS, depending on which would be needed to express maxSeconds. // E.g. secondsToString(12) === '0:12', but secondsToString(12, 3600) === '0:00:12'. function secondsToString(seconds, maxSeconds) { let s = `${Math.round(seconds % 60)}`; let m = ''; let h = ''; const minutes = Math.floor(seconds / 60); m = `${minutes % 60}:`; if (!/^\d\d/.test(s)) s = '0' + s; if (seconds >= 60 * 60 || maxSeconds >= 60 * 60) { h = `${Math.floor(minutes / 60)}:`; if (m.length <= 2) m = '0' + m; } return `${h}${m}${s}`; } async function createBookmark({ url, start, end, trackDuration }) { // Create a (hopefully) meaningful title; e.g. 'filename.mp3 0:30–1:10' const filenameMatch = url.match(/.*\/([^\/#?]+)(?:\?.*)?(?:#.*)?/); const filename = filenameMatch ? filenameMatch[1] : 'Audio fragment'; const startString = secondsToString(start, trackDuration); const endString = (end !== undefined) ? '–' + secondsToString(end, trackDuration) : ''; const title = `${filename} ${startString}${endString}`; await browser.bookmarks.create({ url, title }); } makeRemotelyCallable({ createBookmark });