Skip to content

Commit deb8223

Browse files
committed
feat: add ability to trigger script
1 parent fdf82b6 commit deb8223

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

dist/worker.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pages/home.html

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
button {
3535
padding: 15px;
3636
font-size: 20px;
37-
margin-top: 15px;
37+
margin: 15px 0px;
3838
background-color: black;
3939
color: white;
4040
border: none;
@@ -45,6 +45,12 @@
4545
opacity: 0.8;
4646
cursor: pointer;
4747
}
48+
.success {
49+
color: green;
50+
}
51+
.failure {
52+
color: red;
53+
}
4854
</style>
4955
</head>
5056
<body>
@@ -54,7 +60,10 @@
5460

5561
To manually test the script, click this button.
5662
<br />
57-
<button>☁️ Trigger Export</button>
63+
<div class="actions">
64+
<button id="trigger">☁️ Trigger Export</button>
65+
<div class="responses"></div>
66+
</div>
5867
<p>
5968
in order for the script to execute on a time-based, you must create
6069
<br />a cron job task for it, Navigate to
@@ -78,5 +87,26 @@
7887
</ul>
7988
</div>
8089
</div>
90+
<script>
91+
window.onload = () => {
92+
const btn = document.querySelector('#trigger')
93+
const triggerScript = async () => {
94+
const responses = document.querySelector('.responses')
95+
responses.innerHTML = ''
96+
const span = document.createElement('SPAN')
97+
const res = await fetch('/test')
98+
if (res.ok) {
99+
span.classList.add('success')
100+
span.innerText = 'Success! started exporting your workspaces...'
101+
} else {
102+
span.classList.add('failure')
103+
span.innerText =
104+
'Unable to start the script, please activate the test mode.'
105+
}
106+
responses.appendChild(span)
107+
}
108+
btn.addEventListener('click', triggerScript)
109+
}
110+
</script>
81111
</body>
82112
</html>

src/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import home from '../pages/home.html'
33
import error from '../pages/error.html'
44

55
addEventListener('fetch', event => {
6-
event.respondWith(handleRequest(event.request))
6+
event.respondWith(handleRequest(event))
77
})
88
addEventListener('scheduled', event => {
99
event.waitUntil(triggerExport())
1010
})
1111

12-
async function handleRequest(request) {
13-
const { pathname } = new URL(request.url)
12+
async function handleRequest(event) {
13+
const { pathname } = new URL(event.request.url)
1414

1515
if (typeof TOKEN_V2 === 'undefined') {
1616
return new Response(error, {
@@ -20,8 +20,18 @@ async function handleRequest(request) {
2020
})
2121
}
2222

23-
if (pathname === '/test' && typeof MODE !== 'undefined' && MODE == 'test')
24-
await triggerExport()
23+
if (pathname === '/test') {
24+
if (typeof MODE !== 'undefined' && MODE == 'test') {
25+
event.waitUntil(triggerExport())
26+
return new Response('OK', { status: 200 })
27+
} else {
28+
return new Response(
29+
'Failed to trigger the script, please enable test mode',
30+
{ status: 400 },
31+
)
32+
}
33+
}
34+
2535
return new Response(home, {
2636
headers: {
2737
'content-type': 'text/html',

0 commit comments

Comments
 (0)