whym (owner)

Revisions

gist: 171089 Download_button fork
public
Description:
Minibuffer command for tagging multiple posts on Tumblr. Type 'p' to mark/unmark a post, type 'g' to tag. インストールはこちらで http://userscripts.org/scripts/show/56547
Public Clone URL: git://gist.github.com/171089.git
Embed All Files: show embed
README.rst #

TumblrTagCommand - fast tagger for Tumblr posts

Date:2009-12-21
Version:0.0.5
Homepage:TumblrTagCommand@UserScripts
Contact:http://github.com/whym

Features

TumblrTagCommand supports:

Install

  1. First, install softwares/addons/scripts below, if you don't have.

    1. Firefox 3.5 or 3.6
    2. Greasemonkey
    3. Minibuffer for Greasemonkey (Dec 5, 2009)
    4. LDRize for Minibuffer (Dec 5, 2009)
    5. Autopagerize for Greasemonkey (optional, but strongly recommended)

    Note that some of the scripts are sensitive to the ordering; Minibuffer, LDRize and TumblrTagCommand should be placed in this order. For the ordering, see also the notes on the bottom of the Minibuffer page.

  1. Install TumblrTagCommand at UserScripts page or Gist page.

Usage: Pin & tag

  1. Choose posts with p.

    Go to a tumblr page (including Dashboard). Mark the target posts with pins on Dashboard.

    • To choose multiple posts on dashboard, move next/previous with j/k and mark(or pin) the current post with p. Just follow the general usage of LDRize.
    • Same feature is available at /tagged pages, too.
  2. Type g and input tags.

    Type g to show a prompt, and input tags that are to be added/deleted.

    • Use comma (,) to separate multiple tags.
    • Use minus (-) to mark deletion of a tag.

    ex. To add the tag pic and remove picture, input pic,-picture.

See also Screenshot by vaedar.

Usage: Tag all

  1. Type Shift + g and input tags.

    If you want to tag all of the post in the current page (e.g., http://yourname.tumblr.com/page/1), just type Shift + G and then input tags.

Known bugs

  • Doesn't work on private posts (due to LDRize's limitation, see also here)

History

2009-08-20 0.0.1
First version. (notes in ja)
2009-08-25 0.0.2
Bug fix for clear-pin and spaces in tags. (notes in ja)
2009-09-05 0.0.3
Support of /drafts and /queue URLs. Bug fix for error trap etc. (notes in ja)
2009-09-17 0.0.4
Bug fix for manual edit and unsupported input, etc. (notes in ja)
2009-12-21 0.0.5
'Tag all' with Shfit + G, etc. (notes in ja)
2010-01-25 0.0.6
Fix for Fx 3.6, etc. (cf. http://coderepos.org/share/changeset/36509)

Links

tumblrtagcommand.user.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// ==UserScript==
// @name TumblrTagCommand
// @namespace http://whym.github.com
// @description Minibuffer command for tagging Tumblr posts. Type 'p' to mark/unmark a post and type 'g' to tag. Requires Minibuffer, LDRize & Autopagerize.
// @include http://www.tumblr.com/dashboard*
// @include http://www.tumblr.com/show/*
// @include http://www.tumblr.com/drafts*
// @include http://www.tumblr.com/queue*
// @include http://www.tumblr.com/tumblelog/*
// @include http://*.tumblr.com/tagged/*
// @include http://*.tumblr.com/post/*
// @include http://*.tumblr.com/private/*
// @include http://*.tumblr.com/search/*
// @include http://*.tumblr.com/page/*
// @include http://*.tumblr.com/archive/*
// @include http://*.tumblr.com/
// @version 0.0.6
// ==/UserScript==
 
// most parts are copied from ReblogCommand
// http://coderepos.org/share/browser/lang/javascript/userscripts/reblogcommand.user.js
 
(function() {
 
const KEY_TAG = 'g';
const KEY_TAG_ALL = 'G';
const COMMAND_TAG = 'tumblr-tag-add';
const COMMAND_POSTS = 'tumblr-posts';
const ALLOW_OWN_DOMAIN = true;
const REGEXP_SEPARATOR = /,/;
const SEPARATOR = ',';
 
var boot = function() {
var $X = window.Minibuffer.$X;
var D = window.Minibuffer.D;
var createDocumentFromString = window.Minibuffer.createDocumentFromString;
 
// ----------------------------------------------------------------------------
// Tumblr Tag
// ----------------------------------------------------------------------------
 
function parseTagList(args) {
var ret = [];
if ( !(args instanceof Array) ) {
args = [args];
}
args.forEach(function(x){
x = (''+x).trim();
if (x.length > 0) {
ret = ret.concat(x.split(REGEXP_SEPARATOR).map(function(x){return x.trim();}));
}
});
return ret;
}
 
Array.prototype.uniq = function() {
var ret = [];
var result = this.reduce(function(res, item) {
if (!res.found[item]) {
res.values.push(item);
res.found[item] = 1;
ret.push(item);
};
return res;
}, { values: [], found: {}});
return ret;
};
Array.prototype.remove = function(x) {
var removed = {};
if ( x instanceof Array ) {
x.forEach(function(x){
removed[x] = 1;
});
} else {
removed[x] = 1;
}
return this.filter(function(item) {
return !removed[item];
});
};
if ( String.prototype.trim == undefined ) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/, '');
};
}
 
function isTumblrDashboardURL(url) {
return url.match("^http://www\\.tumblr\\.com/dashboard") ? true : false;
}
 
function isTumblrUserURL(url) {
return url.match("^https?://[^.]+\\.tumblr\\.com/(post|private)/(\\d+)") ||
// tumblr allow to use own domain. but this is risky.
// $X('id("tumblr_controls")[self::iframe]', Boolean)
(ALLOW_OWN_DOMAIN && url.match("^https?://[^/]+/(post|private)/(\\d+)")) ? true : false;
}
 
function getIDByPermalink(url) {
if (isTumblrUserURL(url)) {
return RegExp.$2;
}
// return what ?
return false;
}
 
function getURLByID(id, token) {
return "http://www.tumblr.com/edit/" + id + "?redirect_to=/dashboard";
}
 
// copy from tombloo
function unescapeHTML(s) {
return s.replace(/"/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&');
}
 
// copy from tombloo
function getReblogToken(url) {
url = unescapeHTML(url);
if (/&pid=([^&]*)&rk=([^&\"]*)/.test(url) || /\/reblog\/([^\/]+)\/([^?]*)/.test(url))
return {
id : RegExp.$1,
token : RegExp.$2
};
}
 
function parseParams(doc) {
// name() returns in lower case on fx3.6
var elms = $X('id("edit_post")//*[translate(name(),"input", "INPUT") ="INPUT" or translate(name(),"textarea", "TEXTAREA")="TEXTAREA" or translate(name(),"select", "SELECT")="SELECT"]', doc);
var params = {};
elms.forEach(function(elm) {
params[elm.name] = elm.value;
});
return params;
}
 
function createPostData(params) {
var arr = [];
for (var param in params) {
if (param != "preview_post" && param != "send_to_twitter") {
arr.push(encodeURIComponent(param));
arr.push("=");
arr.push(encodeURIComponent(params[param]));
arr.push("&");
}
}
return arr.join('');
}
 
function edittags(aURL, appends, removes) {
var id = getIDByPermalink(aURL);
var d;
with (D()) {
d = Deferred();
if (!id) {
wait(0).next(function() { d.call(); });
return d;
}
}
var message = '';
if (appends.length > 0) {
message += 'Adding '+appends.join(SEPARATOR);
}
if (removes.length > 0) {
message += (message.length > 0 ? ', removing ': 'Removing ')+removes.join(SEPARATOR);
}
window.Minibuffer.status(COMMAND_TAG+id, message+' ...');
d = D();
var url = aURL;
var editd = d.xhttp.get(url).
next(function(res) {
var token = getReblogToken(res.responseText.match('iframe src="((?:\\"|[^"])*)"')[1]);
url = getURLByID(token.id, token.token);
return d.xhttp.get(url);
}).
next(function(res) {
var params = parseParams(createDocumentFromString(res.responseText));
params['post[tags]'] = parseTagList(params['post[tags]']).concat(appends).remove(removes).uniq().join(SEPARATOR);
//if(!confirm(params['post[tags]'])){return d;}
if (!params.form_key)
editd.fail("invalid params");
return d.xhttp.post(url, createPostData( params ));
}).
next(function(res) {
if (res.status == 200) {
if ( ! res.finalUrl.match( /^http:\/\/www\.tumblr\.com\/dashboard/ ) )
editd.fail("unexpected response: " + res.finalUrl);
else
window.Minibuffer.status(COMMAND_TAG+id, message+' ... done.', 100);
} else {
window.Minibuffer.status(COMMAND_TAG+id, message+' ... error '+res.status+' .', 100);
editd.fail(res.status + 'in adding tag');
}
d.call();
}).
error(function(res) {
window.Minibuffer.status(COMMAND_TAG+id, message+' ... error.', 100);
if (confirm('error: '+res+', edit manually ? \n' + url))
editTagManually(url);
d.call();
});
return d;
}
 
function editTagManually(aURL) {
var url = aURL;
if (url.match(/\/edit\//)) {
GM_openInTab(url);
return;
}
var d = D();
d.xhttp.get(url).
next(function(res) {
var token = getReblogToken(res.responseText.match('iframe src="((?:\\"|[^"])*)"')[1]);
url = getURLByID(token.id, token.token);
GM_openInTab(url);
});
}
 
 
// ----------------------------------------------------------------------------
// Command
// ----------------------------------------------------------------------------
 
function getTargetCommand() {
var target_cmd = '';
if (isTumblrUserURL(window.location.href)) {
target_cmd = 'location';
} else if (window.LDRize) {
target_cmd = 'pinned-or-current-link';
} else {
target_cmd = 'location';
}
return target_cmd;
}
 
window.Minibuffer.addShortcutkey({
key: KEY_TAG,
description: 'Tag Tumblr posts',
command: function() {
var target_cmd = getTargetCommand();
var clear_pin = (target_cmd == 'pinned-or-current-link') ? ' | clear-pin' : '';
var tags = prompt("Input new tags and the tags to be deleted.\n(ex. To add 'picture' and remove 'pict', use 'picture,-pict'.)");
window.Minibuffer.execute(target_cmd + ' | '+[COMMAND_TAG, tags].join(' ')+clear_pin);
}
});
 
window.Minibuffer.addShortcutkey({
key: KEY_TAG_ALL,
description: 'Tag all Tumblr posts in screen',
command: function() {
var target_cmd = 'tumblr-posts';
var tags = prompt("Input new tags and the tags to be deleted.\n(ex. To add 'picture' and remove 'pict', use 'picture,-pict'.)");
window.Minibuffer.execute(target_cmd + ' | '+[COMMAND_TAG, tags].join(' '));
}
});
 
window.Minibuffer.addCommand({
name: COMMAND_POSTS,
command: function(stdin) {
var linkx = window.LDRize.getSiteinfo().link;
var parax = window.LDRize.getSiteinfo().paragraph;
var getfirst = function(x){
return x[0] != undefined ? x[0] : x;
};
var items = $X(parax).map(function(i){ return getfirst($X(linkx, i));});
return items;
}
});
 
 
window.Minibuffer.addCommand({
name: COMMAND_TAG,
command: function(stdin) {
var args = parseTagList(this.args.join(' '));
var urls = [];
if (!stdin.length) {
if (isTumblrDashboardURL(window.location.href.toString())) {
var link = window.Minibuffer.execute('current-link');
if (link) urls = [link.toString()];
} else {
// command line is just 'tumblr-tag-add'
urls = [window.location.href];
}
} else {
urls = [];
stdin.forEach(function(a){
if (a && typeof a == 'string') {
// command line is 'location | tumblr-tag-add'
urls.push(a);
} else if (a && a.nodeName && a.href && a.nodeName == 'A') {
// command line is 'pinned-or-current-link | tumblr-tag-add'
urls.push(a.href);
} else {
window.Minibuffer.status(COMMAND_TAG, 'Unsupported input: ' + a, 1000);
}
});
}
 
if (urls.length == 0){
window.Minibuffer.status(COMMAND_TAG, 'no input ', 1000);
return null;
}
 
// edit tags
if (args.length >= 1) {
var removes = [];
var appends = [];
args.forEach(function(x){
if (x.charAt(0) == '-') {
removes.push(x.substr(1));
} else {
if (x.charAt(0) == '+')
x = x.substr(1);
appends.push(x);
}
});
appends = parseTagList(appends).uniq();
removes = parseTagList(removes).uniq();
urls = urls.filter(isTumblrUserURL);
if (!urls.length) return stdin;
var lst = urls.map(function(x){return edittags(x, appends, removes);});
if (lst.length > 1) {
with (D()) {
parallel(lst).wait(2).
next(function() { window.Minibuffer.status(COMMAND_TAG, 'Everything is OK', 1000); });
}
}
return stdin;
} else {
window.Minibuffer.status(COMMAND_TAG, 'No tag is specified', 1000);
return null;
}
}
});
};
 
if (document.body.id == 'tinymce')
return;
 
if (window.Minibuffer) {
boot();
} else {
window.addEventListener('GM_MinibufferLoaded', boot, false);
}
 
})();