Skip to content

Commit 803af44

Browse files
committedMar 11, 2024
add browser demo
1 parent e4f1830 commit 803af44

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
config/local.js
33
assets
4+
pnpm-lock.yaml
5+
browser-demo/wikibase-edit.js
46

‎browser-demo/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# wikibase-edit browser demo
2+
3+
To run wikibase-edit in the browser, you need a bundler. In this demo we use `esbuild` (see [`build.sh`](./build.sh)).
4+
```sh
5+
cd browser-demo
6+
./build.sh
7+
```
8+
9+
You can then use a file server to serve the files in this directory and test wikibase-edit in the browser. Note that the edit will only work if the page is served under HTTPS.

‎browser-demo/build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
esbuild ../lib/index.js --bundle --outfile=./wikibase-edit.js --format=esm

‎browser-demo/index.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Test wikibase-edit in the browser</title>
6+
<script type="module" src="./main.js" async></script>
7+
<style>
8+
input, button{
9+
padding: 0.5em;
10+
margin: 0.5em 0;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<h1>Test wikibase-edit in the browser</h1>
16+
<label>
17+
Add an English Alias to <a href="https://www.wikidata.org/entity/Q112795079">Wikidata Sandbox 4 : Q112795079</a>
18+
<br>
19+
<input id="text" type="text" placeholder="foo" />
20+
</label>
21+
<button>Add</button>
22+
<pre id="response"></pre>
23+
</body>
24+
</html>

‎browser-demo/main.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import WbEdit from './wikibase-edit.js'
2+
3+
console.log('hillo')
4+
5+
const wbEdit = WbEdit({
6+
instance: 'https://www.wikidata.org',
7+
credentials: {
8+
browserSession: true,
9+
},
10+
})
11+
12+
document.addEventListener('click', async e => {
13+
if (e.target.tagName === 'BUTTON' && document.getElementById('text').value) {
14+
try {
15+
const res = await wbEdit.alias.add({
16+
id: 'Q112795079',
17+
language: 'fr',
18+
value: document.getElementById('text').value,
19+
})
20+
document.getElementById('response').innerText = JSON.stringify(res, null, 2)
21+
} catch (err) {
22+
document.getElementById('response').innerText = err.stack || err.toString()
23+
}
24+
}
25+
})

‎browser-demo/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "browser-demo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "main.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"esbuild": "^0.20.1"
14+
}
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.