script-lab icon indicating copy to clipboard operation
script-lab copied to clipboard

'[object Object] is not valid JSON' - even with basic script lab example snippet

Open richie5um opened this issue 1 year ago • 3 comments

Bug Report

All scripts are causing endless errors in the console when loading - even the base example.

Environment

  • Host: Word (office online)
  • OS: macOS
  • Browser: Edge (latest version)

Expected behavior

Run the basic script lab example without issues.

Actual behavior

CleanShot 2024-09-10 at 12 13 15

Note: I only (currently) see this on office.com/word, and not Word Desktop to MacOS.

Steps to Reproduce

  1. Use the simplest script lab example
  2. Run it
  3. See endless stream of the json error (see screenshot)

Screenshot

See above

richie5um avatar Sep 10 '24 11:09 richie5um

Me too. Using the web version of Word. I also experimented with the desktop but problems there too but the failures are silent.

image



[ERROR]: Error:
{
    "stack": "SyntaxError: Unexpected token 's', \"setImmedia\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at n (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403649)\n    at undefined (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403919)\n    at Rh.onWindowMessage (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5404223)",
    "message": "Unexpected token 's', \"setImmedia\"... is not valid JSON"
}

[ERROR]: Error:
{
    "stack": "SyntaxError: Unexpected token 's', \"setImmedia\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at n (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403649)\n    at undefined (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403919)\n    at Rh.onWindowMessage (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5404223)",
    "message": "Unexpected token 's', \"setImmedia\"... is not valid JSON"
}

The code

// Import Word API
async function run() {
  await Word.run(async (context) => {
    // Get all paragraphs in the document
    const paragraphs = context.document.body.paragraphs;
    paragraphs.load("text");

    await context.sync();

    // Process in batches of, say, 50 paragraphs at a time
    let batchSize = 50;
    let totalParagraphs = paragraphs.items.length;
    for (let i = 0; i < totalParagraphs; i += batchSize) {
      let end = Math.min(i + batchSize, totalParagraphs);
      let batch = paragraphs.items.slice(i, end);
      let htmlContent = "<html><body>";

      batch.forEach((paragraph, index) => {
        if (index > 4)
         return;
        console.log(`Processing paragraph ${i + index + 1}`);
        htmlContent += `<p>${paragraph.text}</p>`;
      });

      htmlContent += "</body></html>";

      // Display the current batch's result or save it in some way
      console.log(`Processed batch ${i + 1} to ${end}`);
    }
  });
}

// Attach the run function to the ribbon or button click
(async () => {
  try {
    await run();
  } catch (error: any) {
    console.error('Error:', error);
  }
})();

BobFrankston avatar Sep 10 '24 14:09 BobFrankston

I did a bit of digging and I think the problem is that the scriptlab code assumes all browser message events have the same format. This bit of the code assumes all message events have 'event.data' as a string, which isn't gonna be the case with anything else on the page that is sending messages.

CleanShot 2024-09-11 at 20 05 50

richie5um avatar Sep 11 '24 19:09 richie5um

Thanks. This is why I make a lot of use of "?. ".

When I run on the desktop, I don't see the errors. It just fails silently. At least, I assume that's why I don't see the message and don't see the download button.

BobFrankston avatar Sep 11 '24 20:09 BobFrankston

Closing all issues since this repo is being archived and no longer maintained.

ElizabethSamuel-MSFT avatar Mar 14 '25 15:03 ElizabethSamuel-MSFT