Açıklama Yok

common.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. function appendFilesToHead(files, script) {
  3. if (!files) {
  4. return script;
  5. }
  6. return files.reduceRight((file, combinedScript) => {
  7. const { rel, type, href } = file;
  8. return `
  9. var link = document.createElement('link');
  10. link.rel = '${rel}';
  11. link.type = '${type}';
  12. link.href = '${href}';
  13. document.head.appendChild(link);
  14. ${combinedScript}
  15. `;
  16. }, script);
  17. }
  18. function appendStylesToHead(styles, script) {
  19. if (!styles) {
  20. return script;
  21. }
  22. // Escape any single quotes or newlines in the CSS with .replace()
  23. const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  24. return `
  25. var styleElement = document.createElement('style');
  26. var styleText = document.createTextNode('${escaped}');
  27. styleElement.appendChild(styleText);
  28. document.head.appendChild(styleElement);
  29. ${script}
  30. `;
  31. }
  32. function getScript(props, baseScript, iframeBaseScript) {
  33. const { hasIframe, files, customStyle } = props;
  34. let script = hasIframe ? iframeBaseScript : baseScript;
  35. script = files ? appendFilesToHead(files, baseScript) : baseScript;
  36. script = customStyle ? appendStylesToHead(customStyle, script) : script;
  37. return script;
  38. }
  39. function onHeightUpdated(height, props) {
  40. props.onHeightUpdated && props.onHeightUpdated(height);
  41. }
  42. const domMutationObserveScript =
  43. `
  44. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  45. var observer = new MutationObserver(updateHeight);
  46. observer.observe(document, {
  47. subtree: true,
  48. attributes: true
  49. });
  50. `;
  51. export { getScript, onHeightUpdated, domMutationObserveScript };