User:PrimeHunter/Parameter links.js

From Wikipedia, the free encyclopedia
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.
/* This script adds "Parameter links" below "What links here" in the sidebar.
   "What links here" will include pages using a template with the link or
   calling a template to produce the link.
   "Parameter links" tries to search for pages which produce the link by calling a
   link-making template with the page name as a parameter given in the source,
   for example {{Cat|Stubs}} to produce a link to [[:Category:Stubs]].
   It's an imperfect search and may not detect all wanted links correctly.
   To use the script, add the following line to Special:MyPage/common.js:
 
importScript('User:PrimeHunter/Parameter links.js'); // Linkback: [[User:PrimeHunter/Parameter links.js]]

  See [[PrimeHunter/Source links.js]] for a different script to search pages making a link with
  full link syntax directly in the source, for example [[:Category:Stubs]]. 
*/

mw.loader.using(['mediawiki.util'], function () {
  var name = mw.config.get( 'wgPageName' );
  var title= mw.config.get( 'wgTitle' );
  var url;
  try {
      url = mw.util.getUrl( 'Special:Search' ) + '?profile=all&search=' +
      encodeURIComponent('linksto:"' + name + '" ' + 'insource:"' + title + '" ' +
      'insource:/\\|[' + title[0] + title.charAt(0).toLowerCase() + ']' +
      title.substring(1).replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&') + '[\\}\\|#]/' ).replace(/_/g, " ");
  } catch(e) {
      url = '#error'; // EXAMPLE URL: https://en.wikipedia.org/w/index.php?title=%F0%9F%91%A9%F0%9F%8F%BF&action=history
  }
  mw.util.addPortletLink(
    'p-tb',
    url,
    'Parameter links',
    'pt-parameterlinks',
    'What links here via a link-producing template call in the source with ' +
    'the page name (without namespace) as a parameter',
    null,
    '#t-recentchangeslinked'
  );
});