Skip to content

Instantly share code, notes, and snippets.

@tomodutch
Last active August 15, 2022 18:15
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 tomodutch/f3a58c9e10ad01be101d5652b4c11ff0 to your computer and use it in GitHub Desktop.
Save tomodutch/f3a58c9e10ad01be101d5652b4c11ff0 to your computer and use it in GitHub Desktop.
Store yomichan lookups in localstorage

This script is designed to be used with yomichan's clipboard inserter and stores the frequencies of performed lookups in JSON format.

{
  "version": "0.1-alpha",
  "terms": {
    "母娘": { "frequency": 1 },
    "関与": { "frequency": 1 },
    "少年": { "frequency": 3 }
  }
}

A hacky workaround for: FooSoft/yomichan#93

How to use

  1. enable yomichan's clipboard monitor
  2. open devtools (inspect element) inside the clipboard monitor window
  3. paste the yomichan-store-lookups.js script in the console

How to export the data

  1. open devtools (inspect element) inside the clipboard monitor window
  2. open the "Application" tab
  3. expand the Storage > Local Storage list item
  4. copy the content of the Value field where the key is equal to yomichan-lookups
  5. perform your own analytics based on the json data

Disclaimer

This script may be modified at any moment without guarantees and it may break. Use with caution.

(function () {
const localStorageKey = "yomichan-lookups";
document.addEventListener("selectionchange", debounce(handleClick));
function handleClick() {
const selectedText = window.getSelection().toString();
if (selectedText?.trim()?.length >= 1) {
upsertRecord(selectedText);
}
}
function upsertRecord(term) {
let db =
JSON.parse(window.localStorage.getItem(localStorageKey)) ?? createDb();
let affectedRecord = db["terms"][term] ?? { frequency: 0 };
affectedRecord["frequency"]++;
db["terms"][term] = affectedRecord;
window.localStorage.setItem(localStorageKey, JSON.stringify(db));
}
function createDb() {
return {
version: "0.1-alpha",
terms: {},
};
}
function debounce(f, timeout) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => f.apply(this, args), timeout);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment