fix-cors.js 699 B

1234567891011121314151617181920
  1. // 强制所有 fetch 请求添加 CORS 头
  2. const originalFetch = window.fetch;
  3. window.fetch = async (url, options) => {
  4. if (typeof url === 'string' && url.startsWith('http')) {
  5. options = options || {};
  6. options.headers = options.headers || {};
  7. options.headers['Origin'] = window.location.origin;
  8. options.mode = 'cors';
  9. }
  10. return originalFetch(url, options);
  11. };
  12. // 强制所有 XHR 请求添加 CORS 头
  13. const originalOpen = XMLHttpRequest.prototype.open;
  14. XMLHttpRequest.prototype.open = function (method, url) {
  15. originalOpen.apply(this, arguments);
  16. if (typeof url === 'string' && url.startsWith('http')) {
  17. this.setRequestHeader('Origin', window.location.origin);
  18. }
  19. };