forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarksMenuAndButtonShortcuts.uc.js
194 lines (192 loc) · 7.94 KB
/
bookmarksMenuAndButtonShortcuts.uc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// ==UserScript==
// @name Bookmarks Menu & Button Shortcuts
// @version 1.3.5
// @author aminomancer
// @homepageURL https://github.com/aminomancer/uc.css.js
// @description Adds some shortcuts for bookmarking pages. First, middle-clicking the bookmarks or library toolbar button will bookmark the current tab, or un-bookmark it if it's already bookmarked. Second, a menu item is added to the bookmarks toolbar button's popup, which bookmarks the current tab, or, if the page is already bookmarked, opens the bookmark editor popup. These are added primarily so that bookmarks can be added or removed with a single click, and can still be quickly added even if the bookmark page action is hidden for whatever reason. Third, another menu item is added to replicate the "Search bookmarks" button in the app menu's bookmarks panel. Clicking it will open the urlbar in bookmarks search mode.
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/bookmarksMenuAndButtonShortcuts.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/bookmarksMenuAndButtonShortcuts.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// ==/UserScript==
const ucBookmarksShortcuts = {
create(aDoc, tag, props, isHTML = false) {
let el = isHTML ? aDoc.createElement(tag) : aDoc.createXULElement(tag);
for (let prop in props) el.setAttribute(prop, props[prop]);
return el;
},
async bookmarkClick(e) {
if (e.button !== 1 || e.target.tagName !== "toolbarbutton") return;
let bm = await PlacesUtils.bookmarks.fetch({
url: new URL(BookmarkingUI._uri.spec),
});
bm ? PlacesTransactions.Remove(bm.guid).transact() : this.starCmd();
e.preventDefault();
e.stopPropagation();
},
async starCmd() {
if (!BookmarkingUI._pendingUpdate) {
if (!!BookmarkingUI.starAnimBox && !(BookmarkingUI._itemGuids.size > 0)) {
BrowserUIUtils.setToolbarButtonHeightProperty(BookmarkingUI.star);
document
.getElementById("star-button-animatable-box")
.addEventListener(
"animationend",
() => BookmarkingUI.star.removeAttribute("animate"),
{
once: true,
}
);
BookmarkingUI.star.setAttribute("animate", "true");
}
let browser = gBrowser.selectedBrowser;
let url = new URL(browser.currentURI.spec);
let parentGuid = await PlacesUIUtils.defaultParentGuid;
let info = { url, parentGuid };
let charset = null;
let isErrorPage = false;
if (browser.documentURI) {
isErrorPage = /^about:(neterror|certerror|blocked)/.test(
browser.documentURI.spec
);
}
try {
if (isErrorPage) {
let entry = await PlacesUtils.history.fetch(browser.currentURI);
if (entry) info.title = entry.title;
} else {
info.title = browser.contentTitle;
}
info.title = info.title || url.href;
charset = browser.characterSet;
} catch (e) {}
info.guid = await PlacesTransactions.NewBookmark(info).transact();
if (charset) PlacesUIUtils.setCharsetForPage(url, charset, window);
gURLBar.handleRevert();
StarUI.showConfirmation();
}
},
addMenuitems(popup) {
let doc = popup.ownerDocument;
this.bookmarkTab = doc.createXULElement("menuitem");
this.bookmarkTab = this.create(doc, "menuitem", {
id: "BMB_bookmarkThisPage",
label: "",
class: "menuitem-iconic subviewbutton",
onclick: "ucBookmarksShortcuts.updateMenuItem()",
oncommand: "BookmarkingUI.onStarCommand(event);",
key: "addBookmarkAsKb",
image: "chrome://browser/skin/bookmark-hollow.svg",
});
popup.insertBefore(this.bookmarkTab, popup.firstElementChild);
popup.addEventListener("popupshowing", this.updateMenuItem);
this.bookmarkTab.setAttribute(
"data-l10n-id",
"bookmarks-subview-bookmark-tab"
);
this.searchBookmarks = popup
.querySelector("#BMB_viewBookmarksSidebar")
.after(
this.create(doc, "menuitem", {
id: "BMB_searchBookmarks",
class: "menuitem-iconic subviewbutton",
"data-l10n-id": "bookmarks-search",
oncommand: "PlacesCommandHook.searchBookmarks();",
image: "chrome://global/skin/icons/search-glass.svg",
})
);
},
onLocationChange(browser, _prog, _req, location, _flags) {
if (browser !== gBrowser.selectedBrowser) return;
this.updateMenuItem(null, location);
},
async updateMenuItem(_e, location) {
let uri;
let menuitem = ucBookmarksShortcuts.bookmarkTab;
if (location) uri = new URL(location?.spec);
if (BookmarkingUI._uri) uri = new URL(BookmarkingUI._uri.spec);
if (!uri) return;
let isStarred = await PlacesUtils.bookmarks.fetch({ url: uri });
if ("l10n" in menuitem.ownerDocument && menuitem.ownerDocument.l10n) {
menuitem.ownerDocument.l10n.setAttributes(
menuitem,
isStarred
? "bookmarks-subview-edit-bookmark"
: "bookmarks-subview-bookmark-tab"
);
}
menuitem.setAttribute(
"image",
isStarred
? "chrome://browser/skin/bookmark.svg"
: "chrome://browser/skin/bookmark-hollow.svg"
);
},
init() {
let node = CustomizableUI.getWidget("bookmarks-menu-button")?.forWindow(
window
).node;
// delete these two lines if you don't want the confirmation hint to show
// when you bookmark a page.
Services.prefs.setIntPref(
"browser.bookmarks.editDialog.confirmationHintShowCount",
0
);
Services.prefs.lockPref(
"browser.bookmarks.editDialog.confirmationHintShowCount"
);
BookmarkingUI.button.setAttribute(
"onclick",
"ucBookmarksShortcuts.bookmarkClick(event)"
);
CustomizableUI.getWidget("library-button")
.forWindow(window)
.node?.setAttribute(
"onclick",
"ucBookmarksShortcuts.bookmarkClick(event)"
);
this.addMenuitems(node.querySelector("#BMB_bookmarksPopup"));
gBrowser.addTabsProgressListener(this);
PlacesUtils.observers.addListener(
["bookmark-added", "bookmark-removed", "bookmark-url-changed"],
events => {
for (let e of events) {
if (e.url && e.url == BookmarkingUI._uri?.spec) this.updateMenuItem();
}
}
);
// set the "positionend" attribute on the view bookmarks sidebar menuitem.
// this way we can swap between the left/right sidebar icons based on which
// side the sidebar is on, like the sidebar toolbar widget does.
let sidebarItem = node.querySelector("#BMB_viewBookmarksSidebar");
if (sidebarItem) {
sidebarItem.appendChild(
this.create(document, "observes", {
element: "sidebar-box",
attribute: "positionend",
})
);
}
// show the URL and keyword fields in the edit bookmark panel
eval(
`StarUI.showEditBookmarkPopup = async function ${StarUI.showEditBookmarkPopup
.toSource()
.replace(/^\(/, "")
.replace(/\)$/, "")
.replace(/async showEditBookmarkPopup/, "")
.replace(/async function\s*/, "")
.replace(/\[\"location\", \"keyword\"\]/, "[]")}`
);
},
QueryInterface: ChromeUtils.generateQI(["nsINavBookmarkObserver"]),
};
if (gBrowserInit.delayedStartupFinished) {
ucBookmarksShortcuts.init();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
ucBookmarksShortcuts.init();
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}