Skip to content

Commit 8dc66f7

Browse files
author
Oron Port
committed
implement simulateClick function to trigger click events without altering the view
1 parent 0cb6eb3 commit 8dc66f7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

docs/javascripts/scastie.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ function updateScastieTheme() {
3737
scastie.Embedded('.scastie', settings);
3838
}
3939

40+
//simulate click event without changing the view
41+
function simulateClick(element) {
42+
const x = window.scrollX;
43+
const y = window.scrollY;
44+
var event = new MouseEvent('click', {
45+
view: window,
46+
bubbles: true,
47+
cancelable: true
48+
});
49+
element.dispatchEvent(event);
50+
window.scrollTo(x, y);
51+
}
52+
4053
function updateCodeBlocksAndDetails(preferredLanguage) {
4154
var codeBlocks = document.querySelectorAll('.scastie'); // Assuming all targeted code blocks have this class
4255
codeBlocks.forEach(function(codeBlock) {
@@ -67,7 +80,7 @@ function updateCodeBlocksAndDetails(preferredLanguage) {
6780
);
6881
tabLabels.forEach(label => {
6982
if (label.textContent.includes("Verilog")) {
70-
label.click();
83+
simulateClick(label);
7184
}
7285
});
7386
} else { // VHDL
@@ -85,7 +98,7 @@ function updateCodeBlocksAndDetails(preferredLanguage) {
8598
);
8699
tabLabels.forEach(label => {
87100
if (label.textContent.includes("VHDL")) {
88-
label.click();
101+
simulateClick(label);
89102
}
90103
});
91104
}
@@ -99,7 +112,7 @@ window.addEventListener("load", () => {
99112
);
100113
tabLabels.forEach(label => {
101114
if (label.textContent.includes(savedLanguage)) {
102-
label.click();
115+
simulateClick(label);
103116
}
104117
});
105118
});

0 commit comments

Comments
 (0)