Hi

I just wanted to hack a free nodelet hack (sic) on the fly, which automatically links any used or required module in code-sections to CPAN.

But I don't know what I'm doing wrong, can't make it work yet.

Not sure if it's a JS problem or related to Chrome and security issues (couldn't test in FF yet).

Before scratching all the efforts, I thought it might be reasonable to ask for help.

paste this into the JS Console

function linkCPAN() { let codes = document.getElementsByClassName('codetext'); // let pattern = '$1<href="http://search.cpan.org/perldoc?$3">$3</ +a>'; let replacer = function (match, p1, p2, p3) { return p1 + '<href="http://search.cpan.org/perldoc?' + p3 + '" +>' + p3 + '</a>'; }; for (var i in codes) { if (!codes.hasOwnProperty(i)) continue; let text = codes[i]; let inner = text.innerHTML; inner = inner.replace(/((use|require)\s+)((\w+)(::\w+)*)/g, re +placer); console.log(inner); text.innerHTML = inner; } }

and call linkCPAN()

you should see the properly replaced HTML in the console, but nothing happens in the window.

here some input for testing

use strict; use Data::Dump;

Any ideas?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

update

fixed JS has no hashes but objects bug with if (!codes.hasOwnProperty(i)) continue;


In reply to Nodelet Hack to auto-linkify modules in code sections (well almost) by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.