тна Back to MegaDB Search

_bdonkey | 37 points | Jul 04 2017 09:26:36

[META] JavaScript bookmarklet to automatically link-ify MEGA IDs (and decryption keys if present) | Megalinks MegaDB [META] JavaScript bookmarklet to automatically link-ify MEGA IDs (and decryption keys if present)

Converts text like:

/#F!p3x1nTqQ (stuff in between) !g2PCz8gDD0_RL9WhjT1NUw

to click-able links like:

https://mega.nz/#F!p3x1nTqQ!g2PCz8gDD0_RL9WhjT1NUw

Also works with bare Mega IDs only, without decryption keys. Saves you only a couple of seconds, but thought it might be worth sharing.

To install, highlight the entire line below and drag it to your bookmarks bar. (You can edit the bookmark to give it a name.)

javascript:(function()%7Bvar%20regex%20%3D%20new%20RegExp('(%23F%3F!%5Ba-zA-Z0-9%5D%7B8%7D)(%3F%3D(%3F%3A.*(!%5Ba-zA-Z0-9_-%5D%7B22%2C%7D))%3F)'%2C%20'g')%3Bdocument.body.innerHTML%20%3D%20document.body.innerHTML.replace(regex%2C%20'%241%20(%3Ca%20href%3D%22https%3A%2F%2Fmega.nz%2F%241%242%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fmega.nz%2F%241%242%3C%2Fa%3E)')%7D)()

To use, click that bookmark when you're on a page that has mega IDs and keys that you want to convert to links.

Note: it will screw up existing mega links on the page because my regex-fu is not strong, so only use it on pages where you want to convert an un-linkified MEGA id.

Source code:

var regex = new RegExp('(#F?![a-zA-Z0-9]{8})(?=(?:.*(![a-zA-Z0-9_-]{22,}))?)', 'g');
document.body.innerHTML = document.body.innerHTML.replace(regex, '$1 (<a href="https://mega.nz/$1$2" target="_blank">https://mega.nz/$1$2</a>)');

permalink


[-] [deleted] | 2 points | Jul 04 2017 20:06:56

[deleted]

permalink

[-] _bdonkey | 1 points | Jul 04 2017 21:49:47

thanks for finding that! i updated the regex in the post, try it again?

permalink

[-] [deleted] | 2 points | Jul 05 2017 02:19:42

[deleted]

permalink

[-] _bdonkey | 1 points | Jul 06 2017 04:19:32

thanks again, it should work now, but i'm sure you'll find other cases where it doesn't :)

permalink

[-] kuretake | 2 points | Jul 06 2017 00:08:45

How about something like this?

$('.usertext-body').each(function() {
  $(this).html(function(_, html) {
    let link, key;
    return html
    .replace(/#F?!\w{8}/, function(a) { link = a; return a; })
    .replace(/![\w-]{22,}/, function(a) { key = a; return a; })
    .replace(/$/, function() {
      let txt;
      if (!link || !key) {
        return '';
      }
      const url = 'https://mega.nz/' + link + key;
      return '<p><b><a href=' + url + ' target=_blank>' + url + '</a></b></p>';
    });
  });
});

Here's the bookmarklet:

javascript:(function()%7B%24('.usertext-body').each(function()%20%7B%24(this).html(function(_%2C%20html)%20%7Blet%20link%2C%20key%3Breturn%20html.replace(%2F%23F%3F!%5Cw%7B8%7D%2F%2C%20function(a)%20%7B%20link%20%3D%20a%3B%20return%20a%3B%20%7D).replace(%2F!%5B%5Cw-%5D%7B22%2C%7D%2F%2C%20function(a)%20%7B%20key%20%3D%20a%3B%20return%20a%3B%20%7D).replace(%2F%24%2F%2C%20function()%20%7Blet%20txt%3Bif%20(!link%20%7C%7C%20!key)%20%7Breturn%20''%3B%7Dconst%20url%20%3D%20'https%3A%2F%2Fmega.nz%2F'%20%2B%20link%20%2B%20key%3Breturn%20'%3Cp%3E%3Cb%3E%3Ca%20href%3D'%20%2B%20url%20%2B%20'%20target%3D_blank%3E'%20%2B%20url%20%2B%20'%3C%2Fa%3E%3C%2Fb%3E%3C%2Fp%3E'%3B%7D)%3B%7D)%3B%7D)%7D)()

Bookmarklet generated with http://mrcoles.com/bookmarklet/

permalink

[-] _bdonkey | 1 points | Jul 06 2017 04:24:44

this works great! does it only work on reddit and only on the first mega link+key it finds, though?

i was originally trying to make something that works on any website and on all the link+key combos, and for links without keys, too. would be cool to combine the two scripts somehow...

permalink

[-] kuretake | 1 points | Jul 06 2017 11:49:05

Yeah, mine is made to work only on reddit, and will run once for each "usertext-body" element. It assumes the link comes first, followed by the key. I'm sure this could be made more general, but since there's no actual standard for posting megalinks, that would be difficult.

permalink