3 ms·
Show HN: Add button on HN submit form to automatically fill in webpage title
Ninety-nine percent of submissions have exactly the same title as the linked webpage, and in fact the HN Guidelines suggest that this should be the case except when the webpage title has been editorialized.
It's an unnecessary PITA to have to type in the title when it's exactly the same as the webpage title - particularly difficult on mobile - and this process could easily be automated.
I have seen a feature in some subreddits that does this: a button on the form which, when clicked, would pull the webpage title from the link that is in the form, and automatically fill in the Title field with that title. The button will be called "Suggest Title."
I don't code, but I asked ChatGPT to write some code that could work, and I'm hoping this gives whoever wants to work on this a head start. Everyone else can voice their agreement or disagreement. Here is the code:
// Select the necessary DOM elements
const urlField =
document.querySelector('input[name="url"]');
const titleField =
document.querySelector('input[name="title"]');
const suggestButton = document.createElement('button');
// Add text to the button
suggestButton.textContent = 'Suggest Title';
// Add click event listener to the button
suggestButton.addEventListener('click', () => {
// Fetch the webpage title from the given URL
fetch(urlField.value)
.then(response => response.text())
.then(html => {
// Use a regular expression to extract the title tag content
const titleRegex = /<title>(.\*?)<\/title>/;
const matches = html.match(titleRegex);
if (matches && matches.length > 1) {
const title = matches[1];
// Set the title field value
titleField.value = title;
}
});
});
// Insert the button into the DOM
urlField.parentNode.insertBefore(suggestButton, urlField.nextSibling);
- LinuxBender 3y agoOne point of discussion might be the limit of 80 characters on HN. I often have to edit the title to fit and still make sense. If I just capped them at 80 characters without editorializing them the end result would be confusing in many cases. I have reached out to several news sites asking them to limit titles to 80 characters but predictably they are not interested. Can ChatGPT include code that shortens the title to 80 characters and still make sense?
- KomoD 3y agoBookmarklet exists https://news.ycombinator.com/bookmarklet.html https://news.ycombinator.com/bookmarklet.html