Skip to content

Use 'document.scripts' instead of document.getElementsByTagName #496

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

Merged
merged 2 commits into from
Sep 1, 2016
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
21 changes: 18 additions & 3 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
var url = require('url');
var SockJS = require("sockjs-client");
var stripAnsi = require('strip-ansi');

function getCurrentScriptSource() {
// try to get the current script
if (document.currentScript) {
return document.currentScript.getAttribute("src");
}
// fall back to getting all scripts in the document
var scriptElements = document.scripts || [],
currentScript = scriptElements[scriptElements.length - 1];
if (currentScript) {
return currentScript.getAttribute("src");
}
// fail as there was no script to use
throw new Error("Failed to get current script source");
}

var urlParts;
if (typeof __resourceQuery === "string" && __resourceQuery) {
// If this bundle is inlined, use the resource query to get the correct url.
urlParts = url.parse(__resourceQuery.substr(1));
} else {
// Else, get the url from the <script> this file was called with.
var scriptElements = document.getElementsByTagName("script");
var scriptHost = scriptElements[scriptElements.length-1].getAttribute("src");
scriptHost = scriptHost && scriptHost.replace(/\/[^\/]+$/, "");
var scriptHost = getCurrentScriptSource();
scriptHost = scriptHost.replace(/\/[^\/]+$/, "");
urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
}

Expand Down