gissues.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. {
  2. function deparam(query) {
  3. var match;
  4. var plus = /\+/g;
  5. var search = /([^&=]+)=?([^&]*)/g;
  6. var decode = function decode(s) {
  7. return decodeURIComponent(s.replace(plus, ' '));
  8. };
  9. var params = {};
  10. while (match = search.exec(query)) {
  11. params[decode(match[1])] = decode(match[2]);
  12. }
  13. return params;
  14. }
  15. function param(obj) {
  16. var parts = [];
  17. for (var name in obj) {
  18. if (obj.hasOwnProperty(name) && obj[name]) {
  19. parts.push(encodeURIComponent(name) + "=" + encodeURIComponent(obj[name]));
  20. }
  21. }
  22. return parts.join('&');
  23. }
  24. let script = document.currentScript;
  25. let attrs = {};
  26. for (let i = 0; i < script.attributes.length; i++) {
  27. let attribute = script.attributes.item(i);
  28. attrs[attribute.name.replace(/^data-/, '')] = attribute.value;
  29. }
  30. let canonicalLink = document.querySelector("link[rel='canonical']");
  31. attrs.url = canonicalLink ? canonicalLink.href : location.origin + location.pathname + location.search;
  32. attrs.origin = location.origin;
  33. attrs.pathname = location.pathname.length < 2 ? 'index' : location.pathname.substr(1).replace(/\.\w+$/, '');
  34. attrs.title = document.title;
  35. let descriptionMeta = document.querySelector("meta[name='description']");
  36. attrs.description = descriptionMeta ? descriptionMeta.content : '';
  37. let ogtitleMeta = document.querySelector("meta[property='og:title'],meta[name='og:title']");
  38. attrs['og:title'] = ogtitleMeta ? ogtitleMeta.content : '';
  39. document.head.insertAdjacentHTML('afterbegin', "<style>.Gissues{position:relative;box-sizing:border-box;width:100%;margin-left:auto;margin-right:auto;}.Gissues-frame{position:absolute;left:0;right:0;width:1px;min-width:100%;max-width:100%;height:100%;border:0;}</style>");
  40. let Origin = "https://cleve.gitee.io/gissues";
  41. let url = Origin + "/Gissues.html";
  42. script.insertAdjacentHTML('afterend', "<div class='Gissues'><iframe class='Gissues-frame' title='Comments' scrolling='no' src='" + url + "?" + (0, param)(attrs) + "'></iframe></div>");
  43. let container = script.nextElementSibling;
  44. script.parentElement.removeChild(script);
  45. addEventListener('message', function (event) {
  46. if (event.origin !== Origin) {
  47. return;
  48. }
  49. let data = event.data;
  50. if (data && data.type === 'resize' && data.height) {
  51. container.style.height = data.height + "px";
  52. }
  53. });
  54. }