Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
richie5um opened this issue Sep 10, 2024 · 3 comments

Comments

@richie5um
Copy link

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

@BobFrankston
Copy link

BobFrankston commented Sep 10, 2024

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);
  }
})();

@richie5um
Copy link
Author

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

@BobFrankston
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants