statusmessage.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by logicp on 6/05/17.
  3. */
  4. (function($, Drupal, drupalSettings) {
  5. Drupal.behaviors.status= {
  6. attach: function (context, settings) {
  7. Drupal.AjaxCommands.prototype.generatePreview = function(ajax, response, status) {
  8. if (validateUrl(response.url)) {
  9. var cleanUrl = response.url.replace(/^http(s?):\/\//i, "");
  10. // console.log(cleanUrl);
  11. $.ajax({
  12. type: 'POST',
  13. url: '/statusmessage/generate-preview/build' ,
  14. data: {'data': cleanUrl},
  15. success: function (response) {
  16. // console.log(response.data);
  17. if (response.data != null) {
  18. var parser = new DOMParser();
  19. var doc = parser.parseFromString(response.data, "text/html");
  20. var markup = buildPreview(doc);
  21. var statusBlock = document.getElementById('block-statusblock');
  22. var oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  23. if (oldPreviewIframe !== null) {
  24. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  25. }
  26. previewIframe = document.createElement('iframe');
  27. previewIframe.classList.add('statusmessage-preview-iframe');
  28. statusBlock.appendChild(previewIframe);
  29. previewIframe.contentWindow.document.open();
  30. previewIframe.contentWindow.document.appendChild(markup)
  31. previewIframe.contentWindow.document.close();
  32. }
  33. }
  34. });
  35. }
  36. };
  37. function validateUrl(input) {
  38. return input.match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?"));
  39. }
  40. function buildPreview(doc) {
  41. var imgs = doc.querySelectorAll('img');
  42. var metaTags = doc.querySelectorAll('meta');
  43. var title = doc.querySelector('title');
  44. var markup;
  45. var description;
  46. var previewImage = null;
  47. imgs.forEach(function (img) {
  48. if (previewImage === null) {
  49. var imgClasses = img.classList;
  50. if (imgClasses.value.toLowerCase().indexOf('logo') || img.alt.toLowerCase().indexOf('logo') || img.title.toLowerCase().indexOf('logo') || img.src.toLowerCase().indexOf('logo')) {
  51. previewImage = img;
  52. }
  53. }
  54. });
  55. metaTags.forEach(function (metaTag) {
  56. if (metaTag.name == 'description') {
  57. description = metaTag.content;
  58. }
  59. });
  60. console.dir(description);
  61. console.dir(previewImage.src);
  62. var outer = document.createElement('div');
  63. outer.className = 'statusmessage-preview';
  64. var titlemarkup = document.createElement('h4');
  65. titlemarkup.innerHTML = title.innerHTML;
  66. var descmarkup = document.createElement('p');
  67. descmarkup.innerText = description;
  68. var imgmarkup = document.createElement('img');
  69. imgmarkup.src = previewImage.src;
  70. var wrapper = document.createElement('div');
  71. wrapper.appendChild(titlemarkup);
  72. wrapper.appendChild(descmarkup);
  73. wrapper.appendChild(imgmarkup);
  74. return wrapper;
  75. }
  76. }
  77. };
  78. })(jQuery, Drupal, drupalSettings);