Boost.org - Source Highlight

Add code highlight to boost.org pages.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Boost.org - Source Highlight
// @namespace   uk.jixun
// @match       https://www.boost.org/doc/*
// @grant       none
// @version     1.0
// @author      Jixun
// @description Add code highlight to boost.org pages.
// @run-at      document-start
// @license     apache-2.0
// ==/UserScript==

function main() {
  const codeBlocks = Array.from(document.querySelectorAll('pre'));
  if (codeBlocks.length === 0) return;

  const stylesheet = document.createElement('link');
  Object.assign(stylesheet, {
    rel: 'stylesheet',
    href: 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css',
  });
  document.head.appendChild(stylesheet);

  const hljsScript = document.createElement('script');
  Object.assign(hljsScript, {
    src: 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js',
    onload: () => {
      hljs.configure({
        languages: ['cpp'],
        ignoreUnescapedHTML: true, // anchors
      });
      for(const codeBlock of codeBlocks) {
        const anchors = codeBlock.querySelectorAll('a');
        hljs.highlightElement(codeBlock);
        
        if (anchors.length === 0) continue;
        const urlMap = new Map(Array.from(anchors, anchor => [anchor.textContent, anchor.href]));
        console.info(urlMap);

        for (const strEl of codeBlock.querySelectorAll('.hljs-string')) {
          const fileName = strEl.textContent.slice(1, -1);
          console.info('searching for %s...', fileName);
          if (urlMap.has(fileName)) {
            const newAnchor = document.createElement('a');
            newAnchor.href = urlMap.get(fileName);
            newAnchor.style.borderBottom = '1px dotted';
            strEl.insertAdjacentElement('afterend', newAnchor);
            newAnchor.appendChild(strEl);
          }
        }
      }
    },
  });
  document.head.appendChild(hljsScript);
}

if (document.readyState === 'loading' && !document.head) {
  addEventListener('DOMContentLoaded', main);  
} else {
  main();
}