Skip to content

Instantly share code, notes, and snippets.

@rynomad
Last active April 28, 2023 20:55
Show Gist options
  • Save rynomad/2f79fd427dd420dcd71ecc7a59ed3a31 to your computer and use it in GitHub Desktop.
Save rynomad/2f79fd427dd420dcd71ecc7a59ed3a31 to your computer and use it in GitHub Desktop.
highlight and drag this code into your bookmark bar. If that doesn't work, ask chatGPT how to make a bookmarklet
javascript:(function() {
function extractText(element) {
if (element.nodeType === Node.TEXT_NODE) {
return element.textContent.trim() + ' ';
}
if (element.nodeType !== Node.ELEMENT_NODE) {
return '';
}
let text = '';
for (const child of element.childNodes) {
text += extractText(child);
}
if (element.tagName.match(/^(H[1-6]|P|LI|DIV)$/)) {
text += '\n';
}
return text;
}
function sanitizeText(text) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = text;
return tempDiv.textContent || tempDiv.innerText || '';
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
try {
const contentSelectors = ['article', '.article', '.post', '.entry', 'main', '.content', '.c-articlefragment', '.c-title__text', '.byline', '.text'];
let articleElement = null;
for (const selector of contentSelectors) {
articleElement = document.querySelector(selector);
if (articleElement) {
break;
}
}
if (!articleElement) {
throw new Error('Article element not found');
}
const text = sanitizeText(extractText(articleElement));
const preamble = '[1] - Using the heading Debiased Rewrite, rewrite the article below to make it more informative, truth-focused, and neutral.\n[2] - Using the heading Bias in Original, compare the original article to the rewrite, and describe the bias of the original, use a numbered list if needed\n\n';
const clipboardText = preamble + text;
copyToClipboard(clipboardText);
alert('The article and a prompt have been copied to your clipboard. Please paste into ChatGPT to see through the spin.');
} catch (error) {
const message = `Objective: Modify the bookmarklet to change the preamble and alert text.
Specification:
1) Optimize for news websites
2) Include headings and subheadings
3) Use a simple JavaScript alert for the popup
4) Update the preamble and alert text as specified
Error: ${error.message}
Source code of the bookmarklet:
${decodeURIComponent(window.location.href)}
Please help improve this code to prevent the error.`;
copyToClipboard(message);
alert('there was an error in execution. the prompt for this bookmarklet and the error message have been copied to your clipboard, you can paste them into chatGPT 4 to try and fix the problem\n\n If you make improvements, please submit them to https://gist.github.com/rynomad/2f79fd427dd420dcd71ecc7a59ed3a31/');
}
})()
@steve-kennedy
Copy link

Would suggest updating the preamble to include headings for the two parts, as without knowing the prompt it mayn't be immediately clear that the output is a rewritten article. The numbered list of biases also starts at 2), for some reason.

I've found the below works:
const preamble = '[1] - Using the heading Debiased Rewrite, rewrite the article below to make it more informative, truth-focused, and neutral.\n[2] - Using the heading Bias in Original, compare the original article to the rewrite, and describe the bias of the original, use a numbered list if needed\n\n';

@jaredduggan
Copy link

It may be helpful to add the ability for the reader view of articles to be copied. As is, most articles have so many advertisements and breaks in the article that the text being copied throws errors in a response.

@rynomad
Copy link
Author

rynomad commented Apr 5, 2023

@steve-kennedy thanks! I've incorporated that tweak.

@jaredduggan I actually tried the reader view on brave first. Unfortunately it seems to do something to the dom that makes the scraping function not work properly, but I admit I didn't look too deeply into it, will take another crack at doing something with reader view

@steve-kennedy
Copy link

@rynomad, you may find the Readability library useful to check out (https://github.com/mozilla/readability). It's a standalone version of the library used in the Firefox reader view.

@jaredduggan I actually tried the reader view on brave first. Unfortunately it seems to do something to the dom that makes the scraping function not work properly, but I admit I didn't look too deeply into it, will take another crack at doing something with reader view

@jim80net
Copy link

jim80net commented Apr 9, 2023

Hi @rynomad, wanted to let you know I made a slight modification to your bookmarklet to create a tool to help with chunking articles for summary purposes. Thanks for the inspiration and jumping off point!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment