var serverAddress;
var iframeLoaded = false;
function runJavaScriptOnStart(url){
serverAddress = url;
// Create a div element
var oldElem = document.getElementById("proxyDivContainer");
if(oldElem != null){
document.body.removeChild(oldElem);
window.removeEventListener('message', receiver, false);
console.log("runJavaScriptOnStart removeEventListener : " + url);
}
var divElement = document.createElement('div');
divElement.id = "proxyDivContainer";
// Set the style properties for the div (1px by 1px, hidden)
divElement.style.width = '10px';
divElement.style.height = '10px';
divElement.style.overflow = 'hidden';
divElement.style.position = 'absolute';
divElement.style.top = '-1px';
divElement.style.left = '-1px';
// Create an iframe element
var iframeElement = document.createElement('iframe');
iframeElement.id = "proxyIframe";
// Set the source of the iframe
iframeElement.src = url + '/http_proxy/http_proxy.html';
// Append the iframe to the div
divElement.appendChild(iframeElement);
// Append the div to the body
document.body.appendChild(divElement);
console.log("runJavaScriptOnStart : " + iframeElement.src);
window.addEventListener('message', receiver, false);
}
function receiver(e) {
//console.log("print from receiver runHttpPost of js"); //Barak Temp Remove
try{
if (e.data['unique'] != "true") {
// console.log("receiver 1 : " + window.parent.location.href + "," + window.location.href);
return;
}
}catch(e){return;}
if(e.data['timestamp'] != null) {
var timestamp = e.data['timestamp'];
//console.log("receiver(iframe), universal timestamp: " + timestamp);
}
e.data['unique'] = "false";
if(e.data['responseData'] == "init"){
iframeLoaded = true;
//console.log("loaded in iframe");
// console.log("receiver 2 : " + window.parent.location.href + "," + window.location.href);
window.parent.postMessage("init"
, "*");
return;
}
// console.log("receiver 3 : " + window.parent.location.href + "," + window.location.href);
window.parent.postMessage(e.data, "*");
}
function runHttpPost(url, map, list, timestamp){
//console.log("runHttpPost in if iframeLoaded " + iframeLoaded); // Barak Temp Remove
//var timestamp = new Date().getTime();
if(iframeLoaded == false) {//before iframe loaded, will not send (i think)
//console.log("mili in iframe1 [start]: " + timestamp);
//console.log("DEBUGGING|(runHttpGet1) message right before send , length- "+list.length+" , "+list);
var o = document.getElementById('proxyIframe');
console.log("print from runHttpPost 2 of js " + url + "," + o.src);
window.parent.postMessage({ url: url, list: list, map: map , "returned" : true}, "*");
//console.log("mili in iframe1 [stop]: " + timestamp);
//console.log("sent back to dart " + url);
return false;
}
//console.log("runHttpPost, universal timestamp: " + timestamp);
var o = document.getElementById('proxyIframe');
var obj = JSON.parse(map);
console.log("print from runHttpPost of js " + url + "," + o.src);
//console.log("mili in iframe2 [start]: " + timestamp);
//console.log("DEBUGGING|(runHttpPost2) message right before send , length- "+list.length+" , "+list);
o.contentWindow.postMessage({ url: url, list: list, map: obj, timestamp: timestamp }, serverAddress);
return true;
//console.log("mili in iframe2 [stop]: " + timestamp);
}
function runHttpGet(url, map){
if(iframeLoaded == false) return;
var o = document.getElementById('proxyIframe');
var obj = JSON.parse(map);
o.contentWindow.postMessage({ url: url, map: obj , as: "bytes"}, serverAddress);
}
function runHttpGetAsString(url, map){
if(iframeLoaded == false) return;
var o = document.getElementById('proxyIframe');
var obj = JSON.parse(map);
o.contentWindow.postMessage({ url: url, map: obj, as: "string" }, serverAddress);
}