Avatar

About opening of links in new tabs or browser windows (Features)

by Micha ⌂, Friday, October 23, 2020, 18:30 (38 days ago) @ Micha

Hello,


Why not add a new section user_settings (or another meaningful name) instead adding a new file?

Yes, we can also extend the existing file by a new section and adding an option like:

user_settings['open_links_in_new_window'] = 'EXTERNAL';  // EXTERNAL, ALL, NON

Should we also extend the main.js, too, or is it better to create a new script e.g. entry.js. If we use a new file, we can save resources by adding something like:

{if $mode=='entry' || $mode=='thread'}
<script src="{$FORUM_ADDRESS}/js/entry.min.js" type="text/javascript" charset="utf-8"></script>
{/if}

A further problem is HTML structure. Whereas each entry provides a header and a body class in thread mode, such elements are not presented in the single entry view. Is it possible to add such classes, too? It would be helpful to restrict the scope of modifying the target-attribute.

My test script looks like that:

var user_settings = new Array();
user_settings["open_links_in_new_window"] = "EXTERNAL";  // EXTERNAL, ALL, NON
 
function Entry(el) {
 if (!el) 
  return;
 
 this.setLinkTarget = function() {
  if (user_settings["open_links_in_new_window"].toUpperCase() == "EXTERNAL" || 
   user_settings["open_links_in_new_window"].toUpperCase() == "ALL") {
   var entryBodies = el.getElementsByClassName("body");
   for (var i=0; i<entryBodies.length; i++) {
    var links = entryBodies[i].getElementsByTagName("a");
    for (var j=0; j<links.length; j++) {
     // skip internal links
     if (user_settings["open_links_in_new_window"].toUpperCase() == "EXTERNAL" && links[j].href.includes(window.document.location.origin))
      continue;
     links[j].target = "_blank";
    }
   }
  }
 };
};
 
function Entries(cEl) {
 if (!cEl) 
  return;
 console.log(user_settings);
 var pEls = cEl.getElementsByClassName("posting");
 pEls = pEls.length > 0 ? pEls : cEl.getElementsByClassName("thread-posting");
 pEls = (typeof pEls == "object" || typeof pEls == "function") && typeof pEls.length == "number"?pEls:[pEls];
 for (var i=0; i<pEls.length; i++) {
  var entry = new Entry(pEls[i]);
  entry.setLinkTarget(); 
 }
};
 
window.ready.push(function() {
 if (typeof user_settings == "object") 
  new Entries( document.getElementById("content") );
});


/Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences


Complete thread:

 RSS Feed of thread

powered by my little forum