0

I know there is already stuff about that. i'm on it for several days and read and tried before posting.

1: No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API describes a relevant issue according to my problem. But I can't solve it. I'm on a chrome extension development that performs an ajax request on a dictionary web server and injects some css/html in the current page, to display a tooltip with some data related to the user selection. It works really fine with the 'MOESIF Cors' extension ON in my browser. I want it to work without this extension, with some javascript tweaks or manifest.json good parameters. I'm stuck. I had CORS violation because of the server (I've no access to the server and can't modify its response headers) Access-control-allow-origin policy. I've tried a lot of request headers combinations in my $.ajax/jquery GET-request but nothing works. Access to XMLHttpRequest at 'https://dictionary.xxx.ie/dictionary/english/government%20' from origin 'https://en.wikipedia.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. According to the article cited above, I've tried to set 'data-type': to 'jsonp' and that replaced the cors policy violation by another violation : Refused to load the script 'https://dictionary.xxx.ie/dictionary/english/publication?callback=jQuery3600056231503718366715_1615940095416&_=1615940095418' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

There must be a quick fix to this as the MOESIF extension works so fine : I tried all these headers : MOESIF Extension but it doesn't work. I sandboxed my script in the manifest.json after reading the docs (it seems to be more lax) but it hasn't done any better. I'm thinking of embedding a javascript proxy in my extension if such a thing is possible ! Any clue ? thanks for reading.

Manifest.json

{
  "manifest_version": 3,
  "name": "Not a translator",
  "version": "1.0",
  "default_locale": "en",
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": [
    "http://dictionary.xxx.ie/*",
    "https://dictionary.xxx.ie/*"
  ],
  "permissions": ["storage", "activeTab", "contextMenus", "scripting"],
  "sandbox": {
     "pages": ["chrome-extension://afeenkbblogndiamnfkocnomlppheomb/jquery.min.js"]
   },
   "content_security_policy": {
        "sandbox":"sandbox allow-scripts; script-src-elem chrome-extension://afeenkbblogndiamnfkocnomlppheomb/jquery.min.js 'unsafe-inline' 'unsafe-eval'; child-src 'self';"
  },
  "content_scripts": 
   [{
     "matches": ["http://*/*", "https://*/*"],
     "css": ["tooltip.css"],
     "run_at": "document_start",
     "js": ["tooltip.js", "jquery.min.js"]
   }],
  "externally_connectable": {
    "ids": ["afeenkbblogndiamnfkocnomlppheomb"],
    "matches": ["https://localhost/*"],
    "accepts_tls_channel_id": false
  },
  "action": {
    "default_title": "__MSG_tooltip__",
    "default_popup": "popup.htm",
    "default_icon": {
      "16": "/images/get_started16.png",
      "32": "/images/get_started32.png",
      "48": "/images/get_started48.png",
      "128": "/images/get_started128.png"
    }
  },
  "icons": {
    "16": "/images/get_started16.png",
    "32": "/images/get_started32.png",
    "48": "/images/get_started48.png",
    "128": "/images/get_started128.png"
  },
  "options_page": "options.htm"

a part of my jquery request from background.js (very large, can't get a runnable version) :

 var url1 = "https://dictionary.xxx.ie/dictionary/english/" +
                      encodeURIComponent(text);
       $.ajax({
            method: "GET",
            url: url1,
            crossDomain:true,
            dataType: "jsonp",
            data: {}     
        })...

When I inspect the headers of my request I sometime get a 403 forbidden. when I put the 'jsonp' header I've got some 'provisional headers' and nothing else. I think the POST or OPTIONS method used for jsonp gets me some response headers that ban further requests. I don't know which header I can add to solve this. Jquery doesn't allow me to pass a 'Origin': 'http://localhost' for example (for vulnerability reasons).

f b
  • 21
  • 1
  • 5

1 Answers1

1
  1. Remove content_security_policy and sandbox from manifest.json, they won't help here.
  2. Remove jQuery and its calls from background.js, it won't work in ManifestV3 service worker because jQuery is based on DOM things like XMLHttpRequest that aren't present in a service worker.

Solution

  • in the background script: use fetch .

  • in the content script, to make a cross-origin request: send a message to the background, which will make a fetch request and send the results back, example.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Ok...right now in background.js I'm using a contextMenus.onclicked.addListener invoking an injectedFunction that performs the fetch : it's the same CORS violation with fetch like this. – f b Mar 17 '21 at 10:54
  • `injectedFunction` will be a `content script` so the second option in the answer's solution applies. – wOxxOm Mar 17 '21 at 10:56
  • think I did that in the beginning with chrome.tabs.sendMessage. I droppped it becaus eI didn't see the point in having 2 js files and I put everything in background.js. Should it work with this kind of msgs ? Secondly, I think I've done it the reverse manner : background sending a message to content. content do the fetch and response back to background. Can it work that way or is it mandatory to have the fetch made in bkgrnd.js ? – f b Mar 17 '21 at 11:35
  • Content scripts can't make cross-origin requests. The code that you run via executeScript is also a content script so it can't make such request either. Do the request in the background script as shown in the example I linked in the answer. – wOxxOm Mar 17 '21 at 11:38
  • It sounds like you're ignoring everything I write. Oh well :-) – wOxxOm Mar 17 '21 at 12:13
  • @wOxxOm I recently stumbled upon a problem with `fetch` in a Chrome extension - it never provided referrer from the `chrome-extension://` page. I've tried all possible solutions (I think so). Is this a bug and is there a workaround for this? – yegorchik Mar 19 '21 at 16:34
  • ManifestV3 is stupid. -__- Adding more difficulty for no good reason. – Pangamma Jan 02 '23 at 05:58