| 1 |
window.addEventListener("load", function () { |
| 2 |
const url = new URL(window.location.href); |
| 3 |
|
| 4 |
// ==================== |
| 5 |
// Pattern Library Handler |
| 6 |
// ==================== |
| 7 |
|
| 8 |
if (url.searchParams.has("sp_pcp_pattern_library")) { |
| 9 |
function tryClickPatternButton() { |
| 10 |
const patternButton = document.querySelector( |
| 11 |
"#smart-post-library-modal-button" |
| 12 |
); |
| 13 |
if (patternButton) { |
| 14 |
patternButton.click(); |
| 15 |
url.searchParams.delete("sp_pcp_pattern_library"); |
| 16 |
history.replaceState(null, "", url.toString()); |
| 17 |
return true; |
| 18 |
} |
| 19 |
return false; |
| 20 |
} |
| 21 |
|
| 22 |
// Try every 100ms for up to 2 seconds |
| 23 |
let patternAttempts = 0; |
| 24 |
const patternInterval = setInterval(() => { |
| 25 |
patternAttempts++; |
| 26 |
if (tryClickPatternButton() || patternAttempts > 20) { |
| 27 |
clearInterval(patternInterval); |
| 28 |
} |
| 29 |
}, 100); |
| 30 |
} |
| 31 |
|
| 32 |
// ==================== |
| 33 |
// Block Inserter Handler |
| 34 |
// ==================== |
| 35 |
|
| 36 |
if (!url.searchParams.has("spblock_inserter")) return; |
| 37 |
|
| 38 |
function tryScroll() { |
| 39 |
const headings = document.querySelectorAll(".block-editor-inserter__panel-title"); |
| 40 |
const smartPostHeading = Array.from(headings).find( |
| 41 |
(h) => h.textContent.trim() === "SMART POST" |
| 42 |
); |
| 43 |
if (smartPostHeading) { |
| 44 |
smartPostHeading.scrollIntoView({ |
| 45 |
behavior: "smooth", |
| 46 |
block: "start", |
| 47 |
inline: "nearest", |
| 48 |
}); |
| 49 |
return true; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
function tryClick() { |
| 55 |
const btn = document.querySelector(".editor-document-tools__inserter-toggle"); |
| 56 |
if (btn) { |
| 57 |
btn.click(); |
| 58 |
url.searchParams.delete("spblock_inserter"); |
| 59 |
history.replaceState(null, "", url.toString()); |
| 60 |
|
| 61 |
// Try to scroll every 100ms for up to 3 seconds |
| 62 |
let scrollAttempts = 0; |
| 63 |
const scrollInterval = setInterval(() => { |
| 64 |
scrollAttempts++; |
| 65 |
if (tryScroll() || scrollAttempts > 30) { |
| 66 |
clearInterval(scrollInterval); |
| 67 |
} |
| 68 |
}, 100); |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
// Try every 100ms for up to 2 seconds |
| 76 |
let attempts = 0; |
| 77 |
const interval = setInterval(() => { |
| 78 |
attempts++; |
| 79 |
if (tryClick() || attempts > 20) { |
| 80 |
clearInterval(interval); |
| 81 |
} |
| 82 |
}, 100); |
| 83 |
}); |
| 84 |
|