-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
125 lines (104 loc) · 4.59 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Kenny Xiao – Software Engineer</title>
<meta name="description" content="Kenny Xiao's personal website, made with gopagify.com.">
<meta name="author" content="Kenny Xiao">
<meta name="keywords" content="kenny,xiao,pagify,gopagify">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="apple-touch-icon" sizes="180x180" href="./favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./favicon/favicon-16x16.png">
<link rel="manifest" href="./site.webmanifest">
<!-- import JQuery -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7SFW8K4TCM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-7SFW8K4TCM');
</script>
</head>
<body style="margin: 0; max-width: initial;">
<script>
function showError(errMsg) {
$("body").append(`<div style='margin: 25px; font-family: Helvetica, Arial, sans-serif'>Failed to retrieve Google Doc: ${errMsg}<br><br>1) Did you follow the instructions and publish your doc to the web?<br>2) Did you copy/paste your URL correctly?</div>`);
};
// Special handling for Safari and Firefox not loading background-color from inline styles
function handleColorBug(pageBody, styles) {
let classes = $(pageBody).attr("class"); // classes of first div
let mainClass = classes.split(" ", 1)[0]; // ex. c10
let inlineCSS = styles.text(); // long string of entire inline css
let classDefinition = `.${mainClass}{`;
let classIndex = inlineCSS.indexOf(classDefinition); // index of c10 in inline css
let bgColorDefinition = "background-color:";
let bgColorIndex = inlineCSS.indexOf(bgColorDefinition, classIndex); // index of the background-color style
let semicolonIndex = inlineCSS.indexOf(";", bgColorIndex); // index of the semicolon after background-color
// Get and set the new color
newBackgroundColor = inlineCSS.substring(bgColorIndex + bgColorDefinition.length, semicolonIndex);
document.body.style.backgroundColor = newBackgroundColor;
}
function loadHTML(url) {
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error("please check you followed the instructions correctly")
};
return response.text();
})
.then(data => {
let parsedData = $.parseHTML(data);
let contents = $(parsedData).filter('#contents');
let styles = $(contents).children().eq(0);
let pageBody = $(contents).children().eq(1);
pageBody.css({"max-width": "800px", "margin": "0 auto"});
$("body").append($(styles));
$("body").append($(pageBody));
let backgroundColor = $(pageBody).css("background-color");
$("body").css("background-color", backgroundColor);
$("title")[0].innerHTML = $(parsedData).filter("title")[0].innerHTML;
if (backgroundColor == "rgba(0, 0, 0, 0)") {
try {
handleColorBug(pageBody, styles);
}
catch (e) {
// Just log the error, the background color resolves on refresh
console.error(e, e.stack);
};
}
})
.catch(error => {
showError(error.message);
})
.finally(() => {
// Now initialize analytics because page title has been updated
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-426Q16RG3H');
});
};
function main() {
// Hardcode for my website
const docId = "1iibdNnyKBFeen301rVw5pIvGn48J_6O6fSkDtmSP6nU";
if (docId == null || docId.length == 0) {
showError("you did not pass in the document url");
return;
};
let url;
// url from popup has 86 characters. Google doc url has 44 characters
if (docId.startsWith("2PACX-") || docId.length > 60) {
url = `https://docs.google.com/document/d/e/${docId}/pub`;
}
else {
url = `https://docs.google.com/document/d/${docId}/pub`;
};
loadHTML(url);
};
main();
</script>
</body>
</html>