| 1 |
window.addEventListener("load", function () { |
| 2 |
const url = new URL(window.location.href); |
| 3 |
const hasBlockInserter = url.searchParams.has("spblock_inserter"); |
| 4 |
const hasPatternLibrary = url.searchParams.has("sp_pcp_pattern_library"); |
| 5 |
|
| 6 |
if (!hasBlockInserter && !hasPatternLibrary) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
function tryScroll() { |
| 11 |
const headings = document.querySelectorAll(".block-editor-inserter__panel-title"); |
| 12 |
const smartPostHeading = Array.from(headings).find((h) => h.textContent.trim() === "SMART POST"); |
| 13 |
if (smartPostHeading) { |
| 14 |
smartPostHeading.scrollIntoView({ |
| 15 |
behavior: "smooth", |
| 16 |
block: "start", |
| 17 |
inline: "nearest", |
| 18 |
}); |
| 19 |
return true; |
| 20 |
} |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
function tryClick() { |
| 25 |
if (!wp.data.dispatch) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
const { dispatch } = wp.data; |
| 29 |
if (dispatch("core/editor")) { |
| 30 |
dispatch("core/editor").setIsInserterOpened(true); |
| 31 |
} else if (dispatch("core/edit-post")) { |
| 32 |
dispatch("core/edit-post").setIsInserterOpened(true); |
| 33 |
} |
| 34 |
// clear url. |
| 35 |
url.searchParams.delete("spblock_inserter"); |
| 36 |
history.replaceState(null, "", url.toString()); |
| 37 |
|
| 38 |
// Try to scroll every 100ms for up to 3 seconds |
| 39 |
let scrollAttempts = 0; |
| 40 |
const scrollInterval = setInterval(() => { |
| 41 |
scrollAttempts++; |
| 42 |
if (tryScroll() || scrollAttempts > 30) { |
| 43 |
clearInterval(scrollInterval); |
| 44 |
} |
| 45 |
}, 100); |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
// Track retry attempts for skip button |
| 51 |
let skipButtonAttempts = 0; |
| 52 |
const MAX_SKIP_ATTEMPTS = 20; // 10 seconds total (20 * 500ms) |
| 53 |
|
| 54 |
function getEditorDocuments() { |
| 55 |
// The block canvas may render inside iframe[name="editor-canvas"], |
| 56 |
// so search both the top document and the iframe document. |
| 57 |
const documents = [document]; |
| 58 |
const canvasIframe = document.querySelector('iframe[name="editor-canvas"]'); |
| 59 |
if (canvasIframe && canvasIframe.contentDocument) { |
| 60 |
documents.push(canvasIframe.contentDocument); |
| 61 |
} |
| 62 |
return documents; |
| 63 |
} |
| 64 |
|
| 65 |
function tryClickSkipButton() { |
| 66 |
const documents = getEditorDocuments(); |
| 67 |
|
| 68 |
// Try to find the Skip button in template selection overlay |
| 69 |
for (const doc of documents) { |
| 70 |
const skipButton = doc.querySelector(".sp-pcp-layout-modal-skip-button"); |
| 71 |
if (skipButton) { |
| 72 |
skipButton.click(); |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Alternative: find by text content "Skip" |
| 78 |
for (const doc of documents) { |
| 79 |
const allButtons = doc.querySelectorAll("span, button, .components-button"); |
| 80 |
for (const button of allButtons) { |
| 81 |
if (button.textContent.trim() === "Skip") { |
| 82 |
button.click(); |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// If not found yet and under max attempts, retry after delay |
| 89 |
if (skipButtonAttempts < MAX_SKIP_ATTEMPTS) { |
| 90 |
skipButtonAttempts++; |
| 91 |
setTimeout(() => { |
| 92 |
tryClickSkipButton(); |
| 93 |
}, 500); |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
function tryClickPatternButton() { |
| 100 |
const button = document.getElementById("smart-post-library-modal-button"); |
| 101 |
if (button) { |
| 102 |
button.click(); |
| 103 |
// clear url. |
| 104 |
url.searchParams.delete("sp_pcp_pattern_library"); |
| 105 |
history.replaceState(null, "", url.toString()); |
| 106 |
return true; |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
// Handle pattern library |
| 112 |
if (hasPatternLibrary) { |
| 113 |
let attempts = 0; |
| 114 |
const interval = setInterval(() => { |
| 115 |
attempts++; |
| 116 |
if (tryClickPatternButton() || attempts > 30) { |
| 117 |
clearInterval(interval); |
| 118 |
} |
| 119 |
}, 100); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Handle block inserter. |
| 124 |
let attempts = 0; |
| 125 |
const interval = setInterval(() => { |
| 126 |
attempts++; |
| 127 |
if (tryClick() || attempts > 20) { |
| 128 |
clearInterval(interval); |
| 129 |
} |
| 130 |
}, 100); |
| 131 |
}); |
| 132 |
|