added bookmark functions
This commit is contained in:
		
							parent
							
								
									0e5a311028
								
							
						
					
					
						commit
						ed98f821bb
					
				
							
								
								
									
										43
									
								
								src/functions/getBookmarks.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/functions/getBookmarks.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
import { Bookmarks, Bookmark } from "../types/Bookmarks";
 | 
			
		||||
 | 
			
		||||
function createBookmark(from: browser.bookmarks.BookmarkTreeNode): Bookmark {
 | 
			
		||||
	return {
 | 
			
		||||
		name: from.title,
 | 
			
		||||
		url: from.url
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default async (): Promise<Bookmarks> => {
 | 
			
		||||
	const treeNode = await browser.bookmarks.getTree();
 | 
			
		||||
 | 
			
		||||
	const root: Bookmarks = {
 | 
			
		||||
		folder: [],
 | 
			
		||||
		orphans: []
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const rootNode = treeNode[0].children.find((e)=>e.id === "menu________");
 | 
			
		||||
 | 
			
		||||
	// Set all top level bookmarks (without folder)
 | 
			
		||||
	root.orphans = rootNode.children
 | 
			
		||||
		.filter((e)=> e.type === "bookmark" )
 | 
			
		||||
		.sort((a,b)=> a.index - b.index )
 | 
			
		||||
		.map(createBookmark);
 | 
			
		||||
 | 
			
		||||
	// Get all top level folders 
 | 
			
		||||
	root.folder = rootNode.children
 | 
			
		||||
		.filter((e)=> e.type === "folder")
 | 
			
		||||
		.filter((e)=> e.children.length > 0)
 | 
			
		||||
		.sort((a,b)=> a.index - b.index )
 | 
			
		||||
		.map((e)=>{
 | 
			
		||||
			const children: Bookmark[] = e.children
 | 
			
		||||
				.filter((e)=> e.type === "bookmark")
 | 
			
		||||
				.sort((a,b)=> a.index - b.index )
 | 
			
		||||
				.map(createBookmark);
 | 
			
		||||
			return {
 | 
			
		||||
				name: e.title,
 | 
			
		||||
				bookmarks: children
 | 
			
		||||
			};
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
	return root;
 | 
			
		||||
};
 | 
			
		||||
@ -13,7 +13,8 @@
 | 
			
		||||
		"homepage": "startpage/index.html"
 | 
			
		||||
	},
 | 
			
		||||
	"permissions": [
 | 
			
		||||
		"<all_urls>"
 | 
			
		||||
		"<all_urls>",
 | 
			
		||||
		"bookmarks"
 | 
			
		||||
	],
 | 
			
		||||
	"applications": {
 | 
			
		||||
		"gecko": {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								src/types/Bookmarks.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/types/Bookmarks.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
interface Bookmarks {
 | 
			
		||||
	folder: BookmarkFolder[];
 | 
			
		||||
	orphans: Bookmark[];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface BookmarkFolder {
 | 
			
		||||
	name: string;
 | 
			
		||||
	bookmarks: Bookmark[];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface Bookmark {
 | 
			
		||||
	name: string;
 | 
			
		||||
	url: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export { Bookmarks, Bookmark, BookmarkFolder };
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user