This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.showfavicons.js
138 lines (125 loc) · 5.08 KB
/
jquery.showfavicons.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
/**
* Showfavicons
* A jQuery plugin for displaying a favicon on links.
*
* Version 1.0 - Feb 26th, 2011
*
* Author: Benjamin Boudreau (http://bboudreau.ca/)
* Source: https://github.com/dreur/JQuery-Showfavicons-Plugin
*
* Usage:
* <code>
* To show favicons on external links
* $.showfavicons('external');
* // To show favicons on internal links
* $.showfavicons('internal');
* // To show favicons on both types of links
* $.showfavicons();
* // Ignoring other hostnames on external
* $.showfavicons('external', {hosts : [ 'wiki.bboudreau.ca', 'foo.bboudreau.ca' ] }
* // Adding other hostnames on internal
* $.showfavicons('internal', {hosts : [ 'wiki.bboudreau.ca', 'bar.bboudreau.ca' ] })
* // Ignoring other hostnames on external and setting the default favicon
* $.showfavicons('external', { hosts: [ 'bboudreau.ca' ], defaultFavicon : 'images/external.gif' });
* // Ignoring other hostnames on external and setting the debug flag to see which links are getting a favicon
* $.showfavicons('external', { hosts: [ 'bboudreau.ca' ], debug : true });
* </code>
*
* Options:
* <code>
* defaultFavicon : 'external.gif' // Default favicon when/if getfavicon app is offline.
* hosts : [] // Array containing hostnames to include on internal or to exclude on external.
* debug : false // Boolean to show which links are getting faviconized.
* </code>
*
* TODO:
* - Support defaultImage of getfavicon by Jason Cartwright
* - Fix getfavicon's bug : Sometimes doesn't get the correct favicon when there are multiple favicons per domain (http://twitter.com/mydogminton/status/2374789273)
* - At first guess it seems that it should get the complete link of the icon instead of adding the icon link to the base url.
*
* Inspired by:
* - http://getfavicon.appspot.com/ by Jason Cartwright (https://potato.codebasehq.com/getfavicon/overview)
* - JQuery Faviconize by Samuel Le Morvan (http://www.babylon-design.com/share/faviconize)
*
**/
(function( $ ){
var $defaultIgnoredPatterns= [ "[href^='#']", ":has(img)", "[href^='file://']" ]
var $defaultInternalPatterns= [ "[href^='/']" ]
var $defaultInternalHosts= [ top.location.host.toString() ]
var $defaultSettings = {
'defaultFavicon' : "external.gif",
'hosts' : [],
'debug' : false
};
$.showfavicons = function( method, options ) {
var settings= $.extend( {}, $defaultSettings, options );
var methods = {
internal : function( ) {
var defaultIgnoredPatternString= '';
$($defaultIgnoredPatterns).each(function(index, pattern) {
defaultIgnoredPatternString+= ":not("+pattern+")";
});
var internalHostList= $.merge($.merge([], $defaultInternalHosts), settings.hosts);
var patternString='';
$(internalHostList).each(function(index, host) {
host= $.trim(host);
if (host.length != 0) {
patternString+= "a[href*=\""+ host +"\"]" + defaultIgnoredPatternString;
}
});
return patternString;
},
external : function( ) {
var ignoredHostList= $.merge($.merge([], $defaultInternalHosts), settings.hosts);
var patternString='a';
$(ignoredHostList).each(function(index, host) {
host= $.trim(host);
if (host.length != 0) {
patternString+= ":not([href*=\""+ host +"\"])";
}
});
var ignoredPatternList= $.merge($.merge([], $defaultInternalPatterns), $defaultIgnoredPatterns);
$(ignoredPatternList).each(function(index, pattern) {
patternString+= ":not("+pattern+")";
});
return patternString;
},
all : function( ) {
var defaultIgnoredPatternString= '';
$($defaultIgnoredPatterns).each(function(index, pattern) {
defaultIgnoredPatternString+= ":not("+pattern+")";
});
return "a" + defaultIgnoredPatternString;
}
};
function append(element, url) {
var img = document.createElement("img");
img.className = "showfavicons";
var protocol= (top.location.protocol == 'file:' ? 'http:' : '')
var imgSrc = protocol + "//getfavicon.appspot.com/"+encodeURIComponent(url);
img.setAttribute("src",imgSrc);
img.setAttribute("style","border:0 none;height:12px;width:12px;padding:0 4px;");
img.onerror = function () {
this.src = settings.defaultFavicon;
}
element.after(img);
}
method= $.trim(method)
if (!method || method.length == 0) {
method= "all"
}
if ( methods[method] ) {
var selectors= methods[ method ].apply( this );
if (typeof console !== 'undefined' && settings.debug) {
console.debug( '(jQuery.showfavicons)[DEBUG]{'+method+'} Showing favicons for "' + selectors + '"' );
}
$(selectors).each(function(){
if ($('img', this).length == 0) {
append($(this), this.protocol + '//' + this.hostname);
}
});
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.showfavicons' );
}
};
})( jQuery );