From aab735bd39b635ecbc0792c2df219c645985f244 Mon Sep 17 00:00:00 2001 From: ashastral Date: Wed, 18 Sep 2024 17:57:16 -0700 Subject: [PATCH] add bookmarklet --- bookmarklet-uri.txt | 1 + bookmarklet.cjs | 68 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 bookmarklet-uri.txt create mode 100644 bookmarklet.cjs diff --git a/bookmarklet-uri.txt b/bookmarklet-uri.txt new file mode 100644 index 0000000..67e0502 --- /dev/null +++ b/bookmarklet-uri.txt @@ -0,0 +1 @@ +javascript:void%20function(){let%20a=JSON.parse(document.getElementById(%22trpc-dehydrated-state%22).innerText),b=a.queries.find(a=%3Ea.queryKey[0].includes(%22singlePost%22)).state.data.post,c=document.createElement(%22cohost-post%22);if(c.setAttribute(%22avatarSrc%22,b.postingProject.avatarURL),c.setAttribute(%22avatarShape%22,b.postingProject.avatarShape),c.setAttribute(%22displayName%22,b.postingProject.displayName),c.setAttribute(%22handle%22,b.postingProject.handle),c.setAttribute(%22permalink%22,b.singlePostPageUrl),c.setAttribute(%22publishedAt%22,b.publishedAt),b.headline%26%26c.setAttribute(%22singlePostHeadline%22,b.headline),c.setAttribute(%22tags%22,b.tags.join(%22,%22)),b.shareTree%26%26b.shareTree.length){c.setAttribute(%22sharedItems%22,%22%22);let%20a=b.shareTree[b.shareTree.length-1];c.setAttribute(%22sharedAvatarSrc%22,a.postingProject.avatarURL),c.setAttribute(%22sharedAvatarShape%22,a.postingProject.avatarShape),c.setAttribute(%22sharedDisplayName%22,a.postingProject.displayName),c.setAttribute(%22sharedHandle%22,a.postingProject.handle);let%20d=[...b.shareTree,b];for(sharedPost%20of%20d){if(null!==sharedPost.transparentShareOfPostId)continue;let%20a=document.createElement(%22cohost-shared-item%22);a.setAttribute(%22avatarSrc%22,sharedPost.postingProject.avatarURL),a.setAttribute(%22avatarShape%22,sharedPost.postingProject.avatarShape),a.setAttribute(%22displayName%22,sharedPost.postingProject.displayName),a.setAttribute(%22handle%22,sharedPost.postingProject.handle),sharedPost.headline%26%26a.setAttribute(%22headline%22,sharedPost.headline),a.setAttribute(%22permalink%22,sharedPost.singlePostPageUrl),a.setAttribute(%22publishedAt%22,sharedPost.publishedAt),a.setAttribute(%22tags%22,sharedPost.tags.join(%22,%22));let%20b=document.getElementById(`post-${sharedPost.postId}`);b%26%26(a.innerHTML=b.parentElement.querySelector(%22[data-post-body]%22).innerHTML),c.appendChild(a)}}else{let%20a=document.getElementById(`post-${b.postId}`);a%26%26(c.innerHTML=document.getElementById(`post-${b.postId}`).parentElement.querySelector(%22[data-post-body]%22).innerHTML)}let%20d=document.createElement(%22div%22);d.id=%22cohost-wc-bookmarklet-output%22,d.style=%22position:%20fixed;%20bottom:%200.5rem;%20left:%200.5rem;%20display:%20flex;%20flex-direction:%20column;%20gap:%200.25rem;%20font-size:%200.875rem;%20z-index:%20100%22;let%20e=document.createElement(%22textarea%22);e.value=c.outerHTML,e.rows=5,e.cols=20,e.style=%22font-size:%20inherit;%20line-height:%201.4%22,d.append(e);let%20f=document.createElement(%22button%22);f.innerText=%22dismiss%22,f.onclick=function(){d.remove()},d.append(f),document.body.append(d)}(); \ No newline at end of file diff --git a/bookmarklet.cjs b/bookmarklet.cjs new file mode 100644 index 0000000..5bd7dd5 --- /dev/null +++ b/bookmarklet.cjs @@ -0,0 +1,68 @@ +let trpcState = JSON.parse(document.getElementById("trpc-dehydrated-state").innerText); +let postData = trpcState.queries.find((q) => q.queryKey[0].includes("singlePost")).state.data.post; +let postElement = document.createElement("cohost-post"); +postElement.setAttribute("avatarSrc", postData.postingProject.avatarURL); +postElement.setAttribute("avatarShape", postData.postingProject.avatarShape); +postElement.setAttribute("displayName", postData.postingProject.displayName); +postElement.setAttribute("handle", postData.postingProject.handle); +postElement.setAttribute("permalink", postData.singlePostPageUrl); +postElement.setAttribute("publishedAt", postData.publishedAt); +if (postData.headline) { + postElement.setAttribute("singlePostHeadline", postData.headline); +} +postElement.setAttribute("tags", postData.tags.join(",")); + +if (postData.shareTree && postData.shareTree.length) { + postElement.setAttribute("sharedItems", ""); + let sharedFrom = postData.shareTree[postData.shareTree.length - 1]; + postElement.setAttribute("sharedAvatarSrc", sharedFrom.postingProject.avatarURL); + postElement.setAttribute("sharedAvatarShape", sharedFrom.postingProject.avatarShape); + postElement.setAttribute("sharedDisplayName", sharedFrom.postingProject.displayName); + postElement.setAttribute("sharedHandle", sharedFrom.postingProject.handle); + + let shareTree = [...postData.shareTree, postData]; + for (sharedPost of shareTree) { + if (sharedPost.transparentShareOfPostId !== null) { + continue; + } + let sharedElement = document.createElement("cohost-shared-item"); + sharedElement.setAttribute("avatarSrc", sharedPost.postingProject.avatarURL); + sharedElement.setAttribute("avatarShape", sharedPost.postingProject.avatarShape); + sharedElement.setAttribute("displayName", sharedPost.postingProject.displayName); + sharedElement.setAttribute("handle", sharedPost.postingProject.handle); + if (sharedPost.headline) { + sharedElement.setAttribute("headline", sharedPost.headline); + } + sharedElement.setAttribute("permalink", sharedPost.singlePostPageUrl); + sharedElement.setAttribute("publishedAt", sharedPost.publishedAt); + sharedElement.setAttribute("tags", sharedPost.tags.join(",")); + let sharedIdElement = document.getElementById(`post-${sharedPost.postId}`); + if (sharedIdElement) { + sharedElement.innerHTML = sharedIdElement.parentElement.querySelector("[data-post-body]").innerHTML; + } + postElement.appendChild(sharedElement); + } +} else { + let postIdElement = document.getElementById(`post-${postData.postId}`); + if (postIdElement) { + postElement.innerHTML = document.getElementById(`post-${postData.postId}`).parentElement.querySelector("[data-post-body]").innerHTML; + } +} + +let outputContainer = document.createElement("div"); +outputContainer.id = "cohost-wc-bookmarklet-output" +outputContainer.style = "position: fixed; bottom: 0.5rem; left: 0.5rem; display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.875rem; z-index: 100"; + +let outputTextarea = document.createElement("textarea"); +outputTextarea.value = postElement.outerHTML; +outputTextarea.rows = 5; +outputTextarea.cols = 20; +outputTextarea.style = "font-size: inherit; line-height: 1.4"; +outputContainer.append(outputTextarea); + +let outputDismissButton = document.createElement("button"); +outputDismissButton.innerText = "dismiss"; +outputDismissButton.onclick = function() { outputContainer.remove(); } +outputContainer.append(outputDismissButton); + +document.body.append(outputContainer); \ No newline at end of file