forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copybookmark.uc.js
165 lines (151 loc) · 6.71 KB
/
copybookmark.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
// ==UserScript==
// @id copyBookmark
// @name Enhanced Bookmark Copy
// @version 0.9.2
// @namespace simon
// @author Simon Chan
// @description Let you copy title or both title and url of bookmark(s) easier.
// @include chrome://browser/content/browser.xul
// @include chrome://browser/content/places/places.xul
// @include chrome://browser/content/bookmarks/bookmarksPanel.xul
// @include chrome://browser/content/history/history-panel.xul
// @run-at document-end
// ==/UserScript==
(function () {
var targetURIs = [
"chrome://browser/content/browser.xul",
"chrome://browser/content/places/places.xul",
"chrome://browser/content/bookmarks/bookmarksPanel.xul",
"chrome://browser/content/history/history-panel.xul"
];
if (!location in targetURIs)
return;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
var stringBundles = {
"zh-CN": {
"copyUrl": "%E5%A4%8D%E5%88%B6%E5%9C%B0%E5%9D%80", // "复制地址"
"copyTitle": "%E5%A4%8D%E5%88%B6%E6%A0%87%E9%A2%98", //"复制标题",
"copyBoth": "%E5%A4%8D%E5%88%B6%E6%A0%87%E9%A2%98%E5%92%8C%E5%9C%B0%E5%9D%80", //"复制标题和地址",
"folder": "(%E6%96%87%E4%BB%B6%E5%A4%B9)", //"(文件夹)",
"noTitle": "(%E6%97%A0%E6%A0%87%E9%A2%98)", //"(无标题)"
},
"zh-TW": {
"copyUrl": "%E8%A4%87%E8%A3%BD%E5%9C%B0%E5%9D%80", // "複製地址"
"copyTitle": "%E8%A4%87%E8%A3%BD%E6%A8%99%E9%A1%8C", //"複製標題",
"copyBoth": "%E8%A4%87%E8%A3%BD%E6%A8%99%E9%A1%8C%E5%92%8C%E5%9C%B0%E5%9D%80", //"複製標題和地址",
"folder": "(%E6%96%87%E4%BB%B6%E5%A4%BE)", //"(文件夾)",
"noTitle": "(%E7%84%A1%E6%A8%99%E9%A1%8C)", //"(無標題)"
},
"en-US": {
"copyUrl": "Copy url",
"copyTitle": "Copy title",
"copyBoth": "Copy title and url",
"folder": "(folder)",
"noTitle": "(No Title)"
}
};
var locale = Cc["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch)
.getCharPref("general.useragent.locale");
var stringBundle = stringBundles[locale] || stringBundles["en-US"];
function getI18n(key) {
return decodeURI(stringBundle[key]);
}
var topLevel;
function getSpace(indentLevel) {
var str = [];
for (var i = -1; i < indentLevel - topLevel; i++)
str.push(null);
return str.join(" ");
}
XPCOMUtils.defineLazyServiceGetter(this, "annotations",
"@mozilla.org/browser/annotation-service;1",
"nsIAnnotationService");
function getChildren(node, type) {
var results = [];
var space = getSpace(node.indentLevel);
if (PlacesUtils.nodeIsFolder(node) && annotations
.itemHasAnnotation(node.itemId, PlacesUtils.LMANNO_FEEDURI)) {
results.push(space + node.title);
if (type == "both")
results.push(space + annotations
.getItemAnnotation(node.itemId, PlacesUtils.LMANNO_FEEDURI));
}
else if (PlacesUtils.nodeIsContainer(node)) {
asContainer(node);
var wasOpen = node.containerOpen;
if (!wasOpen)
node.containerOpen = true;
results.push(space + node.title + " " + getI18n("folder"));
for (var i = 0; i < node.childCount; i++)
results = results.concat(getChildren(node.getChild(i), type));
node.containerOpen = wasOpen;
}
if (PlacesUtils.nodeIsURI(node)) {
if (node.title == null)
results.push(space + getI18n("noTitle"));
else
results.push(space + node.title);
if (type == "both")
results.push(space + node.uri);
}
if (PlacesUtils.nodeIsSeparator(node))
results.push(space + "--------------------");
return results;
}
XPCOMUtils.defineLazyServiceGetter(this, "clipboard",
"@mozilla.org/widget/clipboardhelper;1",
"nsIClipboardHelper");
function copyBookmark_copy(type) {
var results = [];
PlacesUIUtils.getViewForNode(document.popupNode).selectedNodes.forEach(function (node) {
topLevel = node.indentLevel;
if (PlacesUtils.nodeIsFolder(node) &&
asQuery(node).queryOptions.excludeItems) {
var oldState = node.containerOpen;
var concreteId = PlacesUtils.getConcreteItemId(node);
results = results.concat(getChildren(PlacesUtils
.getFolderContents(concreteId, false, true).root, type));
node.containerOpen = oldState;
}
else
results = results.concat(getChildren(node, type));
});
clipboard.copyString(results.join("\r\n"));
}
var copyTitleMenuItem = document.createElement("menuitem");
copyTitleMenuItem.id = "copyBookmark_copyTitle";
copyTitleMenuItem.setAttribute("label", getI18n("copyTitle"));
copyTitleMenuItem.setAttribute("selection", "any");
copyTitleMenuItem.setAttribute("closemenu", "single");
copyTitleMenuItem.addEventListener("command", function () { copyBookmark_copy(); });
copyTitleMenuItem.setAttribute("accesskey", "t");
var copyBothMenuItem = document.createElement("menuitem");
copyBothMenuItem.id = "copyBookmark_copyBoth";
copyBothMenuItem.setAttribute("label", getI18n("copyBoth"));
copyBothMenuItem.setAttribute("selection", "any");
copyBothMenuItem.setAttribute("closemenu", "single");
copyBothMenuItem.addEventListener("command", function () { copyBookmark_copy("both"); });
copyBothMenuItem.setAttribute("accesskey", "u");
var copyMenuItem = document.getElementById("placesContext_copy");
copyMenuItem.setAttribute("label", getI18n("copyUrl"));
copyMenuItem.parentNode.insertBefore(copyTitleMenuItem, copyMenuItem);
copyMenuItem.parentNode.insertBefore(copyBothMenuItem, copyMenuItem.nextSibling);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function QI_node(aNode, aIID) {
var result = null;
try {
result = aNode.QueryInterface(aIID);
}
catch (e) { }
return result;
}
function asContainer(aNode) {
return QI_node(aNode, Ci.nsINavHistoryContainerResultNode);
}
function asQuery(aNode) {
return QI_node(aNode, Ci.nsINavHistoryQueryResultNode);
}
})();