forked from yoko/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflickr_adds_focal_length_on_a_35mm.user.js
107 lines (94 loc) · 2.92 KB
/
flickr_adds_focal_length_on_a_35mm.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
// ==UserScript==
// @name handlerFlickr Adds Focal Length on a 35mm
// @namespace http://codefairy.org/ns/userscripts
// @include http://www.flickr.com/*/meta/*
// @include http://flickr.com/*/meta/*
// @version 0.2.5
// @license MIT License
// @work Greasemonkey
// @work GreaseKit
// ==/UserScript==
new function() {
var cacheable = (typeof GM_setValue == 'function' && typeof GM_getValue == 'function');
var camera = $X('id("Inbox")//tr/td[preceding-sibling::td/b[text()="Camera:"]]/b');
var focalLength = $X('id("Inbox")//tr/td[preceding-sibling::td/b[text()="Focal Length:"]]/b');
if (!camera || !focalLength) return;
camera = camera[0];
focalLength = focalLength[0];
var w = typeof unsafeWindow != 'undefined' ? unsafeWindow : window;
var now = new Date().getTime();
var uri = 'http://wedata.net/databases/AngleOfView/items';
var handler = function(data) {
var text = 'unknown on a 35mm camera. <a href="'+uri+'">add angle of view data?</a>';
for (var i = 0, length = data.length; i < length; i++) {
var item = data[i];
if (item.name == camera.textContent) {
if (item.data.ratio == 1)
text = 'with a full-frame';
else {
var angleOfView = Math.round(parseFloat(focalLength.textContent) * parseFloat(item.data.ratio));
text = angleOfView+' mm on a 35 mm camera';
}
break;
}
}
focalLength.innerHTML += ' ('+text+')';
};
var load = function() {
return eval(GM_getValue('stash'));
};
var save = function(data) {
var stash = data ?
uneval({
data : data,
expires: now + 1000 * 60 * 60 * 24
}) :
'';
GM_setValue('stash', stash);
return data;
};
if (cacheable && typeof GM_registerMenuCommand == 'function')
GM_registerMenuCommand('Clear Angle of View Data Cache', save);
var api = uri+'.json';
if (cacheable) {
var complete = false;
var stash = load();
if (stash && stash.expires >= now)
handler(stash.data);
else {
GM_xmlhttpRequest({
method: 'GET',
url : api,
onload: function(r) {
var data = eval(r.responseText);
save(data);
if (!complete) handler(data);
}
});
if (stash)
setTimeout(function() {
complete = true;
handler(save(stash.data));
}, 1000 * 30);
}
}
else {
w['jsonp'+now] = handler;
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = api+'?callback=jsonp'+now;
s.charset = 'utf-8';
document.body.appendChild(s);
}
// @source http://gist.github.com/29681.txt
function $X (exp, context, resolver, result_type) {
context || (context = document);
var Doc = context.ownerDocument || context;
var result = Doc.evaluate(exp, context, resolver, result_type || XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (result_type) return result;
for (var i = 0, len = result.snapshotLength, res = []; i < len; i++) {
res.push(result.snapshotItem(i));
}
return res;
}
};