added bookmarks as optional_permission and added checkbox in preferences menu.

This commit is contained in:
Kendall Werts 2019-10-24 14:14:30 -05:00
parent 7e4950b184
commit e7824f367b
6 changed files with 102 additions and 14 deletions

View file

@ -5,7 +5,6 @@ const assignManager = {
MENU_HIDE_ID: "hide-container",
MENU_MOVE_ID: "move-to-new-window-container",
OPEN_IN_CONTAINER: "open-bookmark-in-container-tab",
storageArea: {
area: browser.storage.local,
exemptedTabs: {},
@ -222,11 +221,7 @@ const assignManager = {
init() {
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.bookmarkId) {
this._onClickedBookmark(info);
} else {
this._onClickedHandler(info, tab);
}
info.bookmarkId ? this._onClickedBookmark(info) : this._onClickedHandler(info, tab);
});
// Before a request is handled by the browser we decide if we should route through a different container
@ -246,6 +241,24 @@ const assignManager = {
delete this.canceledRequests[options.tabId];
}
},{urls: ["<all_urls>"], types: ["main_frame"]});
this.getPermissions();
},
getPermissions() {
browser.permissions.getAll().
then((permissions) => {
if (permissions.permissions.includes("bookmarks")) {
this.makeBookmarksMenu();
} else {
browser.contextMenus.remove(this.OPEN_IN_CONTAINER);
}
}).
catch((err) => {
return err.message;
});
},
makeBookmarksMenu() {
this.initBookmarksMenu();
browser.contextualIdentities.onCreated.addListener(this.contextualIdentCreated);
browser.contextualIdentities.onUpdated.addListener(this.contextualIdentUpdated);
@ -304,11 +317,19 @@ const assignManager = {
},
async _onClickedBookmark(info) {
let bookmarks = await browser.bookmarks.get(info.bookmarkId);
if (bookmarks[0].type === "folder") {
bookmarks = await browser.bookmarks.getChildren(bookmarks[0].id);
async function _getBookmarksFromInfo(info) {
const bookmarkTreeNode = await browser.bookmarks.get(info.bookmarkId);
const firstBookmark = bookmarkTreeNode[0];
if (firstBookmark.type === "folder") {
return await browser.bookmarks.getChildren(firstBookmark.id);
} else {
return bookmarkTreeNode;
}
}
for (let bookmark of bookmarks) { // eslint-disable-line prefer-const
const bookmarks = await _getBookmarksFromInfo(info);
for (const bookmark of bookmarks) {
browser.tabs.create({
cookieStoreId: info.menuItemId,
url: bookmark.url
@ -493,7 +514,7 @@ const assignManager = {
});
const identities = await browser.contextualIdentities.query({});
for (let identity of identities) { // eslint-disable-line prefer-const
for (const identity of identities) {
browser.contextMenus.create({
parentId: this.OPEN_IN_CONTAINER,
id: identity.cookieStoreId,

View file

@ -10,6 +10,9 @@ const messageHandler = {
let response;
switch (m.method) {
case "resetBookmarksContext":
response = assignManager.getPermissions();
break;
case "deleteContainer":
response = backgroundLogic.deleteContainer(m.message.userContextId);
break;

41
src/js/options.js Normal file
View file

@ -0,0 +1,41 @@
function requestPermissions() {
const checkbox = document.querySelector("#bookmarksPermissions");
if (checkbox.checked) {
browser.permissions.request({permissions: ["bookmarks"]}).
then((response) => {
if (response) {
browser.runtime.sendMessage({ method: "resetBookmarksContext" });
} else {
checkbox.checked = false;
}
}).
catch((err) => {
return err.message;
});
} else {
browser.permissions.remove({permissions: ["bookmarks"]}).
then(() => {
browser.runtime.sendMessage({ method: "resetBookmarksContext" });
}).
catch((err) => {
return err.message;
});
}
}
function restoreOptions() {
browser.permissions.getAll()
.then((permissions) => {
if (permissions.permissions.includes("bookmarks")) {
document.querySelector("#bookmarksPermissions").checked = true;
}
}).
catch((err) => {
return err.message;
});
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("#bookmarksPermissions").addEventListener( "change", requestPermissions);

View file

@ -30,7 +30,10 @@
"storage",
"tabs",
"webRequestBlocking",
"webRequest",
"webRequest"
],
"optional_permissions": [
"bookmarks"
],
@ -71,5 +74,9 @@
"web_accessible_resources": [
"/img/container-site-d-24.png"
]
],
"options_ui": {
"page": "options.html"
}
}

16
src/options.html Normal file
View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<form>
<input type="checkbox" id="bookmarksPermissions"/>
<label>Enable Bookmark Permissions</label>
<p>This setting allows you to open a bookmark or folder of bookmarks in a container.</p>
</form>
<script src="js/options.js"></script>
</body>
</html>