Tampermonkey GM_xmlhttpRequest not sending Request properly

3.2k views Asked by At

I am trying to implement a tampermonkey script that triggers an API call to a Jira instance to create a ticket with some information found in the page I am on (on a different domain). Here's what I've tried:

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: `);
        }
} );

However, when I run createJiraTicket(), I am getting the following error:

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

I have established the correct @connect tags on the script, so I am pretty blind on where the problem may be...

Can someone help? Thanks

1

There are 1 answers

0
ggonmar On BEST ANSWER

So I came up with the answer to fix it, apparently it was a number of different details: So,

  • Authorization had to be included onto the headers, coded on base64 and keyworded "Basic".
  • User-Agent needs to be overriden on headers, with any dummy string.
  • overrideMimeType needed to be set to json.

That all made the trick. This was the working code.

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);
    }