-
Notifications
You must be signed in to change notification settings - Fork 15
/
ouiseo.js
461 lines (366 loc) · 14.9 KB
/
ouiseo.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
;(function() {
// The minimum version of jQuery required
var v = '1.10.2';
// Check if jQuery is present and jQuery version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false,
script = document.createElement('script');
script.src = '//ajax.googleapis.com/ajax/libs/jquery/' + v + '/jquery.min.js';
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
initOuiseo();
}
};
console.log('ouiseo loaded jQuery v%s', v);
document.getElementsByTagName('head')[0].appendChild(script);
} else {
initOuiseo();
}
function initOuiseo() {
(window.ouiseo = function() {
// Add ouiseo
$('head').append("<link rel='stylesheet' id='ouiseo-styles' href='//carlsednaoui.s3.amazonaws.com/ouiseo/ouiseo.css'>");
$('body').append(createHTML());
initializeOuiseoHandlers();
$("#ouiseo").fadeIn(250);
$('head').append(addOuiseoGA());
// Remove ouiseo when user clicks outside of frame
$("#ouiseo_frame").click(function(event) {
$("#ouiseo").fadeOut(750);
$("#ouiseo_frame").slideUp(750);
setTimeout("$('#ouiseo_frame').remove()", 750);
setTimeout("$('#ouiseo-styles').remove()", 750);
setTimeout("$('#ouiseo-ga').remove()", 750);
setTimeout("$('#ouiseo').remove()", 750);
});
})();
function addOuiseoGA() {
return "<script id='ouiseo-ga'>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-23209172-7', window.location.hostname);ga('send', 'pageview');</script>";
}
function createHTML() {
var html = '';
html += createOuiseoFrame().outerHTML;
html += createOuiseoBody().outerHTML;
return html;
}
// Create ouiseo_frame -- allows user to close ouiseo
function createOuiseoFrame() {
var frame = document.createElement('div');
frame.id = 'ouiseo_frame';
return frame;
}
// Call ouiseo html element creation functions
function createOuiseoBody() {
var container = createOuiseoContainer();
var basicSection = createBasicSEOSection();
basicSection.appendChild(getTitle());
basicSection.appendChild(getDescription());
basicSection.appendChild(getKeywords());
basicSection.appendChild(getImages());
basicSection.appendChild(getLinks());
basicSection.appendChild(getHeaders());
basicSection.appendChild(getCanonical());
basicSection.appendChild(getCookie());
container.appendChild(basicSection);
var socialSection = createSocialSection();
socialSection.appendChild(getFacebook());
socialSection.appendChild(getTwitter());
container.appendChild(socialSection);
return container;
}
function createOuiseoContainer() {
var container = document.createElement('div'),
author = document.createElement('h1');
container.id = 'ouiseo';
container.className = 'ouiseo';
author.id = 'ouiseo-author';
author.innerHTML = 'Made by ';
author.innerHTML += '<a href="http://www.carlsednaoui.com?utm_campaign=ouiseo&utm_medium=widget-link&utm_source=ouiseo-browser-extension" target="_blank">Carl Sednaoui</a>';
author.innerHTML += ' - Open sourced on ';
author.innerHTML += '<a href="https://github.com/carlsednaoui/ouiseo" target="_blank">Github</a>';
container.appendChild(author);
return container;
}
////////////////////////////////
// Create Basic SEO Section
////////////////////////////////
function createBasicSEOSection() {
var div = document.createElement('div');
div.id = 'ouiseo-basic-seo';
var title = document.createElement('h2');
title.innerHTML = 'SEO';
div.appendChild(title);
return div;
}
function getTitle() {
var el = document.createElement('p'),
span = document.createElement('span'),
input = document.createElement('input');
el.className = 'ouiseo-basic-result';
span.id = 'ouiseo-title-length';
input.type = 'text';
input.id = 'ouiseo-title';
input.className = 'ouiseo-input-text';
var title = $('title').text() || '';
var titleLen = 0;
if ( !! title)
titleLen = title.replace(/\n$/, '').length; // Remove last line return, if present
span.innerHTML = titleLen;
input.setAttribute('value', title); // Need to use setAttribute here so that value gets pased when appending child
el.innerHTML = 'Title (';
el.appendChild(span);
el.innerHTML += ')';
el.appendChild(input);
return el;
}
function getDescription() {
var el = document.createElement('p'),
span = document.createElement('span'),
input = document.createElement('input');
el.className = 'ouiseo-basic-result';
span.id = 'ouiseo-description-length';
input.type = 'text';
input.id = 'ouiseo-description';
input.className = 'ouiseo-input-text';
var metaDescription = $('meta[name=description]').attr('content') || '';
var metaDescriptionLen = 0;
if ( !! metaDescription)
metaDescriptionLen = metaDescription.replace(/\n$/, '').length; // Remove last line return, if present
span.innerHTML = metaDescriptionLen;
input.setAttribute('value', metaDescription); // Need to use setAttribute here so that value gets pased when appending child
el.innerHTML = 'Meta Description (';
el.appendChild(span);
el.innerHTML += ')';
el.appendChild(input);
return el;
}
function getKeywords() {
var el = document.createElement('p'),
span = document.createElement('span'),
input = document.createElement('input');
el.className = 'ouiseo-basic-result';
span.id = 'ouiseo-keywords-length';
input.type = 'text';
input.id = 'ouiseo-keywords';
input.className = 'ouiseo-input-text';
var metaKeywords = $('meta[name=keywords]').attr('content') || '';
var metaKeywordsLen = 0;
if (metaKeywords !== '')
metaKeywordsLen = metaKeywords.split(',').length;
span.innerHTML = metaKeywordsLen;
input.setAttribute('value', metaKeywords); // Need to use setAttribute here so that value gets pased when appending child
el.innerHTML = 'Meta Keywords (';
el.appendChild(span);
el.innerHTML += ')';
el.appendChild(input);
return el;
}
function calculateImageAltTextCount() {
var imgAltCount = 0;
$.each($('img'), function(index) {
if ($('img')[index].alt) imgAltCount++;
});
return imgAltCount == [] ? 0 : imgAltCount;
}
function getImages() {
var el = document.createElement('p');
el.className = 'ouiseo-basic-result';
var imgCount = $('img').length,
imgAltCount = calculateImageAltTextCount();
el.innerHTML = 'Images with alt text: ';
el.innerHTML += imgAltCount;
el.innerHTML += ' out of ';
el.innerHTML += imgCount;
return el;
}
function calculateLinksWithTitleCount() {
var linkTitleCount = 0;
$.each($('a'), function(index) {
if ($('a')[index].title !== '') linkTitleCount++;
});
return linkTitleCount == [] ? 0 : linkTitleCount;
}
function calculateNoFollowLinkCount() {
var noFollowedLinkCount = 0;
$.each($('a'), function(index) {
if ($('a')[index].rel == 'nofollow') noFollowedLinkCount++;
});
return noFollowedLinkCount;
}
function getLinks() {
var el = document.createElement('p');
el.className = 'ouiseo-basic-result';
var linkCount = $('a').length,
linkWithTitleCount = calculateLinksWithTitleCount(),
noFollowLinkCount = calculateNoFollowLinkCount();
el.innerHTML = 'Links with title set: ';
el.innerHTML += linkWithTitleCount;
el.innerHTML += ' out of ';
el.innerHTML += linkCount;
el.innerHTML += ' links. This page has ';
el.innerHTML += noFollowLinkCount;
el.innerHTML += ' nofollow links.';
return el;
}
function getHeaders() {
var el = document.createElement('p');
el.className = 'ouiseo-basic-result';
var hOneCount = $('h1').length || 0,
hTwoCount = $('h2').length || 0,
hThreeCount = $('h3').length || 0;
el.innerHTML = 'H1: ';
el.innerHTML += hOneCount;
el.innerHTML += "<br> H2: ";
el.innerHTML += hTwoCount;
el.innerHTML += "<br> H3: ";
el.innerHTML += hThreeCount;
return el;
}
function getCanonical() {
var el = document.createElement('p');
el.className = 'ouiseo-basic-result';
var canonicalCount = $("link[rel='canonical']").length || 0;
el.innerHTML = 'Rel Canonical (';
el.innerHTML += canonicalCount;
el.innerHTML += ')';
if (canonicalCount !== 0) {
$.each($("link[rel='canonical']"), function(index) {
el.innerHTML += '<br>';
el.innerHTML += $("link[rel='canonical']")[index].href;
});
}
return el;
}
function getCookie() {
var el = document.createElement('p'),
span = document.createElement('span'),
input = document.createElement('input');
el.className = 'ouiseo-basic-result';
span.id = 'ouiseo-cookie-count';
input.type = 'text';
input.id = 'ouiseo-cookie';
input.className = 'ouiseo-input-text';
var cookie = document.cookie;
input.setAttribute('value', cookie); // Need to use setAttribute here so that value gets pased when appending child
span.innerHTML = '0';
if (cookie !== '')
span.innerHTML = cookie.split(';').length;
el.innerHTML = 'Site cookie: (';
el.appendChild(span);
el.innerHTML += ')';
el.appendChild(input);
return el;
}
////////////////////////////////
// Create Social Section
////////////////////////////////
function createSocialSection() {
var div = document.createElement('div');
div.id = 'ouiseo-social-section';
return div;
}
//////////////////////////////
// Get Facebook Data
//////////////////////////////
function getFacebook() {
var result = document.createElement('div');
title = document.createElement('h2');
result.id = 'ouiseo-facebook';
title.innerHTML = 'Facebook';
result.appendChild(title);
result.appendChild(createElWithSelector('FB App Id', 'fb-app-id', 'meta[property="fb:app_id"]'));
result.appendChild(createElWithSelector('OG URL', 'fb-url', 'meta[property="og:url"]'));
result.appendChild(createElWithSelector('OG Site Name', 'fb-site-name', 'meta[property="og:site_name"]'));
result.appendChild(createElWithSelector('OG Title', 'fb-title', 'meta[property="og:title"]'));
result.appendChild(createElWithSelector('OG Description', 'fb-description', 'meta[property="og:description"]'));
result.appendChild(createElWithSelector('OG Type', 'fb-type', 'meta[property="og:type"]'));
result.appendChild(createImageEl('OG Image', 'meta[property="og:image"]'));
return result;
}
//////////////////////////////
// Get Twitter Data
//////////////////////////////
function getTwitter() {
var result = document.createElement('div');
title = document.createElement('h2');
result.id = 'ouiseo-twitter';
title.innerHTML = 'Twitter';
result.appendChild(title);
result.appendChild(createElWithSelector('Card type', 'twitter-card-type', 'meta[name="twitter:card"]'));
result.appendChild(createElWithAlternateSelector('Site', 'twitter-site', 'meta[name="twitter:site"]', 'meta[name="twitter:site:id"]'));
result.appendChild(createElWithAlternateSelector('Creator', 'twitter-creator', 'meta[name="twitter:creator"]', 'meta[name="twitter:creator:id"]'));
result.appendChild(createElWithSelector('Card title', 'twitter-card-title', 'meta[name="twitter:title"]'));
result.appendChild(createElWithSelector('Card description', 'twitter-card-description','meta[name="twitter:description"]'));
result.appendChild(createImageEl('Image', 'meta[name="twitter:image:src"]'));
return result;
}
function createElWithAlternateSelector(name, title, first_selector, secondary_selector) {
var el = document.createElement('p'),
input = document.createElement('input'),
result;
el.className = 'ouiseo-social-result';
input.type = 'text';
input.id = 'ouiseo-' + title;
input.className = 'ouiseo-input-text';
el.innerHTML = name;
if ($(first_selector)[0]) {
result = $(first_selector)[0].content;
} else if ($(secondary_selector)[0]) {
el.innerHTML += ' ID';
result = $(secondary_selector)[0].content;
} else {
result = '';
}
input.setAttribute('value', result);
el.appendChild(input);
return el;
}
function createElWithSelector(name, title, selector) {
var el = document.createElement('p'),
input = document.createElement('input'),
result;
el.className = 'ouiseo-social-result';
input.type = 'text';
input.id = 'ouiseo-' + title;
input.className = 'ouiseo-input-text';
result = $(selector)[0] ? $(selector)[0].content : '';
el.innerHTML = name;
input.setAttribute('value', result);
el.appendChild(input);
return el;
}
function createImageEl(name, selector) {
var el = document.createElement('p');
el.className = 'ouiseo-social-result';
el.innerHTML = name;
if ($(selector)[0]) {
el.appendChild(document.createElement('div')); // To show images below title
var img = document.createElement('img');
img.src = $(selector)[0].content;
img.className = 'ouiseo-social-image';
el.appendChild(img);
} else {
el.innerHTML += ': none';
}
return el;
}
//////////////////////////////////////////
// Initialize Handlers To Update Values
//////////////////////////////////////////
function initializeOuiseoHandlers() {
function calculateCharLen(selector) {
return $(selector).val().length;
}
$('#ouiseo-title').keyup(function() {
$('#ouiseo-title-length').text(calculateCharLen('#ouiseo-title'));
});
$('#ouiseo-description').keyup(function() {
$('#ouiseo-description-length').text(calculateCharLen('#ouiseo-description'));
});
$('#ouiseo-keywords').keyup(function() {
$('#ouiseo-keywords-length').text($('#ouiseo-keywords').val().split(',').length);
});
}
}
})();