mutation-observer.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (function () {
  2. function loadAsset(path, type) {
  3. if (path) {
  4. if (window.tauri !== void 0) {
  5. window.tauri.loadAsset(path, type)
  6. } else {
  7. if (window.__TAURI_INIT_HOOKS === void 0) {
  8. window.__TAURI_INIT_HOOKS = []
  9. }
  10. window.__TAURI_INIT_HOOKS.push(function () {
  11. window.tauri.loadAsset(path, type)
  12. })
  13. }
  14. }
  15. }
  16. var observer = new MutationObserver(mutation => {
  17. mutation.forEach(function (mutationRecord) {
  18. var addedNodes = mutationRecord.addedNodes
  19. addedNodes.forEach(function (node) {
  20. if (node.nodeType === 1) {
  21. if (node.tagName === 'SCRIPT') {
  22. node.onload = node.onerror = null
  23. loadAsset(node.src)
  24. } else if (node.tagName === 'LINK') {
  25. if (node.type === 'text/css' || (node.href && node.href.endsWith('.css'))) {
  26. loadAsset(node.href, 'stylesheet')
  27. }
  28. }
  29. }
  30. })
  31. })
  32. })
  33. <% if (target === 'body') { %>
  34. var target = document.documentElement
  35. <% } else { %>
  36. var target = document.head
  37. <% } %>
  38. observer.observe(target, {
  39. childList: true,
  40. subtree: true
  41. })
  42. })()