Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (78 loc) · 2.4 KB
/
Copy pathindex.js
File metadata and controls
83 lines (78 loc) · 2.4 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
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
import auto from '@greenweb/gaw-plugin-cloudflare-workers';
// Spammy paths that I don't want to serve the content for.
const spammyPaths = [
'/wp-includes/',
'/wp-content/',
'/wp-admin/',
'/contact?',
'.php?',
'/ueditor/',
'/Ueditor/',
'/php-cgi/',
'/wp-json/',
'.php',
'.php7',
'/.git',
];
// This is the main function that fetches the user's location, fetches the grid data, and determines what page (regular or grid-aware) to return the to the user.
export default {
async fetch(request, env, ctx) {
// EMAPS MULTI ZONES
// Australia, Brazil, Canada, Denmark, Spain, Faroe Islands, France, Great Britain, India, Italy, Japan, Norway, Philippines, Portugal, Russia, Sweden, USA
const eMapsMultiZones = ['AU', 'BR', 'CA', 'DK', 'ES', 'FO', 'FR', 'GB', 'IN', 'IT', 'JP', 'NO', 'PH', 'PT', 'RU', 'SE', 'US'];
const country = request.cf.country;
// Check if the request URL contains any of the spammy paths & return a 404 response.
if (spammyPaths.some((path) => request.url.includes(path))) {
return new Response(null, { status: 404 });
}
return auto(request, env, ctx, {
debug: 'headers',
gawDataApiKey: env.EMAPS_API_KEY,
locationType: eMapsMultiZones.includes(country) ? 'latlon' : 'country',
kvCacheData: true,
kvCachePage: false,
ignoreRoutes: ['/og'],
defaultView: 'low',
infoBar: {
target: '#gaw-bar-holder',
learnMoreLink: 'https://fershad.com/grid-aware-site',
},
htmlChanges: {
low: new HTMLRewriter(),
moderate: new HTMLRewriter()
.on('body', {
element(element) {
element.setAttribute('class', 'deglitch');
element.setAttribute('data-gaw-level', 'moderate');
},
})
.on('#glitch-toggle', {
element(element) {
element.removeAttribute('checked');
},
}),
high: new HTMLRewriter()
.on('body', {
element(element) {
element.setAttribute('class', 'deglitch');
element.setAttribute('data-gaw-level', 'high');
},
})
.on('[data-gaw-remove]', {
element(element) {
element.remove();
},
}),
},
});
},
};