Using JavaScript to Change the Name of Menu Items

NOTE: Programming changes made through JavaScript and CSS are not supported by the dominKnow support team.  

In some cases, your organization may have strict guidelines with naming conventions that may require changes to standard navigation elements found in dominKnow | ONE.

While dominKnow | ONE's theme designer is quite flexible, you are not able to change the names of the different elements in the player. However, you can take advantage of the "Add JS" option in the theme builder to make these types of changes. 

Adding JavaScript to a Theme

Steps:

  1. Edit your desired theme.
  2. Select Add JS.
    1. NOTE: If you do not see this option, then it has been disabled by your admin for your user role. There are a variety of permission settings that an Admin (with the right permissions) can turn on and off. They will need to turn this option on to enable you to add JS to a theme.
  3. Paste in the JavaScript.
    1. NOTE: Do NOT include tags in your code. The code will automatically be put into script tags within the HTML.
  4. Select OK.
  5. Select Save.
  6. Test your changes.

Code for changing the name of menu items

This sample code is changing the term Bibliography to Reference in the menu and in the pop up when the Bibliography is selected in the Course Player theme. To change other elements, you would use a similar approach to identifying the item to change and putting in an alternate. 

NOTE: This code changes the English version of the content. If the course includes other languages it will use the default translation for these terms.

/**
 * Overrides "Bibliography" UI labels to set a custom term (Reference)(set on the last line).
 */
(function renameBibliography(newLabel) {
  // Replace all occurrences of substrings without using replaceAll
  function replaceAllStr(str, find, repl) {
    return String(str).split(find).join(repl);
  }

  // Patch the DKI translation dictionary when available
  function patchDictionary() {
    try {
      if (!window.DKI || !DKI.translatedPlayerStrings) { return false; }
      var locales = DKI.translatedPlayerStrings;
      for (var loc in locales) {
        if (!locales.hasOwnProperty(loc)) continue;
        var t = locales[loc] || {};
        if (t.doc && t.doc["Bibliography Heading"]) {
          t.doc["Bibliography Heading"] = newLabel;
        }
        if (t.references) {
          if (t.references.bibliographyWindowTitle) {
            t.references.bibliographyWindowTitle = newLabel;
          }
          if (t.references.viewBibliography) {
            t.references.viewBibliography = "View " + newLabel;
          }
        }
        if (t.endCourse && t.endCourse.viewBibliography) {
          t.endCourse.viewBibliography = "View " + newLabel;
        }
      }
      return true;
    } catch (e) {
      return false;
    }
  }

  // Walk text nodes and swap visible strings as a safety net
  function replaceInDOM(root) {
    try {
      var pairs = [
        ["Bibliography", newLabel],
        ["View Bibliography", "View " + newLabel],
        ["View the bibliography.", "View the " + newLabel.toLowerCase() + "."],
        ["Bibliography:", newLabel + ":"]
      ];

      function processTextNode(n) {
        var v = n.nodeValue;
        if (!v || !(/\S/).test(v)) return;
        for (var i = 0; i < pairs.length; i++) {
          v = replaceAllStr(v, pairs[i][0], pairs[i][1]);
        }
        if (v !== n.nodeValue) { n.nodeValue = v; }
      }

      if (document.createTreeWalker && window.NodeFilter) {
        var walker = document.createTreeWalker(
          root || document.body,
          NodeFilter.SHOW_TEXT,
          null,
          false
        );
        var node;
        while ((node = walker.nextNode())) {
          processTextNode(node);
        }
      } else {
        // Fallback traversal for very old engines
        var all = (root || document.body).getElementsByTagName("*");
        for (var j = 0; j < all.length; j++) {
          var el = all[j];
          for (var k = 0; k < el.childNodes.length; k++) {
            var cn = el.childNodes[k];
            if (cn.nodeType === 3) processTextNode(cn);
          }
        }
      }
    } catch (e) {}
  }

  // Try to patch immediately; if not ready, poll briefly
  function init() {
    if (patchDictionary()) return;
    var tries = 0, max = 120; // ~6s at 50ms
    var iv = setInterval(function () {
      if (patchDictionary() || ++tries >= max) clearInterval(iv);
    }, 50);
  }

  function onReady(fn) {
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", fn);
    } else {
      fn();
    }
  }

  onReady(function () {
    init();
    replaceInDOM(document.body);
  });

  // Observe late inserts if supported
  if (window.MutationObserver) {
    try {
      var mo = new MutationObserver(function (muts) {
        for (var i = 0; i < muts.length; i++) {
          var m = muts[i];
          if (!m.addedNodes) continue;
          for (var j = 0; j < m.addedNodes.length; j++) {
            var n = m.addedNodes[j];
            if (n.nodeType === 1) replaceInDOM(n);
            else if (n.nodeType === 3) replaceInDOM(n.parentNode || document.body);
          }
        }
      });
      mo.observe(document.documentElement, { subtree: true, childList: true, characterData: true });
    } catch (e) {}
  } else {
    // Last-resort periodic sweep for older browsers
    setInterval(function(){ replaceInDOM(document.body); }, 1000);
  }
})("References"); // <-- set your preferred label here
  0     0

Similar Projects

Questions  ( 0 )