fixlinks.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. $(function() {
  2. var objects = $(document).find('object');
  3. var pathname = location.pathname;
  4. var coursePath = pathname.substr(0, pathname.indexOf('/courses/'));
  5. var iconPath = location.protocol + '//' + location.host+ coursePath + '/main/img/';
  6. var url = "http://"+location.host + coursePath+"/courses/proxy.php?";
  7. objects.each(function (value, obj) {
  8. var openerId = this.id +'_opener';
  9. var link = '<a id="'+openerId+'" href="#">If video does not work, try clicking here.</a>';
  10. var embed = $("#"+this.id).find('embed').first();
  11. var hasHttp = embed.attr('src').indexOf("http");
  12. if (hasHttp < 0) {
  13. return true;
  14. }
  15. var height = embed.attr('height');
  16. var width = embed.attr('width');
  17. var src = embed.attr('src').replace('https', 'http');
  18. var completeUrl = url + 'width='+embed.attr('width')+
  19. '&height='+height+
  20. '&id='+this.id+
  21. '&flashvars='+encodeURIComponent(embed.attr('flashvars'))+
  22. '&src='+src+
  23. '&width='+width;
  24. var result = $("#"+this.id).find('#'+openerId);
  25. if (result.length == 0) {
  26. $("#" + this.id).append('<br />' + link);
  27. $('#' + openerId).click(function () {
  28. var window = window.open(completeUrl, "Video", "width=" + width + ", " + "height=" + height + "");
  29. window.document.title = 'Video';
  30. });
  31. }
  32. });
  33. var iframes = $(document).find('iframe');
  34. iframes.each(function (value, obj) {
  35. var randLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
  36. var uniqid = randLetter + Date.now();
  37. var openerId = uniqid +'_opener';
  38. var link = '<a id="'+openerId+'" class="generated" href="#">Open website.<img width="16px" src="'+iconPath+'link-external.png "/></a>';
  39. var embed = $(this);
  40. var height = embed.attr('height');
  41. var width = embed.attr('width');
  42. var src = embed.attr('src');
  43. var n = src.indexOf("youtube.com");
  44. if (n > 0) {
  45. return true;
  46. }
  47. var completeUrl = url + 'width='+embed.attr('width')+
  48. '&height='+height+
  49. '&type=iframe'+
  50. '&id='+uniqid+
  51. '&src='+src+
  52. '&width='+width;
  53. var result = $(this).find('#'+openerId);
  54. if (result.length == 0) {
  55. if (embed.next().attr('class') != 'generated') {
  56. $(this).prepend(link + '<br />');
  57. $('#' + openerId).click(function () {
  58. width = 1280;
  59. height = 640;
  60. var win = window.open(completeUrl, "Video", "width=" + width + ", " + "height=" + height + "");
  61. win.document.title = 'Video';
  62. });
  63. }
  64. }
  65. });
  66. var anchors = $(document).find('a').not('.generated');
  67. anchors.each(function (value, obj) {
  68. if ($(this).next().attr('class') != 'generated' ) {
  69. var content = $(this).html();
  70. content = content.replace('<br />', '');
  71. content = content.replace('<br>', '');
  72. content = $.trim(content);
  73. if (content == '') {
  74. return true;
  75. }
  76. if ($(this).attr('href')) {
  77. var hasLocalhost = $(this).attr('href').indexOf(location.host);
  78. if (hasLocalhost > 0) {
  79. return true;
  80. }
  81. var hasJs = $(this).attr('href').indexOf('javascript');
  82. if (hasJs >= 0) {
  83. return true;
  84. }
  85. }
  86. if ($(this).attr('class')) {
  87. var hasAccordion = $(this).attr('class').indexOf('accordion-toggle');
  88. if (hasAccordion >= 0) {
  89. return true;
  90. }
  91. }
  92. var src = $(this).attr('href');
  93. src = url+'&type=link&src='+src;
  94. src = src.replace('https', 'http');
  95. $(this).attr('href', src);
  96. $(this).attr('target', '_blank');
  97. var myAnchor = $('<a><img width="16px" src="'+iconPath+'link-external.png "/></a>').attr("href", src).attr('target', '_blank').attr('class', 'generated');
  98. $(this).after(myAnchor);
  99. $(this).after('-');
  100. }
  101. });
  102. });