0

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

ggonmar
  • 760
  • 1
  • 7
  • 28
  • That error doesn't seem related to your code. See the [documentation](https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest): 1) no `header` in gmxhr, it's `headers` and it's an object like `headers: {'foo': 'bar'}`, 2) not `user` but `username` and `password`, 3) It's safer to use JSON.stringify with a literal object instead of constructing a JSON string manually because some of the interpolated properties may need escaping. – wOxxOm Mar 26 '20 at 06:45
  • I implemented those changes, as well as tried with GM.xmlHttpRequest instead of GM_xmlhttpRequest. No luck in any of the attempts :/ – ggonmar Mar 26 '20 at 14:37
  • 2
    Open the background page of your userscript manager in devtools ([instructions](https://stackoverflow.com/a/38920982), if needed) and inspect the actual request performed for your script in devtools `network` tab. – wOxxOm Mar 26 '20 at 14:43

1 Answers1

2

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);
    }
ggonmar
  • 760
  • 1
  • 7
  • 28