import { makeRemotelyCallable, remoteFunction } from 'webextension-rpc'; function createPreciseUrl(url, selector) { const properUrl = url.split('#')[0]; let fragmentIdentifier; if (selector.type === 'FragmentSelector') { fragmentIdentifier = selector.value; } else { throw new Error('Unsupported selector type'); } return properUrl + '#' + fragmentIdentifier; } async function createBookmark({ tab }) { const selector = await remoteFunction('describeSelection', { tabId: tab.id })(); const bookmarkUrl = createPreciseUrl(tab.url, selector); const filenameMatch = tab.url.match(/.*\/([^\/#?]+)(?:\?.*)?(?:#.*)?/); const filename = filenameMatch ? filenameMatch[1] : 'Audio fragment'; const bookmarkTitle = `${filename} ${start}–${end}`; await browser.bookmarks.create({ title: bookmarkTitle, url: bookmarkUrl, }); } makeRemotelyCallable({ createBookmark }, { insertExtraArg: true });