Tampermonkey GM_xmlhttpRequest 未正确发送请求

Tampermonkey GM_xmlhttpRequest not sending Request properly

我正在尝试实施一个 tampermonkey 脚本,该脚本会触发对 Jira 实例的 API 调用,以创建一个票证,其中包含我所在页面(在不同域上)中找到的一些信息。 这是我尝试过的:

async function createJiraTicket() {
      let elements = await obtainThingsForCreatingJiraTicket();
      let createJiraTicketUrl = `https://${jiraDomain}/rest/api/latest/issue`;
      let requestDataForCreation =`
      {
        "fields": {
            "project": { "key": "${elements.project}" },
            "summary": "${elements.title}",
            "description": "nothing",
            "issuetype": {"name": "Issue" }
           }
      }`;
    GM_xmlhttpRequest ( {
        method:     "POST",
        url:        `${http}://${createJiraTicketUrl}`,
        user: 'user:pwd',      // also tried user:'user', password:'pwd',
        data:       requestDataForCreation,
        header: 'Accept: application/json',
        dataType: 'json',
        contentType: 'application/json',
        onload:     function (response) {
           jiraTicketLog(`[Ajax] Response from Ajax call Received: `);
        }
} );

但是,当我 运行 createJiraTicket() 时,出现以下错误:

Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
    at <anonymous>:1:7

我已经在脚本上建立了正确的@connect 标签,所以我对问题可能出在哪里一无所知...

有人可以帮忙吗? 谢谢

所以我想出了修复它的答案,显然是一些不同的细节: 所以,

  • 授权 必须包含在 headers 中,使用 base64 编码并以关键字 "Basic".
  • User-Agent 需要用任何虚拟字符串覆盖 headers。
  • overrideMimeType 需要设置为 json。

这一切都成功了。 这是工作代码。

let createJiraTicketUrl = `//${jiraDomain}/rest/api/latest/issue`;
let authentication = btoa('admin:foobar1');

GM.xmlHttpRequest({
    method: "POST",
    headers: {
        'Accept': 'application/json',
        "Content-Type": "application/json",
        "Authorization": `Basic ${authentication}`,
        "User-Agent": "lolol"
    },
    url: `${http}:${createJiraTicketUrl}`,
    data: JSON.stringify(requestDataForCreation),
    dataType: 'json',
    contentType: 'application/json',
    overrideMimeType: 'application/json',
    onload: function (response) {
        let url = `${http}://${jiraDomain}/browse/${JSON.parse(response.response).key}`;
        log(`${JSON.parse(response.response).key} ticket created: ${url}`);
        openJiraTicket(url);
    }