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

Tampermonkey breaks docx https://github.com/dolanmiu/docx #1600

Closed
m42e opened this issue Sep 30, 2022 · 3 comments
Closed

Tampermonkey breaks docx https://github.com/dolanmiu/docx #1600

m42e opened this issue Sep 30, 2022 · 3 comments

Comments

@m42e
Copy link

m42e commented Sep 30, 2022

(Please fill out the issue template with your details)

Expected Behavior

https://github.com/dolanmiu/docx works, even if some grants were present.

Actual Behavior

It works as long as no grants are present, as soon as there are @grants the example from here is no longer working

Specifications

  • Chromium: Version 105.0.5195.127
  • TM: 4.16.1
  • OS: Windows 11

Script

When replacing the line of the grant with @grant none it works.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @grant         GM_setValue
// @require      https://cdn.jsdelivr.net/npm/docx@7.5.0/build/index.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.3.0/exceljs.min.js
// @require      https://cdn.jsdelivr.net/npm/sweetalert2@11
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js
// ==/UserScript==

 function generate() {
        const doc = new docx.Document({
          sections: [{
            properties: {},
            children: [
              new docx.Paragraph({
                children: [
                  new docx.TextRun("Hello World"),
                  new docx.TextRun({
                    text: "Foo Bar",
                    bold: true,
                  }),
                  new docx.TextRun({
                    text: "\tGithub is the best",
                    bold: true,
                  }),
                ],
              }),
            ],
          }]
        });

        docx.Packer.toBlob(doc).then(blob => {
          console.log(blob);
          saveAs(blob, "example.docx");
          console.log("Document created successfully");
        });
      }
      function onKeydown(evt){
        if (evt.altKey && evt.keyCode == 84) { // ALt + t: download
        generate()
            evt.preventDefault();
        }
      }
document.addEventListener('keydown', onKeydown, true);
@derjanb
Copy link
Member

derjanb commented Sep 30, 2022

One of the required libs uses postMessage to emulate setImmediate and the checks the event target which is the original window but compares it to the sandbox window.

It's a bug, but it should rarely happen. Not sure whether I'm going to fix this.

Workaround: add this @require before the others:

// @require      data:application/javascript,window.setImmediate%20%3D%20window.setImmediate%20%7C%7C%20((f%2C%20...args)%20%3D%3E%20window.setTimeout(()%20%3D%3E%20f(args)%2C%200))%3B

@m42e
Copy link
Author

m42e commented Sep 30, 2022

The workaround is enough for me. Thanks a lot. Couldn't have figured that out by myself. Thanks.

@derjanb
Copy link
Member

derjanb commented Mar 9, 2023

The workaround is enough for me

OK, so I'll close this...

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

No branches or pull requests

2 participants