1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-08-08 16:26:35 +02:00

Integrate GA4 code directly with book.js (#470)

* Integrate GA4 code directly with `book.js`

The main advantage of this is that it simplifies the setup since we
can avoid the monkey-patching we did before.

A secondary advantage is that it should make things a little faster
since we avoid a request to the server on every page load.

* Remove unreachable return

* Watch all of `third_party`

It just occurred to me that we want to refresh the page in `mdbook serve` when anything changes in `third_party`.
This commit is contained in:
Martin Geisler
2023-03-02 18:50:37 +01:00
committed by GitHub
parent e1546235c8
commit 449ead5575
3 changed files with 30 additions and 75 deletions

View File

@ -3,6 +3,16 @@
// Fix back button cache problem
window.onunload = function () { };
function isPlaygroundModified(playground) {
let code_block = playground.querySelector("code");
if (window.ace && code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
return editor.getValue() != editor.originalCode;
} else {
return false;
}
}
// Global variable, shared between modules
function playground_text(playground, hidden = true) {
let code_block = playground.querySelector("code");
@ -129,6 +139,8 @@ function playground_text(playground, hidden = true) {
result_block.innerText = "Running...";
const playgroundModified = isPlaygroundModified(code_block);
const startTime = window.performance.now();
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
headers: {
'Content-Type': "application/json",
@ -139,6 +151,13 @@ function playground_text(playground, hidden = true) {
})
.then(response => response.json())
.then(response => {
const endTime = window.performance.now();
gtag("event", "playground", {
"modified": playgroundModified,
"error": (response.error == null) ? null : 'compilation_error',
"latency": (endTime - startTime) / 1000,
});
if (response.result.trim() === '') {
result_block.innerText = "No output";
result_block.classList.add("result-no-output");
@ -147,7 +166,15 @@ function playground_text(playground, hidden = true) {
result_block.classList.remove("result-no-output");
}
})
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
.catch(error => {
const endTime = window.performance.now();
gtag("event", "playground", {
"modified": playgroundModified,
"error": error.message,
"latency": (endTime - startTime) / 1000,
});
result_block.innerText = "Playground Communication: " + error.message
});
}
// Syntax highlighting Configuration