User:Kku/Scripts/BacklinkTitle.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*replace
<h1 id="firstHeading" class="firstHeading" lang="en">Data visualization</h1>
in
https://en.wikipedia.org/wiki/Data_visualization
by
<h1 id="firstHeading" class="firstHeading" lang="en"><a href="https://en.wikipedia.org/wiki/Special:WhatLinksHere/Data_visualization">Data visualization</a></h1>
*/
/* BUGS:
 1. does not work with firstChild.nodeValue italic titles, these will vanish (nesting!!)
     workaround found. will eradicate italics...
 2. ...
*/
$(document).ready(function() {
console.log('backlinks.js');
//console.log(mw.config.get("wgCanonicalNamespace"));
//console.log(mw.config.get("wgAction"));
//console.log(mw.config.get("wgArticleId"));

    if (mw.config.get("wgCanonicalNamespace") == "") // (true)
    {

       if (mw.config.get("wgAction") == "view") {
       
        var pageName = mw.config.get("wgPageName");
        var hd = document.getElementById("firstHeading");
        var hdchild=hd.firstchild;
        var pagtit=hd.getElementsByClassName('mw-page-title-main');
        var hdtxt= pagtit[0] ? pagtit[0].innerHTML : '';
        
//console.log('mw.config.get("wgPageName") '+pageName);
//console.log('mw-page-title-main '+hdtxt);
        var searchNode = document.createElement("a");
        searchNode.innerHTML = hdtxt;
        searchNode.setAttribute('href', '/wiki/Special:WhatLinksHere/' + encodeURIComponent(hdtxt));
        if ( pagtit[0] ) {
          pagtit[0].textContent='';
          pagtit[0].appendChild(searchNode);
        }
     }
    } 
});