-
Notifications
You must be signed in to change notification settings - Fork 74
/
Updatebookmarklite.uc.js
275 lines (264 loc) · 11.4 KB
/
Updatebookmarklite.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// ==UserScript==
// @name Updatebookmarklite.uc.js
// @description 书签维护
// @author danny
// @include main
// @compatibility Firefox 11.0 10.0+
var updatebookmark = {
init: function(){
window.addEventListener("load", updatebookmark.onload, false);
window.setTimeout("updatebookmark.onload();", 0);
},
uninit: function(event){
var plaMenu = document.getElementById("placesContext");
var menubook1 = document.getElementById("updatebookmarkUpdateCurrentURLMenuItem");
var menubook2 = document.getElementById("updatebookmarkUpdateCurrentTitleMenuItem");
var menubook3 = document.getElementById("addnewbookmarkMenuItem");
if (menubook1 && plaMenu) {
plaMenu.removeChild(menubook1);
}
if (menubook2 && plaMenu) {
plaMenu.removeChild(menubook2);
}
if (menubook3 && plaMenu) {
plaMenu.removeChild(menubook3);
}
window.removeEventListener("load", updatebookmark.onload, false);
},
onload: function() {
var placesContextMenu = document.getElementById("placesContext");
var menuItem = document.createElement("menuitem");
menuItem.setAttribute("id", "updatebookmarkUpdateCurrentURLMenuItem");
menuItem.setAttribute("label", "更新为当前地址");
menuItem.setAttribute("oncommand", "updatebookmark.updateURL();");
menuItem.setAttribute("closemenu", "single");
menuItem.setAttribute("selection", "link");
placesContextMenu.insertBefore(menuItem,placesContextMenu.childNodes[1]);
menuItem = document.createElement("menuitem");
menuItem.setAttribute("id", "updatebookmarkUpdateCurrentTitleMenuItem");
menuItem.setAttribute("label", "更新为当前标题");
menuItem.setAttribute("oncommand", "updatebookmark.updateTitle();");
menuItem.setAttribute("closemenu", "single");
menuItem.setAttribute("selection", "link");
placesContextMenu.insertBefore(menuItem,placesContextMenu.childNodes[2]);
menuItem = document.createElement("menuitem");
menuItem.setAttribute("id", "addnewbookmarkMenuItem");
menuItem.setAttribute("label", "在此书签后面添加新书签");
menuItem.setAttribute("oncommand", "updatebookmark.appendURL();");
menuItem.setAttribute("closemenu", "single");
menuItem.setAttribute("selection", "link");
placesContextMenu.insertBefore(menuItem,placesContextMenu.childNodes[3]);
menuItem = document.createElement("menuitem");
menuItem.setAttribute("id", "updatebookmarkMenuItem");
menuItem.setAttribute("label", "更新当前书签");
menuItem.setAttribute("oncommand", "updatebookmark.updatebookreplace();");
menuItem.setAttribute("closemenu", "single");
menuItem.setAttribute("selection", "link");
placesContextMenu.insertBefore(menuItem,placesContextMenu.childNodes[4]);
placesContextMenu.addEventListener("popupshowing", updatebookmark.onpopup, false);
},
onpopup: function(event) {
// show only when single item is clicked and when item is link
var node = document.popupNode;
var isSingleLink = false;
if ("node" in node) {
node = node.node;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else
if ("_placesNode" in node) {
node = node._placesNode;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else {
if ("view" in node.parentNode) {
node = node.parentNode.view.nodeForTreeIndex(node.parentNode.view.selection.currentIndex);
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
}
}
var menubook1 = document.getElementById('updatebookmarkUpdateCurrentURLMenuItem');
var menubook2 = document.getElementById('updatebookmarkUpdateCurrentTitleMenuItem');
var menubook3 = document.getElementById('addnewbookmarkMenuItem');
var menubook4 = document.getElementById('updatebookmarkMenuItem');
var activeContent = top.window.document.getElementById("content");
var activeBrowser = activeContent.selectedBrowser;
var newurlValue = activeBrowser.currentURI.spec;
if (newurlValue == "about:blank" || newurlValue == "about:home") {
if (menubook1) menubook1.hidden = true;
if (menubook2) menubook2.hidden = true;
if (menubook3) menubook3.hidden = true;
if (menubook4) menubook4.hidden = true;
} else {
if (isSingleLink) {
var oldURI = PlacesUtils.bookmarks.getBookmarkURI(node.itemId);
var oldurl = oldURI.spec;
var oldtitleValue = PlacesUtils.bookmarks.getItemTitle(node.itemId);
var newURI = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).
newURI(newurlValue, null, null);
var newurl = newURI.spec;
var newtitleValue = activeBrowser.contentTitle;
//如果标题或地址与当前tab的符合,显示弹出菜单spec
if (oldurl == newurl || oldtitleValue == newtitleValue) {
if (menubook1) menubook1.hidden = false;
if (menubook2) menubook2.hidden = false;
if (menubook3) menubook3.hidden = true;
if (menubook4) menubook4.hidden = true;
return;
} else {
if (menubook1) menubook1.hidden = true;
if (menubook2) menubook2.hidden = true;
if (menubook3)
{ menubook3.hidden = false;
menubook3.setAttribute("label", "在此书签后面添加新书签");
}
if (menubook4) menubook4.hidden = false;
return;
}
} else {
if (menubook1) menubook1.hidden = true;
if (menubook2) menubook2.hidden = true;
if (menubook3)
{ menubook3.hidden = false;
menubook3.setAttribute("label", "在此文件夹添加新书签");
}
if (menubook4) menubook4.hidden = true;
}
}
},
updateURL: function() {
var node = document.popupNode;
var isSingleLink = false;
if ("node" in node) {
node = node.node;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else
if ("_placesNode" in node) {
node = node._placesNode;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else {
if ("view" in node.parentNode) {
node = node.parentNode.view.nodeForTreeIndex(node.parentNode.view.selection.currentIndex);
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
}
}
try {
var newValue;
var activeContent = top.window.document.getElementById("content");
var activeBrowser = activeContent.selectedBrowser;
newValue = activeBrowser.currentURI.spec;
var oldURI = PlacesUtils.bookmarks.getBookmarkURI(node.itemId);
var newURI = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).
newURI(newValue, null, null);
// add old tags onto new uri
var oldValueTags = PlacesUtils.tagging.getTagsForURI(oldURI, {});
PlacesUtils.tagging.tagURI(newURI, oldValueTags);
PlacesUtils.bookmarks.changeBookmarkURI(node.itemId, newURI);
} catch (ex) {
alert("无法更新书签. 无效的URL.");
}
},
updateTitle: function() {
var node = document.popupNode;
var isSingleLink = false;
if ("node" in node) {
node = node.node;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else
if ("_placesNode" in node) {
node = node._placesNode;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else {
if ("view" in node.parentNode) {
node = node.parentNode.view.nodeForTreeIndex(node.parentNode.view.selection.currentIndex);
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
}
}
try {
var newValue;
var activeContent = top.window.document.getElementById("content");
var activeBrowser = activeContent.selectedBrowser;
newValue = activeBrowser.contentTitle;
PlacesUtils.bookmarks.setItemTitle(node.itemId, newValue);
} catch (ex) { alert(ex.message); }
},
updatebookreplace: function() {
var node = document.popupNode;
var isSingleLink = false;
if ("node" in node) {
node = node.node;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else
if ("_placesNode" in node) {
node = node._placesNode;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else {
if ("view" in node.parentNode) {
node = node.parentNode.view.nodeForTreeIndex(node.parentNode.view.selection.currentIndex);
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
}
}
try {
var newValue;
var activeContent = top.window.document.getElementById("content");
var activeBrowser = activeContent.selectedBrowser;
newValue = activeBrowser.currentURI.spec;
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
var items = ["更新标题和地址", "仅更新地址", "仅更新标题"]; // list items
var uselected = {};
var result = prompts.select(null, "Title", "What greeting do you want?", items.length, items, uselected);
if (result) {
var oldURI = PlacesUtils.bookmarks.getBookmarkURI(node.itemId);
var newURI = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).
newURI(newValue, null, null);
//add old tags onto new uri
var newtitleValue = activeBrowser.contentTitle;
if (uselected.value == 0 || uselected.value == 2)
PlacesUtils.bookmarks.setItemTitle(node.itemId, newtitleValue);
if (uselected.value == 0 || uselected.value == 1) {
var oldValueTags = PlacesUtils.tagging.getTagsForURI(oldURI, {});
PlacesUtils.tagging.tagURI(newURI, oldValueTags);
PlacesUtils.bookmarks.changeBookmarkURI(node.itemId, newURI);
}
}
} catch (ex) {
alert("无法更新书签. 无效的URL.");
}
},
appendURL: function() {
var node = document.popupNode;
var isSingleLink = false;
if ("node" in node) {
node = node.node;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else
if ("_placesNode" in node) {
node = node._placesNode;
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
} else {
if ("view" in node.parentNode) {
node = node.parentNode.view.nodeForTreeIndex(node.parentNode.view.selection.currentIndex);
isSingleLink = node && PlacesUtils.nodeIsBookmark(node);
}
}
try {
var newValue;
var activeContent = top.window.document.getElementById("content");
var activeBrowser = activeContent.selectedBrowser;
newValue = activeBrowser.currentURI.spec;
var newtitle = activeBrowser.contentTitle;
var newURI = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).
newURI(newValue, null, null);
if (isSingleLink) {
var pnodeid = PlacesUtils.bookmarks.getFolderIdForItem(node.itemId);
PlacesUtils.bookmarks.insertBookmark(pnodeid,newURI,PlacesUtils.bookmarks.getItemIndex(node.itemId)+1,newtitle);
} else {
var pnodeid = node.itemId;
PlacesUtils.bookmarks.insertBookmark(pnodeid,newURI,'DEFAULT_INDEX',newtitle);
}
} catch (ex) { alert(ex.message); }
}
};
updatebookmark.init();
window.addEventListener("unload", function(event){ updatebookmark.uninit(event); }, false);