interact-update.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <script>
  2. /**
  3. * To auto-embed hub URLs in interact links if given in a RESTful fashion
  4. */
  5. function getJsonFromUrl(url) {
  6. var query = url.split('?');
  7. if (query.length < 2) {
  8. // No queries so just return false
  9. return false;
  10. }
  11. query = query[1];
  12. // Collect REST params into a dictionary
  13. var result = {};
  14. query.split("&").forEach(function(part) {
  15. var item = part.split("=");
  16. result[item[0]] = decodeURIComponent(item[1]);
  17. });
  18. return result;
  19. }
  20. // Parse a Binder URL, converting it to the string needed for JupyterHub
  21. function binder2Jupyterhub(url) {
  22. newUrl = {};
  23. console.log(url)
  24. parts = url.split('v2/gh/')[1];
  25. // Grab the base repo information
  26. repoinfo = parts.split('?')[0];
  27. var [org, repo, ref] = repoinfo.split('/');
  28. newUrl['repo'] = ['https://github.com', org, repo].join('/');
  29. newUrl['branch'] = ref
  30. // Grab extra parameters passed
  31. params = getJsonFromUrl(url);
  32. if (params['filepath'] !== undefined) {
  33. newUrl['subPath'] = params['filepath']
  34. }
  35. return jQuery.param(newUrl);
  36. }
  37. // Filter out potentially unsafe characters to prevent xss
  38. function safeUrl(url)
  39. {
  40. return String(encodeURIComponent(url))
  41. .replace(/&/g, '&amp;')
  42. .replace(/"/g, '&quot;')
  43. .replace(/'/g, '&#39;')
  44. .replace(/</g, '&lt;')
  45. .replace(/>/g, '&gt;');
  46. }
  47. function addParamToInternalLinks(hub) {
  48. var links = $("a").each(function() {
  49. var href = this.href;
  50. // If the link is an internal link...
  51. if (href.search("{{ site.url }}") !== -1 || href.startsWith('/') || href.search("127.0.0.1:") !== -1) {
  52. // Assume we're an internal link, add the hub param to it
  53. var params = getJsonFromUrl(href);
  54. if (params !== false) {
  55. // We have REST params, so append a new one
  56. params['hub'] = hub;
  57. } else {
  58. // Create the REST params
  59. params = {'hub': hub};
  60. }
  61. // Update the link
  62. var newHref = href.split('?')[0] + '?' + jQuery.param(params);
  63. this.setAttribute('href', decodeURIComponent(newHref));
  64. }
  65. });
  66. return false;
  67. }
  68. // Update interact links
  69. function updateInteractLink() {
  70. // hack to make this work since it expects a ? in the URL
  71. rest = getJsonFromUrl("?" + location.search.substr(1));
  72. hubUrl = rest['hub'];
  73. if (hubUrl !== undefined) {
  74. // Sanitize the hubUrl
  75. hubUrl = safeUrl(hubUrl);
  76. // Add HTTP text if omitted
  77. if (hubUrl.indexOf('http') < 0) {hubUrl = 'http://' + hubUrl;}
  78. link = $("a.interact-button")[0];
  79. if (link !== undefined) {
  80. // Update the interact link URL
  81. var href = link.getAttribute('href');
  82. var hub_type = '{{ site.hub_type }}';
  83. if (hub_type === 'binder') {
  84. // If binder links exist, we need to re-work them for jupyterhub
  85. if (hubUrl.indexOf('http%3A%2F%2Flocalhost') > -1) {
  86. // If localhost, assume we're working from a local Jupyter server and remove `/hub`
  87. first = [hubUrl, 'git-sync'].join('/')
  88. } else {
  89. first = [hubUrl, 'hub', 'user-redirect', 'git-sync'].join('/')
  90. }
  91. href = first + '?' + binder2Jupyterhub(href);
  92. } else {
  93. // If JupyterHub links, we only need to replace the hub url
  94. href = href.replace("{{ site.hub_url }}", hubUrl);
  95. if (hubUrl.indexOf('http%3A%2F%2Flocalhost') > -1) {
  96. // Assume we're working from a local Jupyter server and remove `/hub`
  97. href = href.replace("/hub/user-redirect", "");
  98. }
  99. }
  100. link.setAttribute('href', decodeURIComponent(href));
  101. // Add text after interact link saying where we're launching
  102. hubUrlNoHttp = decodeURIComponent(hubUrl).replace('http://', '').replace('https://', '');
  103. $("a.interact-button").after($('<div class="interact-context">on ' + hubUrlNoHttp + '</div>'));
  104. }
  105. // Update internal links so we retain the hub url
  106. addParamToInternalLinks(hubUrl);
  107. }
  108. }
  109. runWhenDOMLoaded(updateInteractLink)
  110. document.addEventListener('turbolinks:load', updateInteractLink)
  111. </script>