-1

I have a blog where I format links in a specific way, by adding mouseover/hover text with a basic description of the link in the following format: {page title} [by {author(s)} @ {publication date & time}] | {website}. Here’s a screencap with an example.

As you can imagine, manually entering that title text for every single link gets quite tiresome. I’ve been doing this for years and I’m dying for a way to automate it.

Is there a way to automatically generate a target webpage’s title, and possibly the site/domain, in link mouseover texts across an entire website? (Ideally it would be formatted as indicated above, with author(s) and posted date/time and all, but that would likely involve too much coding for me.)

Please keep in mind that I only have a moderate, self-taught grasp of HTML and CSS.


Update: Anik Raj below provided an answer below that’s perfect – a bit of JS to generate a mouseover tooltip with the target page’s title – but I can’t get the script to work on my Blogger blog. I first saved the code to a .js file in my Dropbox and tried linking to it using the following code (which works fine for other external JS scripts):

<!-- Link hover title preview script (source: https://stackoverflow.com/questions/49950669/how-to-generate-target-webpage-title-in-link-mouseover-text/49951153#49951153) -->
<script async='async' src='https://dl.dropboxusercontent.com/s/h6enekx0rt9auax/link_hover_previews.js' type='text/javascript'/>

… But nothing happens. And when I insert the script in the page HTML, I get the following error (screencap here) and Blogger won’t let me save the template:

Error parsing XML, line 4002, column 21: The content of elements must consist of well-formed character data or markup.

I know nothing of code, JS included, so I don’t know how to fix it. I’ve tried several online JS validation tools; some identify an error there, others don’t. It clearly works just fine in the JSFiddle Anik provided.

If anyone could fix the code so it works in Blogger, you’d be my hero.

Walter
  • 249
  • 1
  • 8

1 Answers1

1

Edit: this works only under the same domain as other domains have CORS disabled.

The easiest way would be to add a java script file to the html file.

This is a simple script to set the title of the link as its hover text.

<script type = "text/javascript">
//get all links on the webpage
var links = document.getElementsByTagName("a");

for (var i = 0; i < links.length; i++) {
(function(i) {
    // for every link, make a request using its href property and fetch its html
    var request = makeHttpObject();
    request.open("GET", links[i].href, true);
    request.send(null);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            //on request received, process the html and set the title parameter
            const doc = new DOMParser().parseFromString(request.responseText, "text/html");
            const title = doc.querySelectorAll('title')[0];
            if (title) links[i].setAttribute("title", title.innerText)
        }
    };
})(i);
}

//helper function to create requests
function makeHttpObject() {
try {
    return new XMLHttpRequest();
} catch (error) {}
try {
    return new ActiveXObject("Msxml2.XMLHTTP");
} catch (error) {}
try {
    return new ActiveXObject("Microsoft.XMLHTTP");
} catch (error) {}
throw new Error("Could not create HTTP request object.");
} 
</script>

Adding this script to the end will add hover text to all links.

See this JS Fiddle example -> https://jsfiddle.net/mcdvswud/11/

Anik Raj
  • 76
  • 6
  • Thanks for the script! It works perfectly in the Fiddle, it’s exactly what I want. But I can’t get it to work on my blog. Linking to it remotely does nothing – no hover text appears on my links – and when I insert it directly in the HTML, I get [this error](https://i.imgur.com/AuhFzZt.jpg): `Error parsing XML, line 4002, column 21: The content of elements must consist of well-formed character data or markup.` I know nothing of scripting so I can’t fix it myself. Please help? EDIT: In case it helps at all, my template is encoded in XHTML 1.0 Strict. – Walter Apr 21 '18 at 01:10
  • [look here](https://stackoverflow.com/questions/4338538/error-parsing-xhtml-the-content-of-elements-must-consist-of-well-formed-charact?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Anik Raj Apr 22 '18 at 03:45
  • The less than operator `<` needs to be escaped. So replace `for (var i = 0; i < links.length; i++)` with `for (var i = 0; i < links.length; i++)` – Anik Raj Apr 22 '18 at 03:47
  • Thanks. I have good news and bad news: First, the code now saves and runs on the page just fine. See it in action on my test blog: https://preliator2test.blogspot.com/ However, the hover text it produces is wildly inconsistent. I tested with both Firefox and Chrome. Most links produce “[something went wrong](https://i.imgur.com/bRNGzvf.jpg)” (whether or not those links already have a `title=""` attribute) in both browsers. On links that work, they look fine in Chrome but give me either [one](https://i.imgur.com/Mp5AyGY.jpg) or [two](https://i.imgur.com/zxjLkZR.jpg) empty lines in Firefox. – Walter Apr 22 '18 at 04:21
  • First, can we identify what’s causing the “something went wrong” errors on most links? And second, can we remove the blank spaces in the hover text in Firefox? – Walter Apr 22 '18 at 04:22
  • The "something went wrong" error is due to [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). Simply put, you will not be able to access websites outside your domain. The script works for links in your same domain (like a link to your own blog post). This solutions is not ideal as your page will make many requests on load and it will slow down the website. Best way would be to use a backend to fetch all these data, save it in a database and serve it with the titles. This makes sure you're not making requests from the client browser and CORS problems will be fixed. – Anik Raj Apr 22 '18 at 04:31
  • Ah. Thank you for explaining. It makes sense, but it also means that this solution is thus unusable for me, since getting titles from other domains is exactly what I was looking for. It’s starting to sound (from my newbie perspective) like this may not be possible. Still, thanks for your help. EDIT: Marking your answer as accepted. – Walter Apr 22 '18 at 05:04
  • No, it's not possible to request other website resources from a browser environment. The solution only works in the same domain. – Anik Raj Apr 22 '18 at 05:49