0

I'm trying to do a simple fetch API call, but every time I do the call the browser is deleting the headers that I am passing through. The website gives me a 415 for i I am just trying to pass the content type and the secret. I removed some bits of the code for security but essentially here is the code below:

Edit: This is not a CORS issue, also I have tried running this in the background.js script and it doesn't work. Yes, the content.js script is needed for other functions not listed in this code.

Manafest.JSON

{ "permissions": ["tabs", "activeTab", "scripting", "notifications", "webRequest",
  "host_permissions": [
    "https://*.website.com/*"
  ],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["https://*.website.com/*"],
      "js": ["content.js"]
    }
  ],
}

Background.js

chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
  if (tab.url.indexOf('website.com/') > -1) {
    if (change.status === 'complete') {
      chrome.tabs.sendMessage(tabId, { 
        action: 'checkPage' 
      });
    }
  }
});

Content.js Fetch API Function

(() => {

    chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
        if (request.action === 'checkPage') {
            var url = 'myurl.com';
            var secret = 'mysecret';
            var data = {
                "randomdata": "data",
                }
            
                fetch(url, {
                    method: 'POST',
                    mode: 'no-cors',
                    headers: {
                        'X-CLIENT-SECRET': secret,
                        'Content-Type': 'application/json'
                      },
                    body: JSON.stringify(data),
                })
                .then(response => response.json())
                .then(data => {
                    console.log('Success:', data);
                })
                .catch((error) => {
                    console.error('Error:', error);
                });
        }
    });

})();

Austin Reed
  • 37
  • 1
  • 6

0 Answers0