Skip to content

Commit bc35742

Browse files
Alex ChristiansenAlex Christiansen
Alex Christiansen
authored and
Alex Christiansen
committed
Updated the Vanilla JS extension example to work by removing onclick in HTML and adding an eventlistener to handle counter. The onclick within the HTML was causing a CSP violation and wasn't working.
1 parent 05b4e30 commit bc35742

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

vanilla/counter/src/index.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
import {
2626
connectExtensionHost,
2727
LookerExtensionSDK40,
28-
} from '@looker/extension-sdk'
28+
} from '@looker/extension-sdk';
2929

30-
;(async () => {
30+
(async () => {
3131
const extensionSdk = await connectExtensionHost()
3232
const sdk40 = LookerExtensionSDK40.createClient(extensionSdk)
3333
const result = await sdk40.me()
3434
const name = result.ok ? result.value.display_name : 'Unknown'
35+
36+
// Write the HTML content
3537
document.write(`
3638
<style>
3739
body {
@@ -59,10 +61,32 @@ import {
5961
<h1>Looker Counter Extension</h1>
6062
<h2>Welcome ${name}</h2>
6163
<h3>This number will increase by one upon every click:</h3>
62-
<div class="butt" onclick="event.target.innerHTML = +event.target.innerHTML + (event.shiftKey ? -1 : 1); event.preventDefault
63-
()">0</div>
64+
<div class="butt" id="counterButton">0</div>
6465
<h3>I hope you had fun with this Looker extension.</h3>
6566
<img width="200" src="https://docs.looker.com/assets/site_images/looker-logo.svg" />
6667
</div>
67-
`)
68+
`);
69+
70+
// Set up the click handler directly (without waiting for DOMContentLoaded)
71+
const counterButton = document.getElementById('counterButton');
72+
73+
if (!counterButton) {
74+
console.error('Counter button not found!')
75+
return
76+
} else {
77+
console.log('Counter button found:', counterButton)
78+
}
79+
80+
counterButton.addEventListener('click', (event) => {
81+
console.log('Button clicked!')
82+
console.log('Current counter value:', counterButton.innerHTML)
83+
84+
// Update the counter value
85+
counterButton.innerHTML = +counterButton.innerHTML + (event.shiftKey ? -1 : 1)
86+
87+
console.log('Updated counter value:', counterButton.innerHTML)
88+
event.preventDefault()
89+
});
90+
91+
6892
})()

0 commit comments

Comments
 (0)