-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocial-preview.js
224 lines (194 loc) · 4.06 KB
/
social-preview.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
/**
* Social Preview
*
* This library is meant to dynamically creating previews for social sharing.
*
* @link https://github.com/kktsvetkov/js-social-preview
* @author Kaloyan K. Tsvetkov <[email protected]>
* @license MIT https://opensource.org/licenses/MIT
*/
social_preview = new function()
{
// shortcut for finding the canonical URL
//
function get_canonical_url()
{
var links = document.getElementsByTagName('links');
for (var i = 0; i < links.length; i++)
{
if ('canonical' == links[i].getAttribute('rel'))
{
return links[i].getAttribute('href');
}
}
return '';
}
// shortcut for checking whether to use external source data, or
// to load it from the current page using social_preview.source()
//
function is_empty_source(o)
{
if ('object' != typeof o)
{
return true;
}
for (var i in o )
{
return false;
}
return true;
}
var metas = document.getElementsByTagName('meta');
/**
* Fetch the data for the social preview from this page
*
* @param {string} "from" either empty string, "fb" for reading Open-Graph
* metadata (used by Facebook), or "tw" for reading Twitter Card metadata
* @return {object} object with "title", "description", "url", "site", "img"
*/
this.source = function(from)
{
o = {
"title": "",
"description": "",
"url": "",
"site": "",
"img": ""
}
for (var i = 0; i < metas.length; i++)
{
value = metas[i].getAttribute('content');
prop = metas[i].getAttribute('property')
|| metas[i].getAttribute('name');
if ('fb' == from)
{
switch( prop )
{
case 'og:title':
o.title = value;
break;
case 'og:url':
o.url = value;
break;
case 'og:image':
o.img = value;
break;
case 'og:site_name':
o.site = value;
break;
case 'og:description':
o.description = value;
break;
}
}
if ('tw' == from)
{
switch( prop )
{
case 'twitter:title':
o.title = value;
break;
case 'twitter:url':
case 'og:url':
o.url = o.url || value;
break;
case 'twitter:image':
case 'og:image':
o.img = o.img || value;
break;
case 'twitter:domain':
o.site = value;
break;
case 'twitter:description':
case 'og:description':
o.description = o.description || value;
break;
}
}
switch( prop )
{
case 'description':
o.description = o.description || value;
break;
}
}
// get the document title
//
if (!o.title)
{
o.title = document.title;
}
// get the url from the canonical url
//
if (!o.url)
{
o.url = get_canonical_url();
}
// get the domain name from the url
//
if (!o.site && o.url)
{
o.site = o.url.match(/(?:https?:)?(?:\/\/)?([^\/#\?]+)/i)[1].toLowerCase();
}
return o;
}
/**
* Return the HTML for the social preview
*
* @param {object} "o"
* @param {string} "size" either empty string, "small" or "big"
* @return {string}
*/
this.html = function(o, size)
{
if ('object' != typeof o)
{
o = {}
}
o.title = o.title || '';
o.description = o.description || '';
o.site = o.site || '';
o.url = o.url || '';
o.img = o.img || '';
return '<div class="social-preview-card social-preview-' + size + '">'
+ '<img style="background-image: url(' + o.img + ');" />'
+ '<h1>' + o.title + '</h1>'
+ '<p>' + o.description + '</p>'
+ '<a href="' + o.url + '">'
+ o.site
+ '</a>'
+ '</div>';
}
/**
* Shortcut for rendering a small social preview
*
* @param {object} "o"
* @param {string} "from" either empty string, "fb" or "tw"
* @return {string}
*/
this.small = function(o, from)
{
from = from || 'fb';
if (is_empty_source(o))
{
o = this.source(from);
}
return this.html(o, 'small');
}
/**
* Shortcut for rendering a big social preview
*
* @param {object} "o"
* @param {string} "from" either empty string, "fb" or "tw"
* @return {string}
*/
this.big = function(o, from)
{
from = from || 'tw';
if (is_empty_source(o))
{
o = this.source(from);
}
return this.html(o, 'big');
}
}