-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtab_btn_menu.uc.js
161 lines (115 loc) · 5.49 KB
/
newtab_btn_menu.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
/* Firefox userChrome script
* Open URL in clipboard by right-clicking new-tab-button then use context menu
* Tested on Firefox 115
* Author: garywill (https://garywill.github.io)
*/
// ==UserScript==
// @include main
// ==/UserScript==
console.log("newtab_btn_menu.js");
const new_tab_url_label = 'New tab open: ';
var btn_newtab_w_url_clipboard_str = "";
// https://searchfox.org/mozilla-central/source/browser/base/content/browser.js#3076
// https://udn.realityripple.com/docs/Mozilla/Tech/XPCOM/Using_the_clipboard
// https://udn.realityripple.com/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsITransferable
function _readFromClipboard() {
var url = "";
try {
var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
Ci.nsITransferable
);
trans.init(getLoadContext());
trans.addDataFlavor("text/plain");
// If available, use selection clipboard, otherwise global one
// if (Services.clipboard.supportsSelectionClipboard()) {
if (false) {
Services.clipboard.getData(trans, Services.clipboard.kSelectionClipboard);
} else {
Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
}
var data = {};
trans.getTransferData("text/plain", data);
if (data) {
data = data.value.QueryInterface(Ci.nsISupportsString);
url = data.data;
}
}catch(err) { console.error("Error when trying to get clipboard content from system"); }
return url;
}
function btn_newtab_w_url_click()
{
gBrowser.loadTabs([btn_newtab_w_url_clipboard_str] , {
inBackground: false,
relatedToCurrent: false,
triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}) //FF63
});
}
function newtabbtnContextMenu_onpopupshowing(event)
{
btn_newtab_w_url_clipboard_str = _readFromClipboard();
if (document.getElementById("btn_newtab_w_url"))
{
document.getElementById("btn_newtab_w_url").setAttribute("label", new_tab_url_label + btn_newtab_w_url_clipboard_str);
document.getElementById("btn_newtab_w_url").setAttribute("tooltiptext", new_tab_url_label + btn_newtab_w_url_clipboard_str);
}
if (document.getElementById("btn_newtab_w_url_2"))
{
document.getElementById("btn_newtab_w_url_2").setAttribute("label", new_tab_url_label + btn_newtab_w_url_clipboard_str);
document.getElementById("btn_newtab_w_url_2").setAttribute("tooltiptext", new_tab_url_label + btn_newtab_w_url_clipboard_str);
}
}
(() => {
var newtabbtnContextMenu = document.createXULElement("menupopup");
newtabbtnContextMenu.id = "newtabbtnContextMenu";
newtabbtnContextMenu.setAttribute("onpopupshowing","newtabbtnContextMenu_onpopupshowing(event)");
var btn_newtab = document.createXULElement("menuitem");
btn_newtab.setAttribute("label", 'New tab');
btn_newtab.setAttribute("oncommand", "BrowserOpenTab()");
newtabbtnContextMenu.appendChild(btn_newtab);
var btn_newtab_w_url = document.createXULElement("menuitem");
btn_newtab_w_url.id = "btn_newtab_w_url";
btn_newtab_w_url.setAttribute("label", new_tab_url_label);
btn_newtab_w_url.setAttribute("oncommand", "btn_newtab_w_url_click()");
newtabbtnContextMenu.appendChild(btn_newtab_w_url);
document.getElementById("mainPopupSet").appendChild(newtabbtnContextMenu);
const tabs_newtab_button = document.getElementById("tabs-newtab-button");
var observer1 = new MutationObserver(function(){
observer1.disconnect();
tabs_newtab_button.setAttribute("context","newtabbtnContextMenu");
});
observer1.observe(tabs_newtab_button,{attributes:true});
const new_tab_button = document.getElementById("new-tab-button");
var observer2 = new MutationObserver(function(){
observer2.disconnect();
new_tab_button.setAttribute("context","newtabbtnContextMenu");
});
observer2.observe(new_tab_button,{attributes:true});
const default_new_tab_button_popup = document.getElementById("new-tab-button-popup");
function shift_default_menu(){
if (default_new_tab_button_popup.getAttribute("onpopupshowing") == "return CreateContainerTabMenu(event);" )
{
default_new_tab_button_popup.setAttribute("onpopupshowing", "CreateContainerTabMenu_mod(event);" )
}
}
shift_default_menu();
var observer_dealwith_default = new MutationObserver(function(){
observer_dealwith_default.disconnect();
shift_default_menu();
observer_dealwith_default.observe(default_new_tab_button_popup, {attributes:true} );
});
observer_dealwith_default.observe(default_new_tab_button_popup, {attributes:true} );
})();
function CreateContainerTabMenu_mod(event) {
CreateContainerTabMenu(event);
event.target.appendChild(document.createXULElement("menuseparator"));
var btn_newtab = document.createXULElement("menuitem");
btn_newtab.setAttribute("label", 'New tab');
btn_newtab.setAttribute("oncommand", "BrowserOpenTab()");
event.target.appendChild(btn_newtab);
var btn_newtab_w_url_2 = document.createXULElement("menuitem");
btn_newtab_w_url_2.id = "btn_newtab_w_url_2";
btn_newtab_w_url_2.setAttribute("label", new_tab_url_label);
btn_newtab_w_url_2.setAttribute("oncommand", "btn_newtab_w_url_click()");
event.target.appendChild(btn_newtab_w_url_2);
newtabbtnContextMenu_onpopupshowing();
}