-
Notifications
You must be signed in to change notification settings - Fork 0
/
installpwa.js
159 lines (135 loc) · 4.56 KB
/
installpwa.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
// detect html load
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function createBlobImageFromUrl(imageUrl, newWidth, newHeight) {
const img = new Image();
img.crossOrigin = 'anonymous'; // Enable CORS for external images
img.src = imageUrl;
img.onload = function() {
const canvas = document.createElement('canvas');
canvas.width = newWidth;
canvas.height = newHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, newWidth, newHeight);
canvas.toBlob(function(blob) {
// Use the blob as needed (e.g., upload to a server)
console.log('Blob image created successfully!');
}, 'image/jpeg', 0.9);
};
}
// get icon for the install app and manfest
function getIcons() {
var links = document.getElementsByTagName('link');
var icons = [];
for(var i = 0; i < links.length; i++) {
var link = links[i];
//Technically it could be null / undefined if someone didn't set it!
//People do weird things when building pages!
var rel = link.getAttribute('rel');
if(rel) {
//I don't know why people don't use indexOf more often
//It is faster than regex for simple stuff like this
//Lowercase comparison for safety
if(rel.toLowerCase().indexOf('icon') > -1) {
var href = link.getAttribute('href');
//Make sure href is not null / undefined
if(href) {
//Relative
//Lowercase comparison in case some idiot decides to put the
//https or http in caps
//Also check for absolute url with no protocol
if(href.toLowerCase().indexOf('https:') == -1 && href.toLowerCase().indexOf('http:') == -1
&& href.indexOf('//') != 0) {
//This is of course assuming the script is executing in the browser
//Node.js is a different story! As I would be using cheerio.js for parsing the html instead of document.
//Also you would use the response.headers object for Node.js below.
var absoluteHref = window.location.protocol + '//' + window.location.host;
if(window.location.port) {
absoluteHref += ':' + window.location.port;
}
//We already have a forward slash
//On the front of the href
if(href.indexOf('/') == 0) {
absoluteHref += href;
}
//We don't have a forward slash
//It is really relative!
else {
var path = window.location.pathname.split('/');
path.pop();
var finalPath = path.join('/');
absoluteHref += finalPath + '/' + href;
}
icons.push(absoluteHref);
}
//Absolute url with no protocol
else if(href.indexOf('//') == 0) {
var absoluteUrl = window.location.protocol + href;
icons.push(absoluteUrl);
}
//Absolute
else {
icons.push(href);
}
}
}
}
}
return icons;
}
icons = getIcons()
// create blob url
var manifest = `
{
"name":"${document.title.replaceAll('"','').replaceAll("'","")}",
"short_name":"${document.title.replaceAll('"','').replaceAll("'","")}",
"start_url":"${location.pathname.replace('/','')}/",
"display":"standalone",
"background_color":"#5900b3",
"theme_color":"black",
"scope": ".",
"display": "standalone",
"description":"INstalled with geoloup/PWAfromconsole",
"icons":[
{
"src":"${createBlobImageFromUrl(icons[0], 192, 192)}",
"sizes":"192x192",
"type":"image/png"
},
{
"src":"${createBlobImageFromUrl(icons[0], 512, 512)}",
"sizes":"512x512",
"type":"image/png"
}
]
}
`;// Example usage:
// Example usage:
console.log(manifest)
console.log(icons)
var blob = new Blob([manifest]);
var murl = URL.createObjectURL(blob);
// create manifest to the html
const link = document.createElement('link');
link.rel = 'manifest';
link.href = murl /* custom blob */;
link.id = 'pwamanifestbygeoloupteam'
document.head.appendChild(link)
waitForElm('pwamanifestbygeoloupteam').then((elm) => {
console.log('[pwa installer] Ready click on the icon on top right of the screen and say yes to install the app')
});