|
| 1 | +(function () { |
| 2 | + if (window.__algorithmHubHackerrankBridgeInstalled) { |
| 3 | + return; |
| 4 | + } |
| 5 | + |
| 6 | + window.__algorithmHubHackerrankBridgeInstalled = true; |
| 7 | + |
| 8 | + var EVENT_NAME = "algorithmhub:hackerrank-submission"; |
| 9 | + var SUBMISSION_PATH_PATTERN = |
| 10 | + /^\/rest\/contests\/([^/]+)\/challenges\/([^/]+)\/submissions(?:\/(\d+))?\/?$/; |
| 11 | + |
| 12 | + function getRequestUrl(input) { |
| 13 | + if (typeof input === "string") { |
| 14 | + return input; |
| 15 | + } |
| 16 | + |
| 17 | + if (input && typeof input.url === "string") { |
| 18 | + return input.url; |
| 19 | + } |
| 20 | + |
| 21 | + return ""; |
| 22 | + } |
| 23 | + |
| 24 | + function getRequestMethod(input, init) { |
| 25 | + var initMethod = init && typeof init.method === "string" ? init.method : ""; |
| 26 | + if (initMethod) { |
| 27 | + return initMethod.toUpperCase(); |
| 28 | + } |
| 29 | + |
| 30 | + var inputMethod = input && typeof input.method === "string" ? input.method : ""; |
| 31 | + return (inputMethod || "GET").toUpperCase(); |
| 32 | + } |
| 33 | + |
| 34 | + function parseSubmissionRequest(url, method) { |
| 35 | + try { |
| 36 | + var parsedUrl = new URL(url, window.location.href); |
| 37 | + var match = parsedUrl.pathname.match(SUBMISSION_PATH_PATTERN); |
| 38 | + if (!match) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + var submissionId = match[3] || ""; |
| 43 | + if (method === "POST" && submissionId) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + if (method === "GET" && !submissionId) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + if (method !== "POST" && method !== "GET") { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + contestSlug: match[1], |
| 57 | + challengeSlug: match[2], |
| 58 | + submissionId: submissionId, |
| 59 | + }; |
| 60 | + } catch (_error) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function publishSubmission(body, request) { |
| 66 | + var model = body && body.model; |
| 67 | + if (!model || !model.id) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + window.dispatchEvent( |
| 72 | + new CustomEvent(EVENT_NAME, { |
| 73 | + detail: JSON.stringify({ |
| 74 | + submissionId: String(model.id || request.submissionId), |
| 75 | + contestSlug: model.contest_slug || request.contestSlug, |
| 76 | + challengeSlug: |
| 77 | + model.challenge_slug || model.slug || request.challengeSlug, |
| 78 | + submission: model, |
| 79 | + }), |
| 80 | + }) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + function readJsonResponseText(text, request) { |
| 85 | + if (!text) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + publishSubmission(JSON.parse(text), request); |
| 91 | + } catch (_error) { |
| 92 | + // Ignore non-JSON responses. |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (typeof window.fetch === "function") { |
| 97 | + var originalFetch = window.fetch; |
| 98 | + window.fetch = function (input, init) { |
| 99 | + var request = parseSubmissionRequest( |
| 100 | + getRequestUrl(input), |
| 101 | + getRequestMethod(input, init) |
| 102 | + ); |
| 103 | + |
| 104 | + return originalFetch.apply(this, arguments).then(function (response) { |
| 105 | + if (request) { |
| 106 | + response |
| 107 | + .clone() |
| 108 | + .json() |
| 109 | + .then(function (body) { |
| 110 | + publishSubmission(body, request); |
| 111 | + }) |
| 112 | + .catch(function () { |
| 113 | + // The page still receives the original response. |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return response; |
| 118 | + }); |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + var originalOpen = XMLHttpRequest.prototype.open; |
| 123 | + var originalSend = XMLHttpRequest.prototype.send; |
| 124 | + |
| 125 | + XMLHttpRequest.prototype.open = function (method, url) { |
| 126 | + this.__algorithmHubHackerrankRequest = parseSubmissionRequest( |
| 127 | + String(url || ""), |
| 128 | + String(method || "GET").toUpperCase() |
| 129 | + ); |
| 130 | + return originalOpen.apply(this, arguments); |
| 131 | + }; |
| 132 | + |
| 133 | + XMLHttpRequest.prototype.send = function () { |
| 134 | + if (this.__algorithmHubHackerrankRequest) { |
| 135 | + this.addEventListener("load", function () { |
| 136 | + var request = this.__algorithmHubHackerrankRequest; |
| 137 | + if (!request) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if (this.responseType === "json") { |
| 142 | + publishSubmission(this.response, request); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + if (!this.responseType || this.responseType === "text") { |
| 147 | + readJsonResponseText(this.responseText, request); |
| 148 | + } |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + return originalSend.apply(this, arguments); |
| 153 | + }; |
| 154 | +})(); |
0 commit comments