Skip to content

Invoking a script gives multiple errors #24

@venvaneless

Description

@venvaneless

When invoking my small script:

// Live Preview copy flair: active copy icon
(() => {
  const PULSE_MS = 1300;

  const isSuccess = (el) => {
    // Obsidian typically flips aria-label to "Copied" and/or sets --text-success
    const aria = (el.getAttribute("aria-label") || "").toLowerCase();
    const style = el.getAttribute("style") || "";
    return aria.includes("copied") || /--text-success/i.test(style);
  };

  const flashCopied = (btn) => {
    if (btn.classList.contains("is-copied")) return;

    // Suppress the top tooltip
    const prevTitle = btn.getAttribute("title");
    const prevAria = btn.getAttribute("aria-label");
    btn.removeAttribute("title");
    btn.removeAttribute("aria-label");

    // Trigger CSS icon + animation
    btn.classList.add("is-copied");

    // Restore everything after pulse
    setTimeout(() => {
      btn.classList.remove("is-copied");
      if (prevTitle != null) btn.setAttribute("title", prevTitle);
      if (prevAria != null) btn.setAttribute("aria-label", prevAria);
    }, PULSE_MS);
  };

  const bindFlair = (btn) => {
    if (btn.dataset._lpCopyBound === "1") return;
    btn.dataset._lpCopyBound = "1";

    // 1) React immediately on click for instant feedback
    btn.addEventListener("click", () => {
      // Let Obsidian handle the actual copying;
      // we handle only the visual state.
      flashCopied(btn);
    });

    // 2) Also observe Obsidian's own success toggle (tooltip/inline style)
    const attrObserver = new MutationObserver(() => {
      if (isSuccess(btn)) flashCopied(btn);
    });
    attrObserver.observe(btn, {
      attributes: true,
      attributeFilter: ["aria-label", "style", "class", "title"],
    });
  };

  const scan = () => {
    document
      .querySelectorAll(".markdown-source-view.mod-cm6 .code-block-flair")
      .forEach(bindFlair);
  };

  // Initial pass + watch for re-renders
  scan();
  const mo = new MutationObserver(scan);
  mo.observe(document.body, { childList: true, subtree: true });

  console.info("LP copy flair icon");
})();

I get this error in the console

CustomStackTraceError: An unhandled error occurred executing async operation
    at Kv.afterLoad (plugin:fix-require-modules:1276:127)
    at async eval (plugin:fix-require-modules:358:154)
    at async addErrorHandler (plugin:fix-require-modules:356:908)
    at --- convertAsyncToSync --- (0)
    at t.<anonymous> (app.js:1:2528893)
    at app.js:1:250072
    at Object.next (app.js:1:250177)
    at a (app.js:1:248895)
    at --- Caused by: --- (0)
    at --- TypeError: Px.invoke is not a function --- (0)
    at invokeStartupScript (plugin:fix-require-modules:1301:2694)
    at async Kv.onLayoutReady (plugin:fix-require-modules:1302:1032)
    at async Kv.onLayoutReadyBase (plugin:fix-require-modules:1276:197)
    at async addErrorHandler (plugin:fix-require-modules:356:908)
printError @ plugin:fix-require-modules:68
handleAsyncError @ plugin:fix-require-modules:68
tryTrigger @ plugin:fix-require-modules:62
trigger @ plugin:fix-require-modules:62
emitAsyncErrorEvent @ plugin:fix-require-modules:62
addErrorHandler @ plugin:fix-require-modules:356
await in addErrorHandler
invokeAsyncSafely @ plugin:fix-require-modules:358
eval @ plugin:fix-require-modules:358
(anonymous) @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
(anonymous) @ app.js:1
y @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
(anonymous) @ app.js:1
y @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
(anonymous) @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1
Promise.then
l @ app.js:1
a @ app.js:1

I only learn just now Javascript so I'm not well versed yet what might cause it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions