-
Notifications
You must be signed in to change notification settings - Fork 7
/
favicon.js
102 lines (90 loc) · 3.2 KB
/
favicon.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
/**
* A class for finding a website’s favicon URL, if any. Requires a context, like
* a browser extension, that allows cross-origin requests.
* <br />
* <br />
* Copyright 2012, 2013 Disconnect, Inc.
* <br />
* <br />
* 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 <a
* href="https://mozilla.org/MPL/2.0/">https://mozilla.org/MPL/2.0/</a>.
* <br />
* @constructor
* @param {string} [alt] A default favicon URL, absolute or relative.
* @author <a href="https://github.com/byoogle">Brian Kennish</a>
*/
function Favicon(alt) {
/**
* Fetches the default favicon URL.
* @return {string} An absolute or relative URL.
*/
this.getAlt = function() { return alt; };
/**
* Mungs the default favicon URL.
* @param {string} alt An absolute or relative URL.
* @return {Favicon} The favicon object.
*/
this.setAlt = function(newAlt) {
alt = newAlt;
return this;
};
/**
* Finds a favicon URL.
* @param {string} url A website’s absolute URL or hostname.
* @param {function(string)} callback A continuation, to execute when the
* method completes, that takes a favicon
* URL.
* @return {Favicon} The favicon object.
*/
this.get = function(url, callback) {
var favicon = this.getAlt();
if (typeof favicon != undeclared) callback(favicon);
var id = setInterval(function() {
if (typeof jQuery != undeclared) {
clearInterval(id);
if (url.indexOf('/') + 1) {
anchor.href = url;
url = anchor.hostname;
}
var domain = url.slice(url.indexOf('.') + 1);
var successful;
for (var i = 0; i < protocolCount; i++) {
for (var j = -1; j < subdomainCount; j++) {
for (var k = 0; k < pathCount; k++) {
favicon =
protocols[i] + (j + 1 ? subdomains[j] + domain : url) +
paths[k];
jQuery.get(favicon, function(data, status, xhr) {
var type = xhr.getResponseHeader('Content-Type');
if (!successful && type && type.indexOf('image/') + 1 && data) {
successful = true;
callback(favicon);
}
}).bind(favicon);
}
}
}
}
}, 100);
return this;
};
var version = '1.3.0';
var protocols = ['http://'];
var subdomains = ['', 'www.'];
var paths = ['/favicon.ico'];
var protocolCount = protocols.length;
var subdomainCount = subdomains.length;
var pathCount = paths.length;
var anchor = document.createElement('a');
var undeclared = 'undefined';
if (typeof jQuery == undeclared) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'vendor/jquery.js');
script.onload = function() { jQuery.noConflict(); };
document.head.appendChild(script);
}
return this;
}