forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmarksMenuAndButtonShortcuts.uc.js
147 lines (145 loc) · 7.21 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
// ==UserScript==
// @name Bookmarks Menu & Button Shortcuts
// @version 1.2.2
// @author aminomancer
// @homepage 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.
// ==/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, false);
this.bookmarkTab.ownerDocument.l10n.setAttributes(
this.bookmarkTab,
"bookmarks-current-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);
},
handlePlacesEvents(events) {
for (let e of events) if (e.url && e.url == BookmarkingUI._uri?.spec) this.updateMenuItem();
},
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 });
menuitem.ownerDocument.l10n.setAttributes(
menuitem,
isStarred ? "bookmarks-bookmark-edit-panel" : "bookmarks-current-tab"
);
menuitem.setAttribute(
"image",
isStarred
? "chrome://browser/skin/bookmark.svg"
: "chrome://browser/skin/bookmark-hollow.svg"
);
},
init() {
// 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(document.getElementById("BMB_bookmarksPopup"));
gBrowser.addTabsProgressListener(this);
PlacesUtils.bookmarks.addObserver(this);
PlacesUtils.observers.addListener(
["bookmark-added", "bookmark-removed"],
this.handlePlacesEvents.bind(this)
);
// 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.
document.getElementById("BMB_viewBookmarksSidebar").appendChild(
this.create(document, "observes", {
"element": "sidebar-box",
"attribute": "positionend",
})
);
},
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");
}