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

fix css path contain query string bug #319

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
136 changes: 60 additions & 76 deletions cross-domain/respond-proxy.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,74 @@
</head>
<body>
<script>
(function () {
var domain, css, query, getQueryString, ajax, xmlHttp;

(function () {
var domain, css, url, match, file, ajax, xmlHttp;
ajax = function( url, callback ) {
var req = xmlHttp();
if (!req){
return;
}
req.open( "GET", url, true );
req.onreadystatechange = function () {
if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
return;
}
callback( req.responseText );
};
if ( req.readyState == 4 ){
return;
}
req.send();
};

/*
http://stackoverflow.com/questions/4963673/get-url-array-variables-in-javascript-jquery/4963817#4963817
*/
getQueryString = function() {
var ret = {}, parts, i, p;
//define ajax obj
xmlHttp = (function() {
var xmlhttpmethod = false,
attempts = [
function(){ return new XMLHttpRequest(); },
function(){ return new ActiveXObject("Microsoft.XMLHTTP"); },
function(){ return new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
],
al = attempts.length;

parts = (document.location.toString().split("?")[1]).split("&");
while( al-- ){
try {
xmlhttpmethod = attempts[ al ]();
}
catch(e) {
continue;
}
break;
}
return function(){
return xmlhttpmethod;
};
})();

for (i = 0; i < parts.length; i++) {
url = window.location.href;

p = parts[i].split("=");
// so strings will be correctly parsed:
p[1] = decodeURIComponent(p[1].replace(/\+/g, " "));
if (url) {
match = (/css\=(.*\.css)$/).exec(url.slice(url.indexOf('?') + 1));

if (p[0].search(/\[\]/) >= 0) { // then it"s an array
p[0] = p[0].replace("[]", "");
if (match && match[1]) {
css = match[1];
}

if (typeof ret[p[0]] != "object") {
ret[p[0]] = [];
}
ret[p[0]].push(p[1]);
} else {
ret[p[0]] = p[1];
}
}
return ret;
};
match = (/url\=([^&]+)/).exec(url);

ajax = function( url, callback ) {
var req = xmlHttp();
if (!req){
return;
}
req.open( "GET", url, true );
req.onreadystatechange = function () {
if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
return;
}
callback( req.responseText );
};
if ( req.readyState == 4 ){
return;
}
req.send();
};
if (match && match[1]) {
domain = match[1];
}
}

//define ajax obj
xmlHttp = (function() {
var xmlhttpmethod = false,
attempts = [
function(){ return new XMLHttpRequest(); },
function(){ return new ActiveXObject("Microsoft.XMLHTTP"); },
function(){ return new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
],
al = attempts.length;

while( al-- ){
try {
xmlhttpmethod = attempts[ al ]();
}
catch(e) {
continue;
}
break;
}
return function(){
return xmlhttpmethod;
};
})();

query = getQueryString();
css = query["css"];
domain = query["url"];

if (css && domain) {
ajax(css, function (response) {
window.name = response;
window.location.href = domain;
});
}
}());
if (css) {
ajax(css, function (response) {
window.name = response;
window.location.href = domain;
});
}
}());
</script>
</body>
</html>
239 changes: 111 additions & 128 deletions cross-domain/respond.proxy.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,112 @@
/*! Respond.js: min/max-width media query polyfill. Remote proxy (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */
/*! Respond.js: min/max-width media query polyfill. Remote proxy (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */
(function(win, doc, undefined){
var docElem = doc.documentElement,
proxyURL = doc.getElementById("respond-proxy").href,
redirectURL = (doc.getElementById("respond-redirect") || location).href,
baseElem = doc.getElementsByTagName("base")[0],
urls = [],
refNode;

function encode(url){
return win.encodeURIComponent(url);
}

function fakejax( url, callback ){

var iframe,
AXO;

// All hail Google http://j.mp/iKMI19
// Behold, an iframe proxy without annoying clicky noises.
if ( "ActiveXObject" in win ) {
AXO = new ActiveXObject( "htmlfile" );
AXO.open();
AXO.write( '<iframe id="x"></iframe>' );
AXO.close();
iframe = AXO.getElementById( "x" );
} else {
iframe = doc.createElement( "iframe" );
iframe.style.cssText = "position:absolute;top:-99em";
docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild );
}

iframe.src = checkBaseURL(proxyURL) + "?url=" + encode(redirectURL) + "&css=" + encode(checkBaseURL(url));

function checkFrameName() {
var cssText;

try {
cssText = iframe.contentWindow.name;
}
catch (e) { }

if (cssText) {
// We've got what we need. Stop the iframe from loading further content.
iframe.src = "about:blank";
iframe.parentNode.removeChild(iframe);
iframe = null;


// Per http://j.mp/kn9EPh, not taking any chances. Flushing the ActiveXObject
if (AXO) {
AXO = null;

if (win.CollectGarbage) {
win.CollectGarbage();
}
}

callback(cssText);
}
else{
win.setTimeout(checkFrameName, 100);
}
}

win.setTimeout(checkFrameName, 500);
}

// http://stackoverflow.com/a/472729
function checkBaseURL(href) {
var el = document.createElement('div'),
escapedURL = href.split('&').join('&amp;').
split('<').join('&lt;').
split('"').join('&quot;');

el.innerHTML = '<a href="' + escapedURL + '">x</a>';
return el.firstChild.href;
}

function checkRedirectURL() {
// IE6 & IE7 don't build out absolute urls in <link /> attributes.
// So respond.proxy.gif remains relative instead of http://example.com/respond.proxy.gif.
// This trickery resolves that issue.
if (~ !redirectURL.indexOf(location.host)) {

var fakeLink = doc.createElement("div");

fakeLink.innerHTML = '<a href="' + redirectURL + '"></a>';
docElem.insertBefore(fakeLink, docElem.firstElementChild || docElem.firstChild );

// Grab the parsed URL from that dummy object
redirectURL = fakeLink.firstChild.href;

// Clean up
fakeLink.parentNode.removeChild(fakeLink);
fakeLink = null;
}
}

function buildUrls(){
var links = doc.getElementsByTagName( "link" );

for( var i = 0, linkl = links.length; i < linkl; i++ ){

var thislink = links[i],
href = links[i].href,
extreg = (/^([a-zA-Z:]*\/\/(www\.)?)/).test( href ),
ext = (baseElem && !extreg) || extreg;

//make sure it's an external stylesheet
if( thislink.rel.indexOf( "stylesheet" ) >= 0 && ext ){
(function( link ){
fakejax( href, function( css ){
link.styleSheet.rawCssText = css;
respond.update();
} );
})( thislink );
}
}


}

if( !respond.mediaQueriesSupported ){
checkRedirectURL();
buildUrls();
}

})( window, document );
var docElem = doc.documentElement,
proxyURL = doc.getElementById("respond-proxy").href,
redirectURL = (doc.getElementById("respond-redirect") || location).href,
urls = [],
refNode;

function fakejax( url, callback ){

var iframe,
AXO;

// All hail Google http://j.mp/iKMI19
// Behold, an iframe proxy without annoying clicky noises.
if ( "ActiveXObject" in win ) {
AXO = new ActiveXObject( "htmlfile" );
AXO.open();
AXO.write( '<iframe id="x"></iframe>' );
AXO.close();
iframe = AXO.getElementById( "x" );
} else {
iframe = doc.createElement( "iframe" );
iframe.style.cssText = "position:absolute;top:-99em";
docElem.insertBefore(iframe, docElem.firstElementChild || docElem.firstChild );
}

iframe.src = proxyURL + "?url=" + redirectURL + "&css=" + url;

function checkFrameName() {
var cssText;

try {
cssText = iframe.contentWindow.name;
}
catch (e) { }

if (cssText) {
// We've got what we need. Stop the iframe from loading further content.
iframe.src = "about:blank";
iframe.parentNode.removeChild(iframe);
iframe = null;


// Per http://j.mp/kn9EPh, not taking any chances. Flushing the ActiveXObject
if (AXO) {
AXO = null;

if (win.CollectGarbage) {
win.CollectGarbage();
}
}

callback(cssText);
}
else{
win.setTimeout(checkFrameName, 100);
}
}

win.setTimeout(checkFrameName, 500);
}

function checkRedirectURL() {
// IE6 & IE7 don't build out absolute urls in <link /> attributes.
// So respond.proxy.gif remains relative instead of http://example.com/respond.proxy.gif.
// This trickery resolves that issue.
if (~ !redirectURL.indexOf(location.host)) {

// Inject an <a> attribute, with the redirectURL
var fakeLink = doc.createElement("div");

fakeLink.innerHTML = '<a href="' + redirectURL + '"></a>';
docElem.insertBefore(fakeLink, docElem.firstElementChild || docElem.firstChild );

// Grab the parsed URL from that dummy object
redirectURL = fakeLink.firstChild.href;

// Clean up
fakeLink.parentNode.removeChild(fakeLink);
fakeLink = null;
}
}

function buildUrls(){
var links = doc.getElementsByTagName( "link" );

for( var i = 0, linkl = links.length; i < linkl; i++ ){
var thislink = links[i],
href = links[i].href.split('?')[0],
ext = /^([a-zA-Z]+?:(\/\/)?(www\.)?)/;

//make sure it's an external stylesheet
if( thislink.rel.indexOf( "stylesheet" ) >= 0 && ext.test( href ) ){
(function( link ){
fakejax( href, function( css ){
link.styleSheet.rawCssText = css;
respond.update();
} );
})( thislink );
}
}


}

if( !respond.mediaQueriesSupported ){
checkRedirectURL();
buildUrls();
}

})( window, document );
Loading