From 8c256241cbe63b5f622fc7bec94991cebb9b8dde Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 3 Dec 2020 12:19:58 -0500 Subject: [PATCH] only create bookmarks on the initial run, provide option to re-create them in preferences menu --- bookmarks.js | 416 +++++++++++++++++++++++-------------------- options/options.html | 11 +- options/options.js | 55 ++++-- 3 files changed, 275 insertions(+), 207 deletions(-) diff --git a/bookmarks.js b/bookmarks.js index 7c6efe4..deda6cb 100644 --- a/bookmarks.js +++ b/bookmarks.js @@ -1,214 +1,242 @@ -var gettingInfo = browser.runtime.getPlatformInfo(); -gettingInfo.then((got) => { - if (got.os != "android") { - function bookmarks(bookmarkToolbar) { - console.log("(bookmarks)", bookmarkToolbar); - console.log("Setting up bookmark toolbar", bookmarkToolbar); - function bookHome(bookmarkItems) { - if (!bookmarkItems.length) { - function gotProxyInfo(info) { - let port = info.value.http.split(":")[1]; - if (port == "7644") { - let createRhizomeBookmark = browser.bookmarks.create({ - url: "about:I2p", - title: "I2P Extension Home Page", - parentId: bookmarkToolbar[0].id, - }); - createRhizomeBookmark.then(onCreated); - } else { - let createBookmark = browser.bookmarks.create({ - url: browser.runtime.getURL("home.html"), - title: "I2P Extension Home Page", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onCreated); + +function bookmarksSetup() { + var gettingInfo = browser.runtime.getPlatformInfo(); + gettingInfo.then((got) => { + if (got.os != "android") { + function bookmarks(bookmarkToolbar) { + console.log("(bookmarks)", bookmarkToolbar); + console.log("Setting up bookmark toolbar", bookmarkToolbar); + function bookHome(bookmarkItems) { + if (!bookmarkItems.length) { + function gotProxyInfo(info) { + let port = info.value.http.split(":")[1]; + if (port == "7644") { + let createRhizomeBookmark = browser.bookmarks.create({ + url: "about:I2p", + title: "I2P Extension Home Page", + parentId: bookmarkToolbar[0].id, + }); + createRhizomeBookmark.then(onCreated); + } else { + let createBookmark = browser.bookmarks.create({ + url: browser.runtime.getURL("home.html"), + title: "I2P Extension Home Page", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onCreated); + } + console.log("(bookmarks) adding home page bookmark"); } - console.log("(bookmarks) adding home page bookmark"); + console.log( + "(bookmarks) checking if we're running in an I2P Browser" + ); + let gettingProxyInfo = browser.proxy.settings.get({}); + gettingProxyInfo.then(gotProxyInfo); } - console.log( - "(bookmarks) checking if we're running in an I2P Browser" - ); - let gettingProxyInfo = browser.proxy.settings.get({}); - gettingProxyInfo.then(gotProxyInfo); } - } - function bookTorrent(bookmarkItems) { - if (!bookmarkItems.length) { - function gotProxyInfo(info) { - let port = info.value.http.split(":")[1]; - if (port == "7644") { - let createBookmark = browser.bookmarks.create({ - url: "http://localhost:7657/i2psnark", - title: "Bittorrent", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onCreated); - } else { - let createRhizomeBookmark = browser.bookmarks.create({ - url: - "http://" + control_host + ":" + control_port + "/i2psnark", - title: "Bittorrent", - parentId: bookmarkToolbar[0].id, - }); - createRhizomeBookmark.then(onCreated); + function bookTorrent(bookmarkItems) { + if (!bookmarkItems.length) { + function gotProxyInfo(info) { + let port = info.value.http.split(":")[1]; + if (port == "7644") { + let createBookmark = browser.bookmarks.create({ + url: "http://localhost:7657/i2psnark", + title: "Bittorrent", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onCreated); + } else { + let createRhizomeBookmark = browser.bookmarks.create({ + url: + "http://" + control_host + ":" + control_port + "/i2psnark", + title: "Bittorrent", + parentId: bookmarkToolbar[0].id, + }); + createRhizomeBookmark.then(onCreated); + } } + console.log( + "(bookmarks) checking if we're running in an I2P Browser" + ); + let gettingProxyInfo = browser.proxy.settings.get({}); + gettingProxyInfo.then(gotProxyInfo); } - console.log( - "(bookmarks) checking if we're running in an I2P Browser" - ); - let gettingProxyInfo = browser.proxy.settings.get({}); - gettingProxyInfo.then(gotProxyInfo); } - } - function bookConsole(bookmarkItems) { - if (!bookmarkItems.length) { - function gotProxyInfo(info) { - let port = info.value.http.split(":")[1]; - if (port == "7644") { - let createBookmark = browser.bookmarks.create({ - url: "http://localhost:7657/home", - title: "I2P Console", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onCreated); - } else { - let createRhizomeBookmark = browser.bookmarks.create({ - url: "http://" + control_host + ":" + control_port + "/home", - title: "I2P Console", - parentId: bookmarkToolbar[0].id, - }); - createRhizomeBookmark.then(onCreated); + function bookConsole(bookmarkItems) { + if (!bookmarkItems.length) { + function gotProxyInfo(info) { + let port = info.value.http.split(":")[1]; + if (port == "7644") { + let createBookmark = browser.bookmarks.create({ + url: "http://localhost:7657/home", + title: "I2P Console", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onCreated); + } else { + let createRhizomeBookmark = browser.bookmarks.create({ + url: "http://" + control_host + ":" + control_port + "/home", + title: "I2P Console", + parentId: bookmarkToolbar[0].id, + }); + createRhizomeBookmark.then(onCreated); + } } + console.log( + "(bookmarks) checking if we're running in an I2P Browser" + ); + let gettingProxyInfo = browser.proxy.settings.get({}); + gettingProxyInfo.then(gotProxyInfo); } - console.log( - "(bookmarks) checking if we're running in an I2P Browser" - ); - let gettingProxyInfo = browser.proxy.settings.get({}); - gettingProxyInfo.then(gotProxyInfo); } - } - function bookMail(bookmarkItems) { - if (!bookmarkItems.length) { - function gotProxyInfo(info) { - let port = info.value.http.split(":")[1]; - if (port == "7644") { - let createBookmark = browser.bookmarks.create({ - url: "http://localhost:7657/webmail", - title: "Web Mail", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onCreated); - } else { - let createRhizomeBookmark = browser.bookmarks.create({ - url: "http://" + control_host + ":" + control_port + "/webmail", - title: "Web Mail", - parentId: bookmarkToolbar[0].id, - }); - createRhizomeBookmark.then(onCreated); + function bookMail(bookmarkItems) { + if (!bookmarkItems.length) { + function gotProxyInfo(info) { + let port = info.value.http.split(":")[1]; + if (port == "7644") { + let createBookmark = browser.bookmarks.create({ + url: "http://localhost:7657/webmail", + title: "Web Mail", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onCreated); + } else { + let createRhizomeBookmark = browser.bookmarks.create({ + url: "http://" + control_host + ":" + control_port + "/webmail", + title: "Web Mail", + parentId: bookmarkToolbar[0].id, + }); + createRhizomeBookmark.then(onCreated); + } + console.log("(bookmarks) adding webmail bookmark"); } - console.log("(bookmarks) adding webmail bookmark"); + console.log( + "(bookmarks) checking if we're running in an I2P Browser" + ); + let gettingProxyInfo = browser.proxy.settings.get({}); + gettingProxyInfo.then(gotProxyInfo); } - console.log( - "(bookmarks) checking if we're running in an I2P Browser" - ); - let gettingProxyInfo = browser.proxy.settings.get({}); - gettingProxyInfo.then(gotProxyInfo); } - } - function bookI2PTunnel(bookmarkItems) { - if (!bookmarkItems.length) { - function gotProxyInfo(info) { - let port = info.value.http.split(":")[1]; - if (port == "7644") { - var createBookmark = browser.bookmarks.create({ - url: "http://localhost:7657/i2ptunnel", - title: "Hidden Services Manager", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onCreated); - } else { - var createRhizomeBookmark = browser.bookmarks.create({ - url: - "http://" + control_host + ":" + control_port + "/i2ptunnel", - title: "Hidden Services Manager", - parentId: bookmarkToolbar[0].id, - }); - createRhizomeBookmark.then(onCreated); + function bookI2PTunnel(bookmarkItems) { + if (!bookmarkItems.length) { + function gotProxyInfo(info) { + let port = info.value.http.split(":")[1]; + if (port == "7644") { + var createBookmark = browser.bookmarks.create({ + url: "http://localhost:7657/i2ptunnel", + title: "Hidden Services Manager", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onCreated); + } else { + var createRhizomeBookmark = browser.bookmarks.create({ + url: + "http://" + control_host + ":" + control_port + "/i2ptunnel", + title: "Hidden Services Manager", + parentId: bookmarkToolbar[0].id, + }); + createRhizomeBookmark.then(onCreated); + } + console.log("(bookmarks) adding i2ptunnel bookmark"); } - console.log("(bookmarks) adding i2ptunnel bookmark"); + console.log( + "(bookmarks) checking if we're running in an I2P Browser" + ); + var gettingProxyInfo = browser.proxy.settings.get({}); + gettingProxyInfo.then(gotProxyInfo); } - console.log( - "(bookmarks) checking if we're running in an I2P Browser" - ); - var gettingProxyInfo = browser.proxy.settings.get({}); - gettingProxyInfo.then(gotProxyInfo); + } + + function onRejected(error) { + console.log(`An error : ${error}`); + } + function onCreated(node) { + console.log("Bookmarked", node); + } + + var b0 = browser.bookmarks.search({ + title: "I2P Extension Home Page", + }); + b0.then(bookHome, onRejected); + + var b1 = browser.bookmarks.search({ + title: "Bittorrent", + }); + b1.then(bookTorrent, onRejected); + + var b2 = browser.bookmarks.search({ + title: "Hidden Services Manager", + }); + b2.then(bookI2PTunnel, onRejected); + + var b3 = browser.bookmarks.search({ + title: "Web Mail", + }); + b3.then(bookMail, onRejected); + + var b4 = browser.bookmarks.search({ + title: "I2P Console", + }); + b4.then(bookConsole, onRejected); + } + + var bt = browser.bookmarks.search({ + query: "Toolbar", + }); + + function toolDir(bookmarkToolbar) { + let defaultSettings = {}; + defaultSettings["bookmarks_state"] = true + chrome.storage.local.set(defaultSettings) + console.log("(bookmarks) created") + var ibbt = browser.bookmarks.search("I2P Toolbar"); + function setupDir(ibbt) { + function onToolbarCreated(node) { + var ibt = browser.bookmarks.search("I2P Toolbar"); + ibt.then(bookmarks); + } + if (ibbt[0] == null) { + let createBookmark = browser.bookmarks.create({ + title: "I2P Toolbar", + parentId: bookmarkToolbar[0].id, + }); + createBookmark.then(onToolbarCreated); + } + } + ibbt.then(setupDir); + } + bt.then(toolDir); + + function handleCreated(id, bookmarkInfo) { + //var propValue; + for (var propName in bookmarkInfo) { + let propValue = bookmarkInfo[propName]; + console.log(propName, propValue); } } - function onRejected(error) { - console.log(`An error : ${error}`); - } - function onCreated(node) { - console.log("Bookmarked", node); - } - - var b0 = browser.bookmarks.search({ - title: "I2P Extension Home Page", - }); - b0.then(bookHome, onRejected); - - var b1 = browser.bookmarks.search({ - title: "Bittorrent", - }); - b1.then(bookTorrent, onRejected); - - var b2 = browser.bookmarks.search({ - title: "Hidden Services Manager", - }); - b2.then(bookI2PTunnel, onRejected); - - var b3 = browser.bookmarks.search({ - title: "Web Mail", - }); - b3.then(bookMail, onRejected); - - var b4 = browser.bookmarks.search({ - title: "I2P Console", - }); - b4.then(bookConsole, onRejected); + browser.bookmarks.onCreated.addListener(handleCreated); } + }); +} - var bt = browser.bookmarks.search({ - query: "Toolbar", - }); - - function toolDir(bookmarkToolbar) { - var ibbt = browser.bookmarks.search("I2P Toolbar"); - function setupDir(ibbt) { - function onToolbarCreated(node) { - var ibt = browser.bookmarks.search("I2P Toolbar"); - ibt.then(bookmarks); - } - if (ibbt[0] == null) { - let createBookmark = browser.bookmarks.create({ - title: "I2P Toolbar", - parentId: bookmarkToolbar[0].id, - }); - createBookmark.then(onToolbarCreated); - } - } - ibbt.then(setupDir); - } - bt.then(toolDir); - - function handleCreated(id, bookmarkInfo) { - //var propValue; - for (var propName in bookmarkInfo) { - let propValue = bookmarkInfo[propName]; - console.log(propName, propValue); - } - } - - browser.bookmarks.onCreated.addListener(handleCreated); +function conditionalBookmarksSetup(obj){ + console.log("(bookmarks) state", obj.bookmarks_state) + if (obj.bookmarks_state == false) { + bookmarksSetup() } -}); + if (obj.bookmarks_state == undefined) { + bookmarksSetup() + } +} + + +if (browser != null){ + let gettingStorage = browser.storage.local.get("bookmarks_state"); + gettingStorage.then(conditionalBookmarksSetup, bookmarksSetup); +} + +const bookmarksButton = document.getElementById("bookmarksButton"); +if (bookmarksButton != null){ + bookmarksButton.addEventListener("click", bookmarksSetup); +} diff --git a/options/options.html b/options/options.html index a3f6fa5..fc028c0 100644 --- a/options/options.html +++ b/options/options.html @@ -19,11 +19,17 @@ +
+ Bookmarks Status: + +
+
Proxy Options
-

@@ -83,7 +89,8 @@
- diff --git a/options/options.js b/options/options.js index 5bd07cd..98ff3e7 100644 --- a/options/options.js +++ b/options/options.js @@ -1,3 +1,8 @@ +function SetBookButton() { +// var bmid = document.getElementById("bookmark"); + //bmid.textContent = chrome.i18n.getMessage("bookmarkButton"); +} + function SetHostText() { var hostid = document.getElementById("hostText"); hostid.textContent = chrome.i18n.getMessage("hostText"); @@ -28,6 +33,21 @@ function SetControlHelpText() { portid.textContent = chrome.i18n.getMessage("controlHelpText"); } +function getBookmarksCreated() { + bookmarks_state = document.getElementById("bookmarks").value; + console.log("(options)Got i2p bookmarks state:", bookmarks_state); + if (bookmarks_state == undefined) { + return false; + } + if (bookmarks_state == "false") { + return false + } + if (bookmarks_state == "true") { + return true + } + return false; +} + function getScheme() { const proxy_scheme = document.querySelector("#proxy_scheme"); console.log("(options)Got i2p proxy scheme:", proxy_scheme.value); @@ -159,6 +179,9 @@ function checkStoredSettings(storedSettings) { port = undefined; } console.log("(options)proxy", "'" + host + "'", ":", port); + if (!storedSettings["bookmarks_state"]) + defaultSettings["bookmarks_state"] = false; + else defaultSettings["bookmarks_state"] = storedSettings["bookmarks_state"]; if (!storedSettings["proxy_scheme"]) defaultSettings["proxy_scheme"] = "http"; else defaultSettings["proxy_scheme"] = storedSettings["proxy_scheme"]; @@ -245,6 +268,9 @@ function checkAndroidStoredSettings(storedSettings) { let defaultSettings = {}; let host = ""; let port = ""; + if (!storedSettings["bookmarks_state"]) + defaultSettings["bookmarks_state"] = false; + else defaultSettings["bookmarks_state"] = storedSettings["bookmarks_state"]; if (!storedSettings["proxy_scheme"]) defaultSettings["proxy_scheme"] = "http"; else defaultSettings["proxy_scheme"] = storedSettings["proxy_scheme"]; if (!storedSettings["proxy_host"]) { @@ -320,6 +346,7 @@ function onError(e) { function storeSettings() { let storableSettings = {}; + storableSettings["bookmarks_state"] = getBookmarksCreated() storableSettings["proxy_scheme"] = getScheme(); storableSettings["proxy_host"] = getHost(); storableSettings["proxy_port"] = getPort(); @@ -341,15 +368,19 @@ function storeSettings() { function updateUI(restoredSettings) { const selectList = document.querySelector("#proxy_scheme"); - if (selectList == undefined) selectList.value = restoredSettings.proxy_scheme; + if (selectList != undefined) selectList.value = restoredSettings.proxy_scheme; //console.log("(options)showing proxy scheme:", selectList.value); + console.log(restoredSettings) + const bms = document.getElementById("bookmarksState"); + if (bms != undefined) bms.checked = restoredSettings.bookmarks_state; + const hostitem = document.getElementById("host"); - if (hostitem == undefined) hostitem.value = restoredSettings.proxy_host; + if (hostitem != undefined) hostitem.value = restoredSettings.proxy_host; //console.log("(options)showing proxy host:", hostitem.value); const portitem = document.getElementById("port"); - if (portitem == undefined) portitem.value = restoredSettings.proxy_port; + if (portitem != undefined) portitem.value = restoredSettings.proxy_port; //console.log("(options)showing proxy port:", portitem.value); const controlhostitem = document.getElementById("controlhost"); @@ -363,41 +394,42 @@ function updateUI(restoredSettings) { //console.log("(options)showing control port:", controlportitem.value); const rpchostitem = document.getElementById("rpchost"); - if (rpchostitem == undefined) rpchostitem.value = restoredSettings.rpc_host; + if (rpchostitem != undefined) rpchostitem.value = restoredSettings.rpc_host; //console.log("(options)showing rpc host:", rpchostitem.value); const rpcportitem = document.getElementById("rpcport"); - if (rpcportitem == undefined) rpcportitem.value = restoredSettings.rpc_port; + if (rpcportitem != undefined) rpcportitem.value = restoredSettings.rpc_port; //console.log("(options)showing rpc port:", rpcportitem.value); const rpcpathitem = document.getElementById("rpcpath"); - if (rpcpathitem == undefined) rpcpathitem.value = restoredSettings.rpc_path; + if (rpcpathitem != undefined) rpcpathitem.value = restoredSettings.rpc_path; //console.log("(options)showing rpc path:", rpcpathitem.value); const rpcpassitem = document.getElementById("rpcpass"); - if (rpcpassitem == undefined) rpcpassitem.value = restoredSettings.rpc_pass; + if (rpcpassitem != undefined) rpcpassitem.value = restoredSettings.rpc_pass; //console.log("(options)showing rpc pass:"); const btrpchostitem = document.getElementById("btrpchost"); - if (btrpchostitem == undefined) + if (btrpchostitem != undefined) btrpchostitem.value = restoredSettings.rpc_host; //console.log("(options)showing bt rpc host:", btrpchostitem.value); const btrpcportitem = document.getElementById("btrpcport"); - if (btrpcportitem == undefined) + if (btrpcportitem != undefined) btrpcportitem.value = restoredSettings.rpc_port; //console.log("(options)showing rbt pc port:", rpcportitem.value); const btrpcpathitem = document.getElementById("btrpcpath"); - if (btrpcpathitem == undefined) + if (btrpcpathitem != undefined) btrpcpathitem.value = restoredSettings.rpc_path; //console.log("(options)showing bt rpc path:", btrpcpathitem.value); const btrpcpassitem = document.getElementById("btrpcpass"); - if (btrpcpassitem == undefined) + if (btrpcpassitem != undefined) btrpcpassitem.value = restoredSettings.rpc_pass; //console.log("(options)showing bt rpc pass:"); + SetBookButton() SetHostText(); SetPortText(); SetPortHelpText(); @@ -427,3 +459,4 @@ gettingInfo.then((got) => { const saveButton = document.querySelector("#save-button"); saveButton.addEventListener("click", storeSettings); +