Last active
June 21, 2022 04:39
-
-
Save niobium93/232a14c9e655332a750bef2fbde3a1f4 to your computer and use it in GitHub Desktop.
Firefox Bookmarks API issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Bookmark Test", | |
"description": "Bookmark Test", | |
"version": "0.1", | |
"manifest_version": 2, | |
"permissions": ["bookmarks"], | |
"browser_action": { | |
"default_title": "Bookmark Test", | |
"default_popup": "popup.html" | |
}, | |
"background": { | |
"scripts": ["background.js"], | |
"persistent": false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<button id="bookmarkButton">Add bookmark</button> | |
<script type="module" src="popup.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getBookmarkMenuNode() { | |
const bookmarkTree = await browser.bookmarks.getTree() | |
for (const rootNode of bookmarkTree) { | |
if (rootNode.id.includes('root')) { | |
for (const menuNode of rootNode.children) { | |
if (menuNode.id.includes('menu')) { | |
return menuNode | |
} | |
} | |
} | |
} | |
} | |
async function getBookmarksFolderInNode(folderName, node) { | |
let folderNode = null | |
if (node.children != undefined) { | |
for (const subNode of node.children) { | |
if (subNode.title == folderName) folderNode = subNode | |
} | |
} | |
if (folderNode == null) { | |
folderNode = await browser.bookmarks.create({ | |
parentId: node.id, | |
title: folderName, | |
type: 'folder' | |
}) | |
} | |
console.log('Folder id: ' + folderNode.id) | |
return folderNode | |
} | |
async function onBookmark(event) { | |
if (extensionFolder.children != undefined) { | |
for (const bookmark of extensionFolder.children) { | |
if (bookmark.url == 'https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/bookmarks/create') return | |
} | |
} | |
const bookmark = await browser.bookmarks.create({ | |
title: 'bookmarks.create() - Mozilla | MDN', | |
url: 'https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/bookmarks/create', | |
parentId: extensionFolder.id, | |
type: 'bookmark' | |
}) | |
console.log('Bookmark parent id: ' + bookmark.parentId) | |
} | |
const menuNode = await getBookmarkMenuNode() | |
let extensionFolder = await getBookmarksFolderInNode('Extension Folder', menuNode) | |
document.getElementById("bookmarkButton").addEventListener("click", onBookmark) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment