XMLHttpRequest succeeds without manifest permissions? Maybe CORS?

前端 未结 1 1119
-上瘾入骨i
-上瘾入骨i 2020-12-21 16:11

I have developed a Google Chrome extensions that uses YouTube Data API v2. My permission field in the manifest looks like this, because the script is inject

相关标签:
1条回答
  • 2020-12-21 16:37

    If Chrome does not find the permission in the manifest, it treats a request as a normal request. This means that a request will still succeed when the right CORS headers are set. Otherwise, a request will fail because of the same origin policy.

    The Google API JavaScript library explicitly mentions support for CORS:

    Making a request: Option 3
    Google APIs support CORS. Please visit the CORS page for more information on using CORS to make requests.

    If possible, I still recommend adding the permission to the manifest file. For simple requests, this does not bring any advantages. For non-simple requests, this will half the number of requests: Non-simple requests are always preceeded by a preflight (OPTIONS) request which checks if the client is permitted to access the source.

    By adding the permission to the manifest file, Chrome will not fall back to CORS, and always use one network request to complete the request. Great!
    However... you might think again if you're the author of an already-deployed extension. When new origin permissions are added to the manifest file, the extension will be disabled until the user approves the extension. The dialog box shows "Remove extension" and "Enable" next to each other, so there's a chance of loosing the user.

    If you wish, you can overcome this problem by using an optional permission, activated at the options page. Clearly explain in layman language that the option will improve the speed of the extension, and don't forget to mention that additional permissions will be requested.

    0 讨论(0)
提交回复
热议问题