0

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

有人可以帮忙吗?谢谢

4

1 回答 1

2

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

  • 授权必须包含在标头中,以 base64 编码并以“基本”为关键字。
  • 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);
    }
于 2020-03-26T20:41:23.917 回答