【问题标题】:Tampermonkey GM_xmlhttpRequest not sending Request properlyTampermonkey GM_xmlhttpRequest 未正确发送请求
【发布时间】:2020-07-06 14:01:29
【问题描述】:

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

有人可以帮忙吗? 谢谢

【问题讨论】:

  • 该错误似乎与您的代码无关。见documentation:1)gmxhr中没有header,它是headers,它是一个像headers: {'foo': 'bar'}这样的对象,2)不是user而是usernamepassword,3)使用JSON更安全.stringify 使用文字对象而不是手动构造 JSON 字符串,因为某些内插属性可能需要转义。
  • 我实现了这些更改,并尝试使用 GM.xmlHttpRequest 而不是 GM_xmlhttpRequest。任何尝试都没有运气:/
  • 在 devtools 中打开用户脚本管理器的后台页面(instructions,如果需要)并在 devtools network 选项卡中检查为您的脚本执行的实际请求。

标签: jira greasemonkey tampermonkey jira-rest-api gm-xmlhttprequest


【解决方案1】:

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

  • 授权必须包含在标头中,以 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);
    }

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2012-07-28
    • 2018-07-14
    • 2020-04-24
    • 2018-05-14
    • 2018-05-26
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多