Skip to content

Commit

Permalink
Make this code useful for more than just tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaharagon committed Mar 15, 2024
1 parent 07f9c02 commit 8f7511e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
1 change: 1 addition & 0 deletions config/mkdocs-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extra_css:
- assets/stylesheets/extra.css?v=3.17.0
extra_javascript:
- assets/javascripts/mathjax.js
- assets/javascripts/randomize-element.js

watch:
- ../theme
Expand Down
3 changes: 2 additions & 1 deletion docs/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ title: "DNS Resolvers"
icon: material/dns
description: These are some encrypted DNS providers we recommend switching to, to replace your ISP's default configuration.
cover: dns.webp
randomizeTable: true
global:
- [randomize-element, "table tbody"]
---

Encrypted DNS with third-party servers should only be used to get around basic [DNS blocking](https://en.wikipedia.org/wiki/DNS_blocking) when you can be sure there won't be any consequences. Encrypted DNS will not help you hide any of your browsing activity.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @overview Randomizes the FIRST table on a page.
* @overview Randomizes the order of an HTML element's children.
* @author Jonah Aragon <[email protected]>
* @license
* Copyright (c) 2024 Jonah Aragon
Expand All @@ -23,20 +23,35 @@
* SOFTWARE.
*/

// Create references to table and rows
let table = document.querySelector("table:first-of-type tbody");
let rows = table.querySelectorAll("tr");
let globalData = document.querySelector("#global-data");
if (globalData.dataset.randomizeElement) {
// Get elements to be randomized from meta tag in HTML
let randomizeElement = globalData.dataset.randomizeElement;
console.log("Randomizing child elements of " + randomizeElement)

// Randomize rows (simple, probably not efficient)
let randomRows = Array.prototype.slice.call(rows)
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value)
// Get all elements to be randomized
let randomizeChildren = document.querySelectorAll(randomizeElement);
let randomizeArray = Array.from(randomizeChildren);

// Clear the contents of the table
table.innerHTML = "";
// Handle multiple elements to be randomized
for (const element of randomizeArray) {
// Select all direct children of element
let children = element.querySelectorAll(randomizeElement + " > *");

// Randomize rows (simple, probably not efficient)
let randomChildren = Array.from(children)
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value)

// Clear the contents of the table
element.innerHTML = "";

// Iterate over each row of randomRows
for (const childElement of randomChildren) {
element.appendChild(childElement);
}

}

// Iterate over each row of randomRows
for (const element of randomRows) {
table.appendChild(element);
}
15 changes: 3 additions & 12 deletions theme/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
{% endif %}
<link rel="icon" href="{{ config.theme.favicon | url }}">
<meta name="generator" content="mkdocs-{{ mkdocs_version }}, mkdocs-material-9.1.0+insiders-4.32.2">
{% if page %}
<meta name="global-data" id="global-data" {% for data in page.meta.global %}data-{{ data.0 }}="{{data.1}}"{% endfor %}>
{% endif %}
{% endblock %}
{% block htmltitle %}
{% if page.meta and page.meta.meta_title and config.theme.language == "en" %}
Expand Down Expand Up @@ -157,15 +160,3 @@ <h2>Share this website and spread privacy knowledge</h2>
{% include "partials/feedback.html" %}
{% include "partials/comments.html" %}
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.js' | url }}"></script>

<!-- Custom JavaScript -->
{% for script in config.extra_javascript %}
{{ script | script_tag }}
{% endfor %}

{% if page and page.meta and page.meta.randomizeTable %}
<script src="{{ 'assets/javascripts/randomize-table.js' | url }}"></script>
{% endif %}
{% endblock %}

0 comments on commit 8f7511e

Please sign in to comment.