-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (76 loc) · 2.27 KB
/
Copy pathindex.js
File metadata and controls
90 lines (76 loc) · 2.27 KB
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
const body = document.body;
const messageEl = document.createElement('div');
const message = [];
const textElMaker = (text, type) => {
type = type || 'p';
const el = document.createElement(type);
el.innerHTML = text;
return el;
};
const pMaker = (text) => {
const el = textElMaker(text);
el.style.fontSize = '0.88rem';
return el;
};
const hedMaker = (text) => {
const el = textElMaker(text, 'h2');
el.style.fontSize = '1.6rem';
el.style.margin = '2rem 0 1rem 0';
return el;
};
const imageMaker = (url) => {
const el = document.createElement('img');
el.src = url;
el.style.display = 'block';
el.style.width = '300px';
el.style.margin = '0.5rem 0';
return el;
};
const reduceFraction = (numerator,denominator) =>{
var gcd = function gcd(a,b){
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}
// Google compatible images
const structuredData = [...document.querySelectorAll('script[type="application/ld+json"]')].map((el) => JSON.parse(el.text));
const articles = structuredData.filter((d) => d['@type'] === 'NewsArticle' );
if(!!articles.length && !!articles[0].image && !!articles[0].image.length){
message.push(hedMaker('LD+JSON/Google Search'));
articles.forEach((article) => {
const images = article.image;
images.forEach((img) => {
const {url, width, height} = img;
message.push(imageMaker(url));
const reduced = reduceFraction(width, height);
message.push(pMaker(`${width}px x ${height}px (${reduced[0]}x${reduced[1]}) image`));
});
});
}
[{
name: 'Facebook Meta',
selector: 'meta[property="og:image"]'
}, {
name: 'Twitter Meta',
selector: 'meta[property="twitter:image"]'
}].forEach((platform) => {
const metaEl = document.querySelector(platform.selector);
if(metaEl){
const url = metaEl.getAttribute('content');
message.push(hedMaker(platform.name));
message.push(imageMaker(url));
}
});
message.forEach((el) => {
messageEl.append(el);
});
body.appendChild(messageEl);
messageEl.style.zIndex = 2147483647;
messageEl.style.position = "fixed";
messageEl.style.top = "0";
messageEl.style.right = "0";
messageEl.style.fontFamily = "Arial, sans-serif";
messageEl.style.backgroundColor = "#f4ff60";
messageEl.style.color = '#000000';
messageEl.style.padding = "20px 20px 20px 20px";