Created
November 25, 2019 16:22
-
-
Save OEvgeny/f081b92ac6e01ba53e61fa3b9fe5981a to your computer and use it in GitHub Desktop.
Bookmarklet to make tables to be todo lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//javascript: | |
(() => { | |
const hash = str => { | |
let hash = 0, | |
chr; | |
for (let i = 0; i < str.length; i++) { | |
chr = str.charCodeAt(i); | |
hash = (hash << 5) - hash + chr; | |
} | |
return btoa(hash.toFixed(0)); | |
}; | |
const key = "____done-work"; | |
const load = () => { | |
let result; | |
try { | |
result = JSON.parse(localStorage.getItem(key)); | |
} catch (e) { | |
console.error(e); | |
} finally { | |
return result || {}; | |
} | |
}; | |
const save = data => { | |
localStorage.setItem(key, JSON.stringify(data)); | |
}; | |
const start = () => { | |
const data = load(); | |
const tables = document.querySelectorAll("table"); | |
for (const table of Array.from(tables)) { | |
const rows = table.querySelectorAll("tr"); | |
for (const row of Array.from(rows)) { | |
const rowKey = hash(row.textContent); | |
const checked = !!data[rowKey]; | |
addCheckbox(row, data, rowKey, checked); | |
} | |
} | |
}; | |
const addCheckbox = (row, data, rowKey, checked) => { | |
const checkbox = document.createElement("input"); | |
checkbox.setAttribute("type", "checkbox"); | |
checkbox.checked = checked; | |
checkbox.addEventListener("change", () => { | |
if (!checkbox.checked) { | |
delete data[rowKey]; | |
} else { | |
data[rowKey] = true; | |
} | |
save(data); | |
}); | |
const td = document.createElement("td"); | |
td.appendChild(checkbox); | |
row.appendChild(td); | |
}; | |
start(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment