1 points · by hammock · 3y ago · 2 comments
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);