Skip to content

Instantly share code, notes, and snippets.

@MathijsNL
Forked from Greenscreener/12to24.user.js
Last active April 1, 2022 19:07
Show Gist options
  • Save MathijsNL/45493b67df6e63ccc7a1e71f437a18d6 to your computer and use it in GitHub Desktop.
Save MathijsNL/45493b67df6e63ccc7a1e71f437a18d6 to your computer and use it in GitHub Desktop.
Convert all elements with 12-hour time to 24-hour automatically (for WhatsApp Web, but could be adapted easily)
// ==UserScript==
// @name WAWeb 24-hourifier
// @version 0.4
// @description Convert all elements with 12-hour time to 24-hour automatically (for WhatsApp Web, but could be adapted easily)
// @author Greenscreener
// @homepage https://grsc.cz/
// @match https://web.whatsapp.com/*
// @grant none
// @downloadURL https://gist.githubusercontent.com/Greenscreener/4422f5be36752bc720cbc7222fb73cdc/raw/12to24.user.js
// @updateURL https://gist.githubusercontent.com/Greenscreener/4422f5be36752bc720cbc7222fb73cdc/raw/12to24.user.js
// ==/UserScript==
function conv12to24(str) {
const re = /^(\d\d?):(\d\d) ([ap])m$/
const hr = parseInt(str.match(re)[1])
const mi = parseInt(str.match(re)[2])
const add = str.match(re)[3] === "p"
return ((hr+12*add)%24).toString().padStart(2,"0")+":"+mi.toString().padStart(2,"0")
}
setInterval(() => {
[
...document.body.getElementsByTagName("span"),
...document.body.getElementsByTagName("div")
].filter(e => e.innerText.match(/^\d\d?:\d\d [ap]m$/))
.forEach(e => e.innerText = conv12to24(e.innerText))
}, 500)
function lastseenconv12to24(str) {
const timepart = str.substring(19)
const convtime = conv12to24(timepart)
return (" last seen today at " + convtime)
}
setInterval(() => {
[
...document.body.getElementsByTagName("span"),
...document.body.getElementsByTagName("div")
].filter(e => e.innerText.match(/^last seen today at \d\d?:\d\d [ap]m$/))
.forEach(e => e.innerText = lastseenconv12to24(e.innerText))
}, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment