This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
global.html
112 lines (101 loc) · 4.03 KB
/
global.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
var message = {};
safari.application.addEventListener('message', function handleMessage(e) {
switch (e.name) {
case 'mouseover':
// copy any new state into our state
for (var key in e.message)
if (e.message.hasOwnProperty(key))
message[key] = e.message[key];
var s = getStatus(message);
dispatchMessage(e.target, 'displayStatus', s);
if (e.message.href && safari.extension.settings.longURL && !safari.application.privateBrowsing.enabled) {
// TODO debounce?
lengthenURL(e.message.href, function(result) {
var url = result['long-url'];
if (url && url !== e.message.href) dispatchMessage(e.target, 'displayStatus', s + ' (' + url + ')');
});
}
break;
case 'mouseout':
dispatchMessage(e.target, 'hideStatus');
break;
}
}, false);
function dispatchMessage(to, name, data) {
return 'page' in to ? to.page.dispatchMessage(name, data) : to.dispatchMessage(name, data);
}
var keyMap = {
'0,0,0': 'go',
'0,0,1': 'readingList',
'0,1,0': 'download',
'0,1,1': 'go',
'1,0,0': 'newTabBehind',
'1,0,1': 'newTab',
'1,1,0': 'newWindowBehind',
'1,1,1': 'newWindow',
};
function getStatus(options) {
var absoluteURL = safari.extension.settings.absoluteURL,
href = decodeURIComponent(absoluteURL ? options.href : options.hrefRelative).replace(/\s+/g, ' ').trim(),
email = href.match(/^mailto:([^?]*)(\?subject=([^&]+))?/i),
script = href.match(/^javascript:(.*)/);
if (options.ctrlKey) {
return strings.menu.replace(/{href}/g, href);
} else if (script) {
return strings.script.replace(/{src}/g, script[1]);
} else if (email) {
if (email[3]) {
return strings.email.subject.replace(/{to}/g, email[1])
.replace(/{subject}/g, decodeURIComponent(email[3]));
} else {
return strings.email.to.replace(/{to}/g, email[1]);
}
} else {
var keys = [options.metaKey, options.altKey, options.shiftKey],
key = keyMap[keys.map(Number)];
if (options.target && options.target !== '_self' && key === 'go')
key = 'newTab';
return strings[key].replace(/{href}/g, href);
}
}
var strings;
(function loadNextLocale(locales) {
if (!locales.length) return;
getJSON('locale/' + locales.shift() + '.json', function(json) {
if (json)
strings = json;
else
loadNextLocale(locales);
});
})([ navigator.language, navigator.language.split('-')[0], 'en' ]);
var shortURL;
getJSON('http://api.longurl.org/v2/services?format=json', function(r) {
shortURL = new RegExp('://(' + Object.keys(r).join('|') + ')', 'i');
});
function isShortURL(url) {
return shortURL && url.match(shortURL);
}
function lengthenURL(url, callback) {
if (isShortURL(url))
getJSON('http://api.longurl.org/v2/expand?format=json&url=' + encodeURIComponent(url), callback);
}
function getJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 0))
callback(xhr.responseText && JSON.parse(xhr.responseText));
};
xhr.send();
}
safari.extension.addContentScriptFromURL(safari.extension.baseURI + 'inject.js', ['safari-reader://*/*'], null, true);
safari.extension.addContentStyleSheetFromURL(safari.extension.baseURI + 'style.css', ['safari-reader://*/*'], null, true);
</script>
</head>
<body></body>
</html>