-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
151 lines (134 loc) · 4.18 KB
/
commands.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
Object.prototype.extend = function (src) {
for (var prop in src)
this[prop] = this[prop] || src[prop];
return this;
}
var metadata = {
homepage: "http://web.student.chalmers.se/~arvidj",
author: {
name: "Arvid Jakobsson", email: "arvid.jakobsson@gmail.com"},
license: "GPL"
/*
* name: "date",
* help: "If you're in an editable text area, inserts today's date, formatted for the current locale.",
* description: "Inserts today's date.",
* */
};
var ArvidsCommands = [
{
name: "up",
description: "Moves up one dir, e.g.: from <em>http://example.com/aaa/bbb/ccc.html</em> to <em>http://example.com/aaa/</em>",
execute: function() {
var l = getDocument().location.pathname = this._parentDir();
},
preview: function(pblock) {
var msg = 'Go to: "<i>${parentDir}</i>"';
pblock.innerHTML = CmdUtils.renderTemplate( msg, {parentDir: this._parentDir()} );
},
_parentDir: function() {
var l = getDocument().location;
return l.pathname.split("/").slice(0,-1).join("/");
}
},
{
name: "base",
description: "Goes to the root of the host, e.g.: from <em>http://example.com/aaa/bbb/ccc.html</em> to <em>http://example.com/</em>",
execute: function() {
getDocument().location.href = this._baseDir();
},
preview: function(pblock) {
var msg = 'Go to: "<i>${baseDir}</i>"';
pblock.innerHTML = CmdUtils.renderTemplate( msg, {baseDir: this._baseDir()} );
},
_baseDir: function() {
var l = getDocument().location;
return l.protocol + "://" + l.hostname + l.port;
}
},
{
name: "coral",
description: "Finds mirror of the current page using the coral content network.",
execute: function() {
getDocument().location.href = this._baseDir();
},
preview: function(pblock) {
var msg = 'Coralize this url, goes to a mirror at: "<i>${coralUrl}</i>"';
pblock.innerHTML = CmdUtils.renderTemplate( msg, {coralUrl: this._coralize()} );
},
_coralize: function() {
var l = getDocument().location;
return l.protocol + "://" + l.hostname + ".nyud.net" + l.port + l.pathname + l.search + l.hash;
}
},
{
name: "src",
description: "Goes to the source of any ubiquity commands on page.",
execute: function() {
if ((h = $xs("//link[@rel='commands']", getDocument()).href)) {
Utils.openUrlInBrowser(h);
}
},
preview: function(pblock) {
var msg = 'Goes to the source of any ubiquity commands on this page.';
pblock.innerHTML = CmdUtils.renderTemplate(msg);
}
}
];
ArvidsCommands = ArvidsCommands.map(function (cmd) {
return cmd.extend(metadata);
});
ArvidsCommands.forEach(function(cmd) {
if (this.CmdUtils !== undefined) CmdUtils.CreateCommand(cmd);
});
function getCmdsMeta() {
var metaprops = ["homepage", "author", "name", "email", "license", "help", "description"];
return ArvidsCommands.map(function(cmd) {
var ret = {};
for (var prop in cmd)
if (metaprops.indexOf(prop))
ret[prop] = cmd[prop];
return ret;
});
}
/* STD-Functions */
function getLocation() {
return Application.activeWindow.activeTab.document.location;
}
function getDocument() {
return Application.activeWindow.activeTab.document;
}
function $x(xpath, root) {
// From Johan Sundström
var doc = root ? root.evaluate ? root : root.ownerDocument : document, next;
var got = doc.evaluate(xpath, root||doc, null, null, null), result = [];
while((next = got.iterateNext()))
result.push(next);
return result;
}
function $xs(xpath, root) {
return $x(xpath, root)[0];
}
String.prototype.trim = function() { return this.replace(/^\W+|\W+$/gi, ""); }
function bind(method, obj) {
return function() {
method.apply(obj, arguments);
};
}
function curry(method, obj) {
var curryargs = $A(arguments).slice(2);
return function() {
return method.apply(obj || window, curryargs.concat($A(arguments)));
};
}
function $A(arr) {
var r = [], len = arr.length;
while (len--) r.unshift(arr[len]);
return r;
}
function sprintf(str) {
var a, args = $A(arguments);
args.shift();
while (a = args.shift())
str = str.replace("\f", a);
return str;
}