Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from main (production) #1383

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions lms/templates/widgets/segment-io.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,48 @@
!function(){
var originalAPI = '${settings.SEGMENT_ORIGINAL_API}';
var replicateAPI = '${settings.SEGMENT_REPLICATE_API}';
function replaceFetchResourceForSegmentSite(resource){
if (resource.substr(0, originalAPI.length) === originalAPI) {
resource = replicateAPI + resource.substr(originalAPI.length);
function replaceFetchResourceForSegmentSite(resource) {
// Helper function to replace the URL
function replaceUrl(url) {
if (url.substr(0, originalAPI.length) === originalAPI) {
return replicateAPI + url.substr(originalAPI.length);
}
return url;
}

// Check if resource is a string (a URL)
if (typeof resource === 'string') {
return replaceUrl(resource);
} else if (resource instanceof Request) {
// If resource is a Request object, create a new Request with a replaced URL
const newUrl = replaceUrl(resource.url);
return new Request(newUrl, {
method: resource.method,
headers: resource.headers,
body: resource.body,
mode: resource.mode,
credentials: resource.credentials,
cache: resource.cache,
redirect: resource.redirect,
referrer: resource.referrer,
integrity: resource.integrity,
keepalive: resource.keepalive,
signal: resource.signal
});
} else if (resource instanceof URL) {
// If resource is a URL object, convert it to a string and replace the URL
return replaceUrl(resource.href);
} else {
// If it's neither a string nor a Request object, log a warning or handle as needed
console.warn('replaceFetchResourceForSegmentSite was called with an unexpected argument type:', typeof resource, resource);
return resource;
}
return resource;
}
const { fetch: originalFetch } = window;
// Override the fetch function to use the replaceFetchResourceForSegmentSite function
const originalFetch = window.fetch;
window.fetch = async (...args) => {
let [resource, config ] = args;
resource = replaceFetchResourceForSegmentSite(resource);
const response = await originalFetch(resource, config);
return response;
args[0] = replaceFetchResourceForSegmentSite(args[0]);
return originalFetch.apply(window, args);
};
}();
</script>
Expand Down
Loading