-
Notifications
You must be signed in to change notification settings - Fork 20
/
bookmarks-button.js
168 lines (149 loc) · 7.2 KB
/
bookmarks-button.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
//Bookmarks button before AddressBar
//Кнопка с выпадающими закладками перед строкой адреса
{
const position = { separate: "separate", addressfield: "addressfield" }
vivaldi.jdhooks.hookModuleExport("vivaldiSettings", "default", exports => {
let oldGetDefault = exports.getDefault
exports.getDefault = name => {
switch (name) {
case "BOOKMARK_BUTTON_POSITION": return position.separate
default: return oldGetDefault(name)
}
}
return exports
})
function bookmarksOnClick(event) {
const CommandManager = vivaldi.jdhooks.require("_CommandManager")
const ShowMenu = vivaldi.jdhooks.require("_ShowMenu")
const menu = CommandManager.getNamedMenu("vivaldi", true)
.concat(CommandManager.getNamedMenu("menubar", true))
const bookmarks = menu.find(i => i.commandName == "MENU_BOOKMARKS")
if (bookmarks) {
const rect = event.target.getBoundingClientRect()
const props = {
id: 0,
rect: {
x: parseInt(rect.left),
y: parseInt(rect.top),
width: parseInt(rect.width),
height: parseInt(rect.height)
},
menu: {
items: bookmarks.items,
expandId: "",
preferred: []
}
}
ShowMenu.show(props.id, [props], "bottom")
}
}
vivaldi.jdhooks.hookClass("createbookmark_createbookmark", origClass => {
const ReactDom = vivaldi.jdhooks.require("ReactDOM")
class newCreateBookmarkButton extends origClass {
constructor(...e) {
super(...e)
this.bookmarksButtonJs = {
pointerUp: (e => {
if (e.button == 2 && this.state.jdVivaldiSettings.BOOKMARK_BUTTON_POSITION == position.addressfield) {
bookmarksOnClick(e)
}
}).bind(this)
}
}
componentDidMount() {
if (super.componentDidMount) super.componentDidMount()
const button = ReactDom.findDOMNode(this)
if (button) button.addEventListener("pointerup", this.bookmarksButtonJs.pointerUp, true)
}
componentWillUnmount() {
const button = ReactDom.findDOMNode(this)
if (button) button.removeEventListener("pointerup", this.bookmarksButtonJs.pointerUp, true)
if (super.componentWillUnmount) super.componentWillUnmount()
}
}
return vivaldi.jdhooks.insertWatcher(newCreateBookmarkButton, { settings: ["BOOKMARK_BUTTON_POSITION"] })
})
vivaldi.jdhooks.hookClass("toolbars_Toolbar", origClass => {
const React = vivaldi.jdhooks.require("React")
const SettingsPaths = vivaldi.jdhooks.require("_PrefKeys")
const ToolbarButton = vivaldi.jdhooks.require("toolbars_ToolbarButton")
class newClass extends origClass {
render() {
let ret = super.render()
if (this.props.name == SettingsPaths.kToolbarsNavigation) {
ret.props.children.push(
this.state.jdVivaldiSettings.BOOKMARK_BUTTON_POSITION == position.separate &&
this.state.jdPrefs[SettingsPaths.kMenuDisplay] != "top"
? React.createElement(ToolbarButton, {
tooltip: "Bookmarks",
onClick: bookmarksOnClick,
image: vivaldi.jdhooks.require("_svg_bookmarks_large")
})
: null
)
}
return ret
}
}
return vivaldi.jdhooks.insertWatcher(newClass, {
settings: ["BOOKMARK_BUTTON_POSITION"],
prefs: [SettingsPaths.kMenuDisplay]
})
})
vivaldi.jdhooks.hookClass("settings_bookmarks_BookmarkSettings", origClass => {
const React = vivaldi.jdhooks.require("React")
const RadioGroup = vivaldi.jdhooks.require("common_RadioGroup")
const VivaldiSettings = vivaldi.jdhooks.require("vivaldiSettings")
const Settings_SettingsSearchCategoryChild = vivaldi.jdhooks.require("settings_SettingsSearchCategoryChild")
function toArray(i) {
if (Array.isArray(i)) return i;
return [i];
}
const Setting = vivaldi.jdhooks.insertWatcher(class extends React.PureComponent {
render() {
return React.createElement(Settings_SettingsSearchCategoryChild, { filter: this.props.filter },
React.createElement("div", { className: "setting-group" },
React.createElement("h3", {}, "Bookmark Button"),
React.createElement(RadioGroup,
{
name: "bookmark_button_position",
value: this.state.jdVivaldiSettings.BOOKMARK_BUTTON_POSITION,
onChange: (evt) => VivaldiSettings.set({ BOOKMARK_BUTTON_POSITION: evt.target.value })
},
React.createElement("div", { className: "setting-single" },
React.createElement("label", {},
React.createElement("input",
{
type: "radio",
value: position.separate,
}),
React.createElement("span", {}, "Separate button")
)
),
React.createElement("div", { className: "setting-single" },
React.createElement("label", {},
React.createElement("input",
{
type: "radio",
value: position.addressfield,
}),
React.createElement("span", {},
'Right click on "Add bookmark" button in the address field'
)
)
)
)
)
)
}
}, { settings: ["BOOKMARK_BUTTON_POSITION"] })
return class extends origClass {
render() {
let r = super.render()
r.props.children = toArray(r.props.children)
r.props.children.push(React.createElement(Setting, this.props))
return r
}
}
})
}