bifrost-cors
Advanced tools
Comparing version 1.0.1 to 1.0.4
339
index.js
@@ -1,1 +0,338 @@ | ||
console.log("Bifrost Loaded -",window.location.href);class BifrostCors{constructor(e,t=!1,s){var o;(this.bifrostResponse,this.address=e,this.socketListner,this.bifrostBridge=bifrostBridge.bind(this),this.promiseConstructor=promiseConstructor.bind(this),this.postbackLocalstorage=postbackLocalstorage.bind(this),this.postbackSetLocalstorage=postbackSetLocalstorage.bind(this),this.postbackDeleteLocalstorage=postbackDeleteLocalstorage.bind(this),this.postbackGetCookie=postbackGetCookie.bind(this),this.postbackSetCookie=postbackSetCookie.bind(this),this.postbackRunEval=postbackRunEval.bind(this),this.postbackDomManipulationId=postbackDomManipulationId.bind(this),this.postbackDomManipulationClass=postbackDomManipulationClass.bind(this),this.startMessageThread=startMessageThread.bind(this),this.handleSocketMessage=handleSocketMessage.bind(this),t&&(this.midgard=document.getElementById(s)),!1===t)&&((o=document.createElement("iframe")).setAttribute("id","bifrost-cors"),o.setAttribute("width","0px"),o.setAttribute("height","0px"),o.setAttribute("style","position:absolute; top: -999px; display: none"),o.setAttribute("src",this.address),document.body.appendChild(o),this.midgard=document.getElementById("bifrost-cors"));window.addEventListener("message",e=>{if(e.origin===this.address){if(e.data.type.includes("request")){let t=e.data.type.replace("request","postback").replace(/-/g,"_");this.heimdall(t,e.data.value)}"bifrost-socket-message"===e.data.type&&(this.socketListner?(this.bifrostBridge("bifrost-response",!0,!0),this.socketListner(e.data.value)):(console.group("Error - Bifrost-Cors"),console.error("Message Thread not init -- Bifrost-Cors"),console.log("You.developer ? dig into lib at line 37 : Raise a issue"),console.groupEnd()))}})}async getLocalStorage(e){return this.heimdall("get_localstorage",e),this.heimdall("get_response"),await this.bifrostResponse}async setLocalStorage(e){return this.heimdall("set_localstorage",e),this.heimdall("get_response"),await this.bifrostResponse}async deleteLocalStorage(e){return this.heimdall("delete_localstorage",e),this.heimdall("get_response"),await this.bifrostResponse}async getCookie(e){return this.heimdall("get_cookie",e),this.heimdall("get_response"),await this.bifrostResponse}async setCookie(e,t,s){let o=[e,t,s];return this.heimdall("set_cookie",o),this.heimdall("get_response"),await this.bifrostResponse}async runExpression(e){return this.heimdall("run_eval",e),this.heimdall("get_response"),await this.bifrostResponse}async domManipulationById(e,t){let s=[e,t];return this.heimdall("dom_manipulation_id",s),this.heimdall("get_response"),await this.bifrostResponse}async domManipulationByClass(e,t,s){let o=[e,t,s];return this.heimdall("dom_manipulation_class",o),this.heimdall("get_response"),await this.bifrostResponse}async requestMessageThread(e){return this.heimdall("start_message_thread",e),!!this.socketListner}async send(e){return this.heimdall("bifrost_socket_send_message",e),this.heimdall("get_response"),await this.bifrostResponse}heimdall(e,t){switch(e){case"get_response":this.promiseConstructor("bifrost-response");break;case"get_localstorage":this.bifrostBridge("request-get-localstorage",t);break;case"set_localstorage":this.bifrostBridge("request-set-localstorage",t);break;case"delete_localstorage":this.bifrostBridge("request-delete-localstorage",t);break;case"get_cookie":this.bifrostBridge("request-get-cookie",t);break;case"set_cookie":this.bifrostBridge("request-set-cookie",t);break;case"run_eval":this.bifrostBridge("request-run-eval",t);break;case"dom_manipulation_id":this.bifrostBridge("request-dom-manipulation-id",t);break;case"dom_manipulation_class":this.bifrostBridge("request-dom-manipulation-class",t);break;case"postback_get_localstorage":this.postbackLocalstorage(t);break;case"postback_set_localstorage":this.postbackSetLocalstorage(t);break;case"postback_delete_localstorage":this.postbackDeleteLocalstorage(t);break;case"postback_get_cookie":this.postbackGetCookie(t);break;case"postback_set_cookie":this.postbackSetCookie(t);break;case"postback_run_eval":this.postbackRunEval(t);break;case"postback_dom_manipulation_id":this.postbackDomManipulationId(t);break;case"postback_dom_manipulation_class":this.postbackDomManipulationClass(t);break;case"start_message_thread":this.startMessageThread(t);break;case"bifrost_socket_send_message":this.handleSocketMessage(t)}}}function bifrostBridge(e,t,s=!1){let o={type:e,value:t};s?window.parent.postMessage(o,"*"):this.midgard&&this.midgard.contentWindow.postMessage(o,"*")}function promiseConstructor(e){return this.bifrostResponse=new Promise((t,s)=>{window.addEventListener("message",s=>{s.origin===this.address&&s.data.type&&s.data.type===e&&t(s.data.value)})}),this.bifrostResponse}function postbackLocalstorage(e){if("object"==typeof e){let t=[];e.map(e=>{t.push(localStorage.getItem(e))}),this.bifrostBridge("bifrost-response",t,!0)}else{let t=localStorage.getItem(e);this.bifrostBridge("bifrost-response",t,!0)}}function postbackSetLocalstorage(e){localStorage.setItem(e.key,e.value),localStorage.getItem(e.key),this.bifrostBridge("bifrost-response",!0,!0)}function postbackDeleteLocalstorage(e){"object"==typeof e?(e.map(e=>{localStorage.removeItem(e)}),this.bifrostBridge("bifrost-response",!0,!0)):(localStorage.removeItem(e),this.bifrostBridge("bifrost-response",!0,!0))}function postbackGetCookie(e){let t=document.cookie.split(";"),s={};for(var o=0;o<t.length;o++){let e=t[o].split("=");s[(e[0]+"").trim()]=unescape(e[1])}if(e){let t=s[e];this.bifrostBridge("bifrost-response",t,!0)}else{let e=s;this.bifrostBridge("bifrost-response",e,!0)}}function postbackSetCookie(e){let t=e[0],s=e[1],o=e[2],i="";if(o){let e=new Date;e.setTime(e.getTime()+24*o*60*60*1e3),i="; expires="+e.toUTCString()}document.cookie=t+"="+(s||"")+i+"; path=/",this.bifrostBridge("bifrost-response",!0,!0)}function postbackRunEval(payload){let data=eval(payload);this.bifrostBridge("bifrost-response",data,!0)}function postbackDomManipulationId(e){let t=e[0],s=e[1],o=document.getElementById(t).style;Object.keys(s).map(e=>{o[e]=s[e]}),this.bifrostBridge("bifrost-response",!0,!0)}function postbackDomManipulationClass(e){let t=e[0],s=e[1],o=e[2],i=document.getElementsByClassName(t)[s].style;Object.keys(o).map(e=>{i[e]=o[e]}),this.bifrostBridge("bifrost-response",!0,!0)}function startMessageThread(e){this.socketListner=e,this.bifrostBridge("bifrost-response",!0,!0)}function handleSocketMessage(e){this.bifrostBridge("bifrost-socket-message",e,Boolean(!this.midgard))}export default BifrostCors; | ||
console.log("Bifrost Loaded -",window.location.href) | ||
class BifrostCors { | ||
constructor(address,iframeBoolean = false,iframeID){ | ||
this.bifrostResponse | ||
this.address = address | ||
this.socketListner | ||
//==========================={ + Function Binding + }================================ | ||
this.bifrostBridge = bifrostBridge.bind(this) | ||
this.promiseConstructor = promiseConstructor.bind(this) | ||
this.postbackLocalstorage = postbackLocalstorage.bind(this) | ||
this.postbackSetLocalstorage = postbackSetLocalstorage.bind(this) | ||
this.postbackDeleteLocalstorage = postbackDeleteLocalstorage.bind(this) | ||
this.postbackGetCookie = postbackGetCookie.bind(this) | ||
this.postbackSetCookie = postbackSetCookie.bind(this) | ||
this.postbackRunEval = postbackRunEval.bind(this) | ||
this.postbackDomManipulationId = postbackDomManipulationId.bind(this) | ||
this.postbackDomManipulationClass = postbackDomManipulationClass.bind(this) | ||
this.startMessageThread = startMessageThread.bind(this) | ||
this.handleSocketMessage = handleSocketMessage.bind(this) | ||
//==========================={ + M I D G A R D + }================================ | ||
if(iframeBoolean){ | ||
this.midgard = document.getElementById(iframeID) | ||
} | ||
if(iframeBoolean === false){ | ||
var ifrm | ||
ifrm = document.createElement('iframe'); | ||
ifrm.setAttribute('id', 'bifrost-cors'); | ||
ifrm.setAttribute('width',"0px"); | ||
ifrm.setAttribute('height',"0px"); | ||
ifrm.setAttribute('style',"position:absolute; top: -999px; display: none") | ||
ifrm.setAttribute('src', this.address) | ||
document.body.appendChild(ifrm) | ||
this.midgard = document.getElementById("bifrost-cors") | ||
} | ||
//======================={ + B I F R O S T - L I S T N E R + }========================= | ||
window.addEventListener("message", (e) => { | ||
if(e.origin === this.address){ | ||
if(e.data.type.includes("request")){ | ||
let requestType = e.data.type.replace("request","postback").replace(/-/g,"_") | ||
this.heimdall(requestType,e.data.value) | ||
} | ||
if(e.data.type === "bifrost-socket-message"){ | ||
if(this.socketListner){ | ||
this.bifrostBridge("bifrost-response",true,true) | ||
this.socketListner(e.data.value) | ||
} else{ | ||
console.group("Error - Bifrost-Cors") | ||
console.error("Message Thread not init -- Bifrost-Cors") | ||
console.log("You.developer ? dig into lib at line 37 : Raise a issue") | ||
console.groupEnd() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
//=========================={ + B I F R O S T - M E T H O D S + }========================= | ||
async getLocalStorage(key){ | ||
this.heimdall("get_localstorage",key) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async setLocalStorage(payload){ | ||
this.heimdall("set_localstorage",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async deleteLocalStorage(payload){ | ||
this.heimdall("delete_localstorage",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async getCookie(payload){ | ||
this.heimdall("get_cookie",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async setCookie(name,value,day){ | ||
let payload = [name,value,day] | ||
this.heimdall("set_cookie",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async runExpression(payload){ | ||
this.heimdall("run_eval",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async domManipulationById(Id,styleObj){ | ||
let payload = [Id,styleObj] | ||
this.heimdall("dom_manipulation_id",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async domManipulationByClass(Class, index, styleObj){ | ||
let payload = [Class,index,styleObj] | ||
this.heimdall("dom_manipulation_class",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
async requestMessageThread(payload){ | ||
this.heimdall("start_message_thread",payload) | ||
if(this.socketListner) return true | ||
return false | ||
} | ||
async send(payload){ | ||
this.heimdall("bifrost_socket_send_message",payload) | ||
this.heimdall("get_response") | ||
return await this.bifrostResponse | ||
} | ||
//=============================={ + H E I M D A L L + }=================================== | ||
heimdall(event,payload){ | ||
switch(event){ | ||
case "get_response": | ||
this.promiseConstructor("bifrost-response") | ||
break; | ||
case "get_localstorage": | ||
this.bifrostBridge("request-get-localstorage",payload) | ||
break; | ||
case "set_localstorage": | ||
this.bifrostBridge("request-set-localstorage",payload) | ||
break; | ||
case "delete_localstorage": | ||
this.bifrostBridge("request-delete-localstorage",payload) | ||
break; | ||
case "get_cookie": | ||
this.bifrostBridge("request-get-cookie",payload) | ||
break; | ||
case "set_cookie": | ||
this.bifrostBridge("request-set-cookie",payload) | ||
break; | ||
case "run_eval": | ||
this.bifrostBridge("request-run-eval",payload) | ||
break; | ||
case "dom_manipulation_id": | ||
this.bifrostBridge("request-dom-manipulation-id",payload) | ||
break; | ||
case "dom_manipulation_class": | ||
this.bifrostBridge("request-dom-manipulation-class",payload) | ||
break; | ||
case "postback_get_localstorage": | ||
this.postbackLocalstorage(payload) | ||
break; | ||
case "postback_set_localstorage": | ||
this.postbackSetLocalstorage(payload) | ||
break; | ||
case "postback_delete_localstorage": | ||
this.postbackDeleteLocalstorage(payload) | ||
break; | ||
case "postback_get_cookie": | ||
this.postbackGetCookie(payload) | ||
break; | ||
case "postback_set_cookie": | ||
this.postbackSetCookie(payload) | ||
break; | ||
case "postback_run_eval": | ||
this.postbackRunEval(payload) | ||
break; | ||
case "postback_dom_manipulation_id": | ||
this.postbackDomManipulationId(payload) | ||
break; | ||
case "postback_dom_manipulation_class": | ||
this.postbackDomManipulationClass(payload) | ||
break; | ||
case "start_message_thread": | ||
this.startMessageThread(payload) | ||
break; | ||
case "bifrost_socket_send_message": | ||
this.handleSocketMessage(payload) | ||
break; | ||
} | ||
} | ||
} | ||
// ====================================={+ F U N C T I O N S +} ========================================= | ||
function bifrostBridge(event,payload,postback = false){ | ||
let message = { | ||
type: event, | ||
value :payload | ||
} | ||
if(postback){ | ||
window.parent.postMessage(message,"*") | ||
} else { | ||
this.midgard && this.midgard.contentWindow.postMessage(message, '*') | ||
} | ||
} | ||
function promiseConstructor(promiseType){ | ||
this.bifrostResponse = new Promise((resolve, reject) => { | ||
window.addEventListener("message", (e) => { | ||
if(e.origin === this.address){ | ||
if(e.data.type && e.data.type === promiseType){ | ||
resolve(e.data.value) | ||
} | ||
} | ||
}) | ||
}) | ||
return this.bifrostResponse | ||
} | ||
function postbackLocalstorage(payload){ | ||
if(typeof(payload) === "object"){ | ||
let data = [] | ||
payload.map(key => { | ||
data.push(localStorage.getItem(key)) | ||
}) | ||
this.bifrostBridge("bifrost-response",data,true) | ||
}else{ | ||
let data = localStorage.getItem(payload) | ||
this.bifrostBridge("bifrost-response",data,true) | ||
} | ||
} | ||
function postbackSetLocalstorage(payload){ | ||
localStorage.setItem(payload["key"],payload["value"]) | ||
if(localStorage.getItem(payload["key"])){ | ||
this.bifrostBridge("bifrost-response", true,true) | ||
}else{ | ||
this.bifrostBridge("bifrost-response", true,true) | ||
} | ||
} | ||
function postbackDeleteLocalstorage(payload){ | ||
if(typeof(payload) === "object"){ | ||
payload.map(key => { | ||
localStorage.removeItem(key) | ||
}) | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} else { | ||
localStorage.removeItem(payload) | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} | ||
} | ||
function postbackGetCookie(payload){ | ||
let cookieString = document.cookie.split(";"); | ||
let cookies = {}; | ||
for (var i=0; i<cookieString.length; i++){ | ||
let cookie = cookieString[i].split("="); | ||
cookies[(cookie[0]+'').trim()] = unescape(cookie[1]); | ||
} | ||
if(payload){ | ||
let data = cookies[payload] | ||
this.bifrostBridge("bifrost-response", data, true) | ||
} else { | ||
let data = cookies | ||
this.bifrostBridge("bifrost-response", data, true) | ||
} | ||
} | ||
function postbackSetCookie(payload){ | ||
let name = payload[0], value = payload[1], days = payload[2] | ||
let expires = ""; | ||
if (days) { | ||
let date = new Date(); | ||
date.setTime(date.getTime() + (days*24*60*60*1000)); | ||
expires = "; expires=" + date.toUTCString(); | ||
} | ||
document.cookie = name + "=" + (value || "") + expires + "; path=/"; | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} | ||
function postbackRunEval(payload){ | ||
let data = eval(payload) | ||
this.bifrostBridge("bifrost-response",data,true) | ||
} | ||
function postbackDomManipulationId(payload){ | ||
let Id = payload[0] | ||
let styleObj = payload[1] | ||
let host = document.getElementById(Id).style | ||
Object.keys(styleObj).map(item =>{ | ||
host[item] = styleObj[item] | ||
}) | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} | ||
function postbackDomManipulationClass(payload){ | ||
let Class = payload[0] | ||
let index = payload[1] | ||
let styleObj = payload[2] | ||
let host = document.getElementsByClassName(Class)[index].style | ||
Object.keys(styleObj).map(item =>{ | ||
host[item] = styleObj[item] | ||
}) | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} | ||
function startMessageThread(payload){ | ||
this.socketListner = payload | ||
this.bifrostBridge("bifrost-response",true,true) | ||
} | ||
function handleSocketMessage(payload){ | ||
this.bifrostBridge("bifrost-socket-message",payload,Boolean(!this.midgard)) | ||
} | ||
export default BifrostCors |
{ | ||
"name": "bifrost-cors", | ||
"version": "1.0.1", | ||
"description": "A cross-domain communication solution to share data and many more functionalities with simple as just calling a method.", | ||
"version": "1.0.4", | ||
"description": "A cross-domain utility lib, for sharing localStorage, cookie and many more over the cross domain", | ||
"main": "index.js", | ||
@@ -9,16 +9,13 @@ "scripts": { | ||
}, | ||
"author": "https://github.com/spurushottam13", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/spurushottam13/bifrost-cors" | ||
"url": "https://github.com/spurushottam13/bifrost-cors.git" | ||
}, | ||
"keywords": [ | ||
"cors", | ||
"iframe message", | ||
"cros-domain", | ||
"get localstorage from other domain", | ||
"get cookie from other domain", | ||
"cross domain" | ||
], | ||
"license": "ISC" | ||
"author": "", | ||
"license": "ISC", | ||
"keywords": ["cors", "cross-domain utility", "cross storage", "get localstorage from other domain", "get cookie from other domain", "cross domain"], | ||
"bugs": { | ||
"url": "https://github.com/spurushottam13/bifrost-cors/issues" | ||
}, | ||
"homepage": "https://github.com/spurushottam13/bifrost-cors#readme" | ||
} |
@@ -1,5 +0,17 @@ | ||
<img src="logo.jpg" width="75px" height="75" align="left"/> | ||
<p align="center"> | ||
<img align="center" src="logo.jpg" width="150px" height="150"/> | ||
</p> | ||
# Bifrost-CORS | ||
###### A cross-domain communication solution to share data and many more functionalities with simple as just calling a method. | ||
#### Get Started | ||
##### NPM Install | ||
> __npm install bifrost-cors__ | ||
##### CDN Link (6.39KB) | ||
>__https://cdn.jsdelivr.net/gh/spurushottam13/bifrost-cors/index.min.js__ | ||
##### GZIP version of File (1.7KB) | ||
>https://cdn.jsdelivr.net/gh/spurushottam13/bifrost-cors/index.min.gz | ||
#### Functionalities can be performed on CROS Domain | ||
@@ -19,3 +31,3 @@ ------------- | ||
---- | ||
##### Initialize Bifrost-CORS | ||
##### Initialize Bifrost-CORS *You have to Initialize Bifrost-CORS in both domain* | ||
```javascript | ||
@@ -29,3 +41,2 @@ var bifrostCors = new bifrostCors(address, iframeBoolean,iframeId) | ||
| iframeId| No | if __iframeBoolean__ is set to __true__ then you need to pass the ID for your Iframe in which you loading the other domain | | ||
======= | ||
@@ -54,3 +65,3 @@ --- | ||
#### Functionalities | ||
* Cookies | ||
* __Cookies__ | ||
* __Get Cookies__ | ||
@@ -62,3 +73,3 @@ ```javascript | ||
// return type string | ||
bifrostCors.getCookie("key") | ||
bifrostCors.getCookie("key") | ||
``` | ||
@@ -68,3 +79,3 @@ | ||
```javascript | ||
bifrostCors.setCookie(name,value,days) | ||
bifrostCors.setCookie(name,value,days) | ||
``` | ||
@@ -78,3 +89,3 @@ * Parameter | ||
* LocalStorage | ||
* __LocalStorage__ | ||
* __Get local Storage__ | ||
@@ -86,3 +97,3 @@ ```javascript | ||
// return type array | ||
bifrostCors.getLocalStorage(["key1","key2"]) | ||
bifrostCors.getLocalStorage(["key1","key2"]) | ||
``` | ||
@@ -93,6 +104,3 @@ | ||
// return type Boolean | ||
bifrostCors.setLocalStorage("key") | ||
// return type Boolean | ||
bifrostCors.setLocalStorage(["key1","key2"]) | ||
bifrostCors.setLocalStorage({key: "user", value: "user-1"}) | ||
``` | ||
@@ -106,6 +114,6 @@ | ||
// return type Boolean | ||
bifrostCors.deleteLocalStorage(["key1","key2"]) | ||
bifrostCors.deleteLocalStorage(["key1","key2"]) | ||
``` | ||
* Bi-directional message thread | ||
* __Bi-directional message thread__ | ||
* __Request Message Thread__ | ||
@@ -134,3 +142,3 @@ ```javascript | ||
* Run JS expression | ||
* __Run JS expression__ | ||
```javascript | ||
@@ -140,3 +148,3 @@ // return type Boolean, parameter type string | ||
``` | ||
* DOM Manipulation | ||
* __DOM Manipulation__ | ||
* __DOM Manipulation by ID__ | ||
@@ -159,3 +167,3 @@ ```javascript | ||
if you can access element by | ||
document.getElementsByClassName("myElementClass)[4]<br/> | ||
```document.getElementsByClassName("myElementClass)[4]```<br/> | ||
so parameter will be <br/> | ||
@@ -162,0 +170,0 @@ * class "myElementClass" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
115464
6
284
1
161