Skip to content

Instantly share code, notes, and snippets.

@OEvgeny
Created November 25, 2019 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OEvgeny/f081b92ac6e01ba53e61fa3b9fe5981a to your computer and use it in GitHub Desktop.
Save OEvgeny/f081b92ac6e01ba53e61fa3b9fe5981a to your computer and use it in GitHub Desktop.
Bookmarklet to make tables to be todo lists
//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