Skip to content

Instantly share code, notes, and snippets.

@salty-horse
Last active April 1, 2025 05:50
Show Gist options
  • Select an option

  • Save salty-horse/37e9c7706c4e82995febcac146c0ddc0 to your computer and use it in GitHub Desktop.

Select an option

Save salty-horse/37e9c7706c4e82995febcac146c0ddc0 to your computer and use it in GitHub Desktop.
Kobo sale forum post generator
function htmlToText(html){
html = html.replace(/\n/g, '');
html = html.replace(/\t/g, '');
html = html.replace(/<\/td>/g, '\t');
html = html.replace(/<\/table>/g, '\n');
html = html.replace(/<\/tr>/g, '\n');
html = html.replace(/<\/p>/g, '\n\n');
html = html.replace(/<\/div>/g, '\n');
html = html.replace(/<\/h>/g, '\n');
html = html.replace(/<em>/g, '[I]');
html = html.replace(/<\/em>/g, '[\/I]');
html = html.replace(/<br>/g, '\n'); html = html.replace(/<br( )*\/>/g, '\n');
let dom = (new DOMParser()).parseFromString('<!doctype html><body>' + html, 'text/html');
return dom.body.textContent.trim();
}
currencies = {'USD': ['$', 'US'], 'GBP': ['£', 'UK']};
function createPostText() {
let title = document.querySelector('h1.title').innerHTML.trim();
let author = document.querySelector('a.contributor-name').innerHTML.trim();
let description = htmlToText(document.querySelector('div.synopsis-description').innerHTML.trim());
let price = Math.round(parseFloat(document.querySelector('meta[property="og:price"]').content));
let [currency, country] = currencies[document.querySelector('meta[property="og:currency_code"]').content];
let seriesField = document.querySelector('span.product-sequence-field');
let series = '';
if (seriesField) {
seriesName = seriesField.innerText;
seriesNumber = document.querySelector('span.sequenced-name-prefix');
if (seriesNumber) {
let match = seriesNumber.innerText.match(/\d+/);
if (match) {
series = ` ([I]${seriesName} #${match[0]}[/I])`;
}
}
if (!series) {
series = ` ([I]${seriesName}[/I])`;
}
}
let isbn = JSON.parse(JSON.parse(document.querySelector('#ratings-widget-details-wrapper').dataset.koboGizmoConfig).googleBook).workExample.isbn;
let amazonTLD = country == 'US' ? 'com' : 'co.uk';
let amazonURL = `https://www.amazon.${amazonTLD}/s?k=${isbn}&i=digital-text`;
let pageUrl = window.location.href.split('?')[0];
let text = `[B][I]${title}[/B][/I]${series} by ${author} is ${currency}${price} in the ${country} ([URL=${pageUrl}]Kobo[/URL], [URL=${amazonURL}]Amazon[/URL])\n[QUOTE]${description}[/QUOTE]`;
let div = document.createElement('div');
div.style = "position: fixed; top: 0em; right: 0em; width: 500px; height: 500px; border: 1px solid black; z-index: 10000;";
let textarea = document.createElement('textarea');
textarea.style = "width: 400px; height: 400px;";
textarea.value = text;
div.appendChild(textarea);
document.body.appendChild(div);
textarea.focus();
textarea.setSelectionRange(0, text.length, 'backward');
window.addEventListener('click',function(event) {
if (!(div.contains(event.target))) {
div.remove();
}
});
}
createPostText();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment