bookmark.js (1653B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 // 'arguments' is defined by the marionette harness. 5 /* global arguments */ 6 7 const { PlacesUtils } = ChromeUtils.importESModule( 8 "resource://gre/modules/PlacesUtils.sys.mjs" 9 ); 10 11 let resolve = arguments[3]; 12 13 try { 14 // Get the number of current bookmarks 15 PlacesUtils.promiseBookmarksTree(PlacesUtils.bookmarks.unfiledGuid, { 16 includeItemIds: true, 17 }).then(root => { 18 let count = root.itemsCount; 19 let maxBookmarks = arguments[2]; 20 // making sure we don't exceed the maximum number of bookmarks 21 if (count >= maxBookmarks) { 22 let toRemove = count - maxBookmarks + 1; 23 console.log("We've reached the maximum number of bookmarks"); 24 console.log("Removing " + toRemove); 25 let children = root.children; 26 for (let i = 0, p = Promise.resolve(); i < toRemove; i++) { 27 p = p.then( 28 _ => 29 new Promise(resolve => 30 PlacesUtils.bookmarks.remove(children[i].guid).then(res => { 31 console.log("removed one bookmark"); 32 resolve(res); 33 }) 34 ) 35 ); 36 } 37 } 38 // now adding the bookmark 39 PlacesUtils.bookmarks 40 .insert({ 41 parentGuid: PlacesUtils.bookmarks.unfiledGuid, 42 url: arguments[0], 43 title: arguments[1], 44 }) 45 .then(res => { 46 resolve(res); 47 }); 48 }); 49 } catch (error) { 50 let res = { logs: {}, result: 1, result_message: error.toString() }; 51 resolve(res); 52 }