Tampermonkey GM_xmlhttpRequest无法正确发送请求

问题描述 投票:0回答:1

我正在尝试实现一个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标记,所以我对问题可能出在哪里很盲目...

有人可以帮忙吗?谢谢

jira greasemonkey tampermonkey jira-rest-api gm-xmlhttprequest
1个回答
0
投票

所以我想出了解决问题的答案,显然这是许多不同的细节:因此,

  • Authorization必须包含在标头中,并在base64上进行编码,并使用关键字“ Basic”。
  • User-Agent必须在标头上用任何虚拟字符串覆盖。
  • 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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.