The following script works on most static websites and hyperlinks, but it currently has an issue rendering tooltips for wikis and other dynamically generated content.
I have even used a timeout to wait for all the content to load before calling tooltipster()
, but this still did not render the tooltips.
setTimeout(function() {
$('a.tooltipster').tooltipster({
theme: 'tooltipster-punk'
});
}, 5000);
It works fine on sites like jQuery and MDN, but not on Wikipedia and Reddit.
Userscript
// ==UserScript==
// @name Link Hover
// @namespace default
// @version 1.0.0
// @description Display link.
// @author Mr. Polywhirl
// @match *://*/*
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.slim.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/js/jquery.tooltipster.min.js
// @resource tt_CSS https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/tooltipster.min.css
// @resource ttTheme_CSS https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/themes/tooltipster-punk.min.css
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_log
// ==/UserScript==
(function() {
'use strict';
GM_addStyle([
GM_getResourceText('tt_CSS'),
GM_getResourceText('ttTheme_CSS')
].join(''));
(function($) {
$.fn.setLinkTitle = function() {
return this.attr('title', processLink(this.attr('href')));
};
})(jQuery);
$(function() {
$('a').each((index, link) => {
$(link).addClass('tooltipster').setLinkTitle();
}).tooltipster({
theme: 'tooltipster-punk'
});
});
function processLink(path) {
return !isPathAbsolute(path) ? relPathToAbs(path) : path;
}
// http://stackoverflow.com/a/25833886/1762224
function relPathToAbs(sRelPath) {
var nUpLn, sDir = "", sPath = location.pathname.replace(/[^\/]*$/, sRelPath.replace(/(\/|^)(?:\.?\/+)+/g, "$1"));
for (var nEnd, nStart = 0; nEnd = sPath.indexOf("/../", nStart), nEnd > -1; nStart = nEnd + nUpLn) {
nUpLn = /^\/(?:\.\.\/)*/.exec(sPath.slice(nEnd))[0].length;
sDir = (sDir + sPath.substring(nStart, nEnd)).replace(new RegExp("(?:\\\/+[^\\\/]*){0," + ((nUpLn - 1) / 3) + "}$"), "/");
}
return location.protocol + '//' + location.host + sDir + sPath.substr(nStart);
}
function isPathAbsolute(path) {
return /^(?:\/|[a-z]+:\/\/)/.test(path);
}
})();