Skip to content

Instantly share code, notes, and snippets.

@OnceUponALoop
Created June 8, 2020 16:30
Show Gist options
  • Select an option

  • Save OnceUponALoop/b45eb5abaeda578a4700fb5081da30d5 to your computer and use it in GitHub Desktop.

Select an option

Save OnceUponALoop/b45eb5abaeda578a4700fb5081da30d5 to your computer and use it in GitHub Desktop.
Pulp v3 Bookmarklet (PulpLinkify)

Converts Pulp v3 API urls to links I'm sure it can be more efficient but it's a quick hack for now.

Linkify Pulp Script

var pp = document.getElementsByClassName('prettyprint');

for (var i = 0; i < pp.length; i++){

  var matches = pp[i].getElementsByClassName('str');

  for(var k = 0; k < matches.length; k++){

    if ( matches[k].innerText.match(/("\/pulp\/api\/v3)/igm) )
    {
      var matchText, replacedText, replacePattern1, replacePattern2;
      
      // Get the text
      matchText = matches[k].innerText;
      
      // Add Pulp URL
      replacePattern1 = /(\/pulp\/api\/v3.*)/gim;
      replacedText = matchText.replace(replacePattern1, document.location.origin+'$1' );

      // Make URL a link
      replacePattern2 = /"(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])"/gim;
      replacedText = replacedText.replace(replacePattern2, '<a style="color: blue" href="$1" target="_blank">$1</a>');

      // Update the DOM
      matches[k].outerHTML = replacedText;
    };
  };
};

Escaped Bookmarklet

javascript:(function()%7Bvar%20pp%20%3D%20document.getElementsByClassName('prettyprint')%3Bfor%20(var%20i%20%3D%200%3B%20i%20%3C%20pp.length%3B%20i%2B%2B)%7Bvar%20matches%20%3D%20pp%5Bi%5D.getElementsByClassName('str')%3Bfor(var%20k%20%3D%200%3B%20k%20%3C%20matches.length%3B%20k%2B%2B)%7Bif%20(%20matches%5Bk%5D.innerText.match(%2F(%22%5C%2Fpulp%5C%2Fapi%5C%2Fv3)%2Figm)%20)%7Bvar%20matchText%2C%20replacedText%2C%20replacePattern1%2C%20replacePattern2%3BmatchText%20%3D%20matches%5Bk%5D.innerText%3BreplacePattern1%20%3D%20%2F(%5C%2Fpulp%5C%2Fapi%5C%2Fv3.*)%2Fgim%3BreplacedText%20%3D%20matchText.replace(replacePattern1%2C%20document.location.origin%2B'%241'%20)%3BreplacePattern2%20%3D%20%2F%22(%5Cb(https%3F%7Cftp)%3A%5C%2F%5C%2F%5B-A-Z0-9%2B%26%40%23%5C%2F%25%3F%3D~_%7C!%3A%2C.%3B%5D*%5B-A-Z0-9%2B%26%40%23%5C%2F%25%3D~_%7C%5D)%22%2Fgim%3BreplacedText%20%3D%20replacedText.replace(replacePattern2%2C%20'%3Ca%20style%3D%22color%3A%20blue%22%20href%3D%22%241%22%20target%3D%22_blank%22%3E%241%3C%2Fa%3E')%3Bmatches%5Bk%5D.outerHTML%20%3D%20replacedText%3B%7D%3B%7D%3B%7D%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment