|
1 | 1 | export function insertAd(proseSelector: string, adContainerId: string) { |
| 2 | + console.log("insertAd: Starting", { proseSelector, adContainerId }); |
2 | 3 | const prose = document.querySelector(proseSelector); |
3 | 4 | const adPlaceholder = document.getElementById(adContainerId); |
4 | 5 |
|
5 | 6 | // Get the ad content (the div with class 'in-article-ad-container' inside the placeholder) |
6 | 7 | const adContent = adPlaceholder?.firstElementChild; |
7 | 8 |
|
8 | | - if (!prose || !adPlaceholder || !adContent) return; |
| 9 | + if (!prose || !adPlaceholder || !adContent) { |
| 10 | + console.error("insertAd: Missing elements", { prose: !!prose, placeholder: !!adPlaceholder, content: !!adContent }); |
| 11 | + return; |
| 12 | + } |
9 | 13 |
|
10 | | - const headings = prose.querySelectorAll('h2'); |
11 | | - let primaryAdUsed = false; |
| 14 | + const headings = prose.querySelectorAll('h2, h3'); |
| 15 | + console.log("insertAd: Found headings", headings.length); |
12 | 16 |
|
13 | | - // Rule 1: Insert before 3rd h2 |
| 17 | + // Priority 1: Before 3rd Heading (index 2) |
| 18 | + // Matches CivicThesis logic (conceptually "Before 2nd section break" -> 3rd header) |
14 | 19 | if (headings.length >= 3) { |
| 20 | + console.log("insertAd: Inserting before 3rd heading"); |
15 | 21 | headings[2].insertAdjacentElement('beforebegin', adContent); |
16 | | - primaryAdUsed = true; |
17 | | - try { |
18 | | - ((window as any).adsbygoogle = (window as any).adsbygoogle || []).push({}); |
19 | | - } catch (e) { |
20 | | - console.error("AdSense push failed", e); |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - // Rule 2: Insert before last h2 if > 4 headings |
25 | | - // "more than 4 headings" usually means 5 or more. |
26 | | - if (headings.length > 5) { |
27 | | - const lastHeading = headings[headings.length - 1]; |
28 | | - |
29 | | - if (primaryAdUsed) { |
30 | | - // Clone the ad for the second slot |
31 | | - const adClone = adContent.cloneNode(true) as HTMLElement; |
32 | | - |
33 | | - // Reset the 'ins' element in the clone to ensure a fresh ad request |
34 | | - const ins = adClone.querySelector('ins'); |
35 | | - if (ins) { |
36 | | - ins.removeAttribute('data-adsbygoogle-status'); |
37 | | - ins.removeAttribute('data-ad-status'); |
38 | | - ins.innerHTML = ''; // Clear any existing content/iframe |
39 | | - } |
40 | | - |
41 | | - lastHeading.insertAdjacentElement('beforebegin', adClone); |
42 | | - |
43 | | - // Trigger ad load for the new slot |
| 22 | + setTimeout(() => { |
44 | 23 | try { |
| 24 | + console.log("insertAd: Pushing to adsbygoogle (P1)"); |
45 | 25 | ((window as any).adsbygoogle = (window as any).adsbygoogle || []).push({}); |
46 | 26 | } catch (e) { |
47 | 27 | console.error("AdSense push failed", e); |
48 | 28 | } |
49 | | - } else { |
50 | | - // Capture this edge case if logic changes, though currently covered by Rule 1 |
51 | | - lastHeading.insertAdjacentElement('beforebegin', adContent); |
52 | | - primaryAdUsed = true; |
| 29 | + }, 100); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Priority 2: Before 1st Heading (index 0) |
| 34 | + // Fallback for shorter articles with at least one header |
| 35 | + if (headings.length >= 1) { |
| 36 | + console.log("insertAd: Inserting before 1st heading"); |
| 37 | + headings[0].insertAdjacentElement('beforebegin', adContent); |
| 38 | + // Add a small delay to ensure DOM is updated and script can see the element |
| 39 | + setTimeout(() => { |
53 | 40 | try { |
| 41 | + console.log("insertAd: Pushing to adsbygoogle (P2)"); |
54 | 42 | ((window as any).adsbygoogle = (window as any).adsbygoogle || []).push({}); |
55 | 43 | } catch (e) { |
56 | 44 | console.error("AdSense push failed", e); |
57 | 45 | } |
58 | | - } |
| 46 | + }, 100); |
| 47 | + return; |
59 | 48 | } |
60 | 49 |
|
61 | | - // Fallback: Paragraphs (only if no ads inserted yet) |
62 | | - // The user said "also insert...". This implies the header rules function together. |
63 | | - // The paragraph rule is a fallback if "no heading tag is there" (from previous prompt). |
64 | | - // So if we inserted ANY ad in headers, we skip paragraphs. |
65 | | - if (!primaryAdUsed) { |
66 | | - const paragraphs = prose.querySelectorAll('p'); |
| 50 | + // Priority 3: Paragraph based fallback |
| 51 | + const paragraphs = prose.querySelectorAll('p'); |
| 52 | + console.log("insertAd: Fallback to paragraphs", paragraphs.length); |
67 | 53 |
|
68 | | - // Insert after 3rd paragraph |
69 | | - if (paragraphs.length >= 3) { |
70 | | - // Previous prompt said "after above 3rd paragraph" -> interpreted as after 3rd. |
71 | | - // Wait, "after above 3rd paragraph" is ambiguous. "insert after above 3rd paragraph" -> maybe "insert after the paragraph that is above the 3rd one" (i.e. 2nd)? |
72 | | - // Or "insert above 3rd paragraph" (before 3rd)? |
73 | | - // Code in Step 153 used `afterend` on paragraphs[2] (after 3rd). |
74 | | - // Let's stick to "After 3rd paragraph" as a reasonable default unless clarified. |
75 | | - // Actually, let's use 'beforebegin' on the 3rd to match the "above" sentiment of header rule? |
76 | | - // "if no heading tag is there then insert after above 3rd paragraph" |
77 | | - // "after above 3rd paragraph" -> literally could mean "after the 3rd paragraph". |
78 | | - // Let's stick to paragraphs[2].afterend (after 3rd). |
79 | | - paragraphs[2].insertAdjacentElement('afterend', adContent); |
80 | | - } |
81 | | - // Fallback: Append to end |
82 | | - else { |
83 | | - prose.appendChild(adContent); |
84 | | - } |
| 54 | + // Before 3rd paragraph |
| 55 | + if (paragraphs.length >= 3) { |
| 56 | + paragraphs[2].insertAdjacentElement('beforebegin', adContent); |
| 57 | + } |
| 58 | + // After 2nd paragraph |
| 59 | + else if (paragraphs.length >= 2) { |
| 60 | + paragraphs[1].insertAdjacentElement('afterend', adContent); |
| 61 | + } |
| 62 | + // After 1st paragraph |
| 63 | + else if (paragraphs.length >= 1) { |
| 64 | + paragraphs[0].insertAdjacentElement('afterend', adContent); |
| 65 | + } |
| 66 | + // End of content |
| 67 | + else { |
| 68 | + prose.appendChild(adContent); |
| 69 | + } |
| 70 | + |
| 71 | + setTimeout(() => { |
85 | 72 | try { |
| 73 | + console.log("insertAd: Pushing to adsbygoogle (Fallback)"); |
86 | 74 | ((window as any).adsbygoogle = (window as any).adsbygoogle || []).push({}); |
87 | 75 | } catch (e) { |
88 | 76 | console.error("AdSense push failed", e); |
89 | 77 | } |
90 | | - } |
| 78 | + }, 100); |
91 | 79 | } |
0 commit comments