PageSourceSearch

https://www.metal-hammer.de/wp-content/plugins/asmb-facebook-embed-2023/facebook-embed-loader.js

js metal-hammer.de collected 2026-10-02 04:31:17 UTC 3,788 bytes, 125 lines download raw bytes

1function log(msg, ...args) {
2  console.log('[FB Embed Loader]', msg, ...args)
3}
4
5function injectFacebookSDK(callback) {
6  if (typeof FB !== 'undefined' && FB.XFBML?.parse) {
7    console.log('[FB Embed Loader] FB SDK already initialized')
8    callback?.()
9    return
10  }
11
12  // If script tag is in DOM but FB is not ready, wait and retry
13  const existing = document.querySelector('script[src*="connect.facebook.net/sdk.js"]')
14  if (existing && typeof FB === 'undefined') {
15    console.log('[FB Embed Loader] SDK script in DOM but FB not ready — retrying...')
16    waitForFB(callback)
17    return
18  }
19
20  // Otherwise: inject fresh SDK
21  const script = document.createElement('script')
22  script.src = 'https://connect.facebook.net/de_DE/sdk.js#xfbml=1&version=v9.0'
23  script.async = true
24  script.defer = true
25  script.onload = () => {
26    console.log('[FB Embed Loader] SDK loaded via inject')
27    waitForFB(callback)
28  }
29  document.body.appendChild(script)
30  console.log('[FB Embed Loader] Injected SDK script')
31}
32
33function waitForFB(callback, attempt = 0) {
34  if (typeof FB !== 'undefined' && FB.XFBML?.parse) {
35    console.log('[FB Embed Loader] FB now available')
36    callback?.()
37    return
38  }
39
40  if (attempt > 10) {
41    console.warn('[FB Embed Loader] FB SDK failed to initialize after waiting')
42    return
43  }
44
45  setTimeout(() => {
46    console.log(`[FB Embed Loader] Waiting for FB... attempt ${attempt + 1}`)
47    waitForFB(callback, attempt + 1)
48  }, 300)
49}
50
51function parseVisibleFacebookEmbeds() {
52  const embeds = document.querySelectorAll('.fb-post')
53  if (!embeds.length) {
54    log('[FB Embed Loader] No .fb-post found')
55    return
56  }
57
58  let parsed = 0
59  embeds.forEach((el, i) => {
60    const style = getComputedStyle(el)
61    const isVisible = style.display !== 'none' && el.offsetParent !== null
62    const isParsed = el.classList.contains('fb_iframe_widget')
63
64    if (isVisible && !isParsed) {
65      log(`[FB Embed Loader] Parsing visible embed #${i}`, el)
66      parsed++
67      injectFacebookSDK(() => {
68        if (typeof FB !== 'undefined' && FB.XFBML?.parse) {
69          FB.XFBML.parse()
70        }
71      })
72    } else {
73      log(`[FB Embed Loader] Embed #${i} is ${isVisible ? 'already parsed' : 'not visible'}, skipping`)
74    }
75  })
76
77  if (parsed === 0) {
78    log('[FB Embed Loader] No embeds needed parsing.')
79  }
80}
81
82// Watch DOM for new .fb-post elements after consent
83const observer = new MutationObserver((mutationsList) => {
84  for (const mutation of mutationsList) {
85    if (mutation.type === 'childList') {
86      const addedNodes = [...mutation.addedNodes]
87      const hasFacebookEmbed = addedNodes.some(node =>
88        node.nodeType === 1 &&
89        (node.matches?.('.fb-post') || node.querySelector?.('.fb-post'))
90      )
91
92      if (hasFacebookEmbed) {
93        log('[FB Embed Loader] New fb-post detected via MutationObserver')
94        parseVisibleFacebookEmbeds()
95      }
96    }
97  }
98})
99
100function observePlaceholderVisibility() {
101  const placeholders = document.querySelectorAll('.embed-placeholder__container[vendor="5ee91b9593fc094b59242e25"]')
102
103  placeholders.forEach((placeholder, i) => {
104    const observer = new MutationObserver((mutationsList) => {
105      for (const mutation of mutationsList) {
106        if (
107          mutation.attributeName === 'style' &&
108          placeholder.style.display === 'none'
109        ) {
110          log(`[FB Embed Loader] Placeholder #${i} hidden — triggering Facebook embed parsing`)
111          setTimeout(parseVisibleFacebookEmbeds, 100)
112        }
113      }
114    })
115
116    observer.observe(placeholder, { attributes: true, attributeFilter: ['style'] })
117  })
118}
119
120
121window.addEventListener('load', () => {
122  log('[FB Embed Loader] window.load triggered')
123  setTimeout(parseVisibleFacebookEmbeds, 1000)
124  observePlaceholderVisibility()
125})

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.