Bypass CORS Restrictions

Fix CORS issues for video resources

  1. // ==UserScript==
  2. // @name Bypass CORS Restrictions
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Fix CORS issues for video resources
  6. // @author YourName
  7. // @match *://*/*
  8. // @grant unsafeWindow
  9. // @license MIT
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Overriding the fetch API
  17. const originalFetch = unsafeWindow.fetch;
  18. unsafeWindow.fetch = async function(...args) {
  19. const [resource, options = {}] = args;
  20.  
  21. // Modify headers to bypass CORS restrictions
  22. const modifiedOptions = {
  23. ...options,
  24. headers: {
  25. ...options.headers,
  26. 'Origin': 'https://ddys.pro', // Replace with the origin required by the server
  27. }
  28. };
  29.  
  30. const response = await originalFetch(resource, modifiedOptions);
  31.  
  32. // Modify the response if necessary
  33. const modifiedResponse = new Response(response.body, {
  34. ...response,
  35. headers: {
  36. ...response.headers,
  37. 'Access-Control-Allow-Origin': '*',
  38. 'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS',
  39. 'Access-Control-Allow-Headers': '*',
  40. }
  41. });
  42.  
  43. return modifiedResponse;
  44. };
  45.  
  46. // Overriding XMLHttpRequest
  47. const originalXhrOpen = XMLHttpRequest.prototype.open;
  48. XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
  49. this.addEventListener('readystatechange', function() {
  50. if (this.readyState === 4) {
  51. // Modify CORS headers in the response
  52. Object.defineProperty(this, 'responseText', {
  53. get: function() {
  54. return this.responseText.replace(
  55. 'Access-Control-Allow-Origin: https://ddys.pro',
  56. 'Access-Control-Allow-Origin: *'
  57. );
  58. }
  59. });
  60. }
  61. });
  62.  
  63. originalXhrOpen.call(this, method, url, async, user, password);
  64. };
  65. })();