momocow / webpack-userscript

A Webpack plugin for userscript projects. 🙈
https://cow.moe/webpack-userscript/
MIT License
200 stars 21 forks source link

multiple grant lines are ignored #15

Closed Gribbs closed 4 years ago

Gribbs commented 4 years ago

Really useful plugin! I've noticed a bug when you need multiple @grant lines like this


new WebpackUserscript({
      headers: {
        version:
          mode === "development" ? `[version]-build.[buildNo]` : `[version]`,
        require: "https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js",
        grant: "GM.xmlHttpRequest",
        grant: "GM_xmlhttpRequest",
        grant: "none",
        match: process.env.TAMPERMONKEY_MATCH_URL_VALUE,
        description:
          "adds a button  to the page",
        ["run-at"]: "document-end"
      },
      pretty: true
    })

The first 2 grant lines end up overridden by the last grant line resulting in a greasemonkey header like

// ==UserScript==
// @name        Name
// @version     1.0.0-build.1
// @description adds a button to the page
// @match       https://myurl/*
// @require     https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==
momocow commented 4 years ago

@Gribbs That's as expected because duplicated keys inside an object are not allowed in JavaScript (nothing about this plugin), only the last value of the duplicated keys is passed into the constructor.

Provide an array as the value instead since the value of a HeaderObject can be a string or an array of strings. (See HeaderObject)

{
    version:
      mode === "development" ? `[version]-build.[buildNo]` : `[version]`,
    require: "https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js",
    grant: [
      "GM.xmlHttpRequest",
      "GM_xmlhttpRequest",
      "none"
    ],
    match: process.env.TAMPERMONKEY_MATCH_URL_VALUE,
    description:
      "adds a button  to the page",
    "run-at": "document-end"
  },
  pretty: true
}
Gribbs commented 4 years ago

Awesome! Thanks for the clarification and quick action

momocow commented 4 years ago

Feel free to reopen the issue or file a new one if there are any questions or bugs. Happy scripting!