Created
May 8, 2022 02:16
-
-
Save thesunshade/7e89e5734db2d721a4ac65292aec8aaa to your computer and use it in GitHub Desktop.
Grab page titles and some content from suttacentral.net, dhammatalks.org and suttafriends.org
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: (() => { | |
let url = window.location; | |
let pageTitle = document.title; | |
postTitle = ""; | |
let partialForTitle = ""; | |
let partialForAttribution = "this"; | |
let suttaBodyContent = "SUTTA BODY CONTENT GOES HERE"; | |
let attribution = ""; | |
let contents = ""; | |
let suttaEnglishTitle = ""; | |
function expandCitations(pageTitle) { | |
pageTitle = pageTitle | |
.replace("DN", "Dīgha Nikāya") | |
.replace(/MN/, "Majjhima Nikāya") | |
.replace("SN", "Saṁyutta Nikāya") | |
.replace("AN", "Aṅguttara Nikāya") | |
.replace(/Khp|Kp/, "Khuddakapāṭha") | |
.replace("Dhp", "Dhammapada") | |
.replace("Ud", "Udāna") | |
.replace(/Iti|Itv/, "Itivuttaka") | |
.replace("Vv", "Vimānavatthu") | |
.replace("Pv", "Petavatthu") | |
.replace("Thag", "Theragāthā") | |
.replace("Thig", "Therīgāthā"); | |
return pageTitle; | |
} | |
if (!window.confirm("If this is a complete sutta-> OK\nIf this is a partial sutta-> CANCEL")) { | |
partialForTitle = " From..."; | |
partialForAttribution = "the entire"; | |
} | |
console.log(window.location.hostname); | |
switch (window.location.hostname) { | |
case "suttacentral.net": | |
let splitPageTitle = pageTitle.split("—"); | |
pageTitle = splitPageTitle[0]; | |
let authorFullName = splitPageTitle[1]; | |
if (/bodhi/.test(url) || /ireland/.test(url)) { | |
let suttaTitleElement = document.getElementsByTagName("h1"); | |
suttaEnglishTitle = suttaTitleElement[0].innerText.replace(/^.+?\. /, ""); | |
pageTitle = pageTitle.replace(":", ""); | |
pageTitle = `${pageTitle}: ${suttaEnglishTitle}`; | |
postTitle = pageTitle.replace(/(\d)\s/, "$1" + partialForTitle + " "); | |
} else { | |
let suttaTitlesElement = document.getElementsByClassName("sutta-title"); | |
suttaEnglishTitle = suttaTitlesElement[0].innerText; | |
console.log({ suttaEnglishTitle }); | |
pageTitle = pageTitle.replace(":", ""); | |
pageTitle = `${pageTitle}: ${suttaEnglishTitle}`; | |
postTitle = pageTitle.replace(/(\d)\s/, "$1" + partialForTitle + " "); | |
console.log("PostTitle=>" + postTitle + " page->" + pageTitle); | |
} | |
attribution = `Read ${partialForAttribution} translation of <i>${expandCitations( | |
pageTitle | |
)}</i> by ${authorFullName} on <a href="${window.location.href.replace( | |
/\?.+$/, | |
"" | |
)}" rel="noreferrer" target="_blank">SuttaCentral.net</a>. `; | |
contents = `#${postTitle}\n${suttaBodyContent}\n<hr>${attribution}`; | |
console.log(contents); | |
break; | |
case "www.dhammatalks.org": | |
pageTitle = pageTitle | |
.replace(/\u00A0/, "") | |
.replace(":", ".") | |
.replace(" |", ":"); | |
postTitle = pageTitle.replace(/(\d)\s/, "$1" + partialForTitle + " "); | |
suttaBodyContent = document.getElementById("sutta"); | |
pageTitle = pageTitle.replace(/\u00A0/, "").replace(":", "."); | |
attribution = `Read ${partialForAttribution} translation of <i>${expandCitations( | |
pageTitle | |
)}</i> by Bhikkhu Ṭhanissaro on <a href="${ | |
window.location.href | |
}" rel="noreferrer" target="_blank">DhammaTalks.org</a>. `; | |
contents = `#${postTitle}\n${suttaBodyContent.innerText}\n<hr>${attribution}`; | |
break; | |
case "suttafriends.org": | |
pageTitle = pageTitle.replace(" – Sutta Friends", ""); | |
postTitle = pageTitle.replace(/(\d)\s/, "$1" + partialForTitle + " "); | |
suttaBodyContent = document.getElementsByClassName("entry-content"); | |
attribution = `Read ${partialForAttribution} translation of <i>${expandCitations(pageTitle).replace( | |
"– Sutta Friends", | |
"" | |
)}</i> by Ven. Kiribathgoda Gnananda Thero on <a href="${ | |
window.location.href | |
}" rel="noreferrer" target="_blank">SuttaFriends.org</a>. `; | |
contents = `#${postTitle}\n${suttaBodyContent[0].innerText}\n<hr>\n${attribution}`; | |
break; | |
case "www.ancient-buddhist-texts.net": | |
pageTitle = document.getElementsByClassName("Heading1"); | |
pageTitle = pageTitle[0].textContent.split("\n"); | |
suttaEnglishTitle = pageTitle[0]; | |
pageTitle[1] = pageTitle[1].replace("suttaṁ", "sutta").replace("(", "").replace(")", "").replace("Aṅg ", "AN "); | |
let paliAndCitation = pageTitle[1].split(", "); | |
let [suttaPaliTitle, suttaCitation] = paliAndCitation; | |
console.log({ pageTitle }); | |
console.log({ suttaPaliTitle }); | |
console.log({ suttaCitation }); | |
postTitle = `${suttaCitation} ${partialForTitle} ${suttaPaliTitle}: ${suttaEnglishTitle}`; | |
console.log(postTitle); | |
attribution = `Read ${partialForAttribution} translation of <i>${suttaCitation} ${suttaPaliTitle}: ${suttaEnglishTitle}</i> by Bhikkhu Ānandajoti on <a href="${window.location.href}" rel="noreferrer" target="_blank">Ancient-Buddhist-Texts.net</a>. `; | |
contents = `#${postTitle}\n${suttaBodyContent}\n<hr>\n${attribution}`; | |
break; | |
} | |
navigator.clipboard.writeText(contents); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment