import { DOM } from "./dom" ; import devices from "@shared/constants/devices" ; import { iDevice } from "@shared/types/extension/device" ; import * as url from "url" ; export function executeScript ( name , tabId , cb ? ) { return new Promise ( ( resolve , reject ) => { if ( process . env . NODE_ENV === "production" ) { chrome . tabs . executeScript ( tabId , { file : `/js/${ name }.js` , runAt : "document_end" } , ( ) => { resolve ( true ) ; } ) ; } else { fetch ( `http://localhost:2400/${ name }.js` ) . then ( ( res ) => res . text ( ) ) . then ( ( fetchRes ) => { chrome . tabs . executeScript ( tabId , { code : fetchRes , runAt : "document_end" } , ( ) => { resolve ( true ) ; } ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } } ) ; } export function submitPostDataWithForm ( url , options = { } ) { console . log ( "URL IS" , url ) ; const form = document . createElement ( "form" ) ; form . method = "post" ; form . action = url ; form . target = "_blank" ; const optionKeys = Object . keys ( options ) ; for ( const optionKey of optionKeys ) { const hiddenField = document . createElement ( "input" ) ; hiddenField . type = "hidden" ; hiddenField . name = optionKey ; hiddenField . value = options [ optionKey ] ; form . appendChild ( hiddenField ) ; } document . body . appendChild ( form ) ; form . submit ( ) ; form . remove ( ) ; } interface iElementAttributeInfo { name : string ; value : any ; } export function getAllAttributes ( element ) { if ( ! DOM . isElement ( element ) ) { throw new Error ( "Invalid element provided." ) ; } const attributeNamesArr = element . getAttributeNames ( ) ; return [ ... attributeNamesArr . map ( ( attributeName ) => { return { name : attributeName , value : element . getAttribute ( attributeName ) , } ; } ) , ] ; } export function getDevice ( deviceId ) { return devices . find ( ( device ) => { return device . id === deviceId ; } ) ; } export function getFrameDepth ( winToID ) { if ( winToID === window . top ) { return 0 ; } else if ( winToID . parent === window . top ) { return 1 ; } return 1 + getFrameDepth ( winToID . parent ) ; } export function toPascalCase ( str ) { if ( ! str || typeof str !== "string" ) { return str ; } let outString = str ?. toLowerCase ( ) ; outString = outString [ 0 ] ?. toUpperCase ( ) + outString ?. substr ( 1 ) ; return outString ; } export function toPrettyEventName ( eventName ) { return eventName . split ( "_" ) . map ( ( name ) => toPascalCase ( name ) ) . join ( " " ) ; } export function pxToRemValue ( pxValue ) { return `${ pxValue / 16 }rem` ; } export function validURL ( str ) { const pattern = new RegExp ( "^(https?:\\/\\/)?" + "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + "((\\d{1,3}\\.){3}\\d{1,3}))" + "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + "(\\?[;&a-z\\d%_.~+=-]*)?" + "(\\#[-a-z\\d_]*)?$" , "i" ) ; const localHostRegex = new RegExp ( "(localhost|127.0.0.1)(:\\d*)?" ) ; return ! ! pattern . test ( str ) || ! ! localHostRegex . test ( str ) ; } export function resolveToFrontend ( relativePath ) { return url . resolve ( process . env . FRONTEND_URL , relativePath ) ; } export function getNodeLevel ( node ) { if ( ! document . body . contains ( node ) && node !== document . body ) return null ; let depth = 0 ; while ( node != document . body ) { node = node . parentNode ; depth ++ ; } return depth ; } export function findDistanceBetweenNodes ( node1 , node2 ) { return Math . abs ( getNodeLevel ( node1 ) - getNodeLevel ( node2 ) ) ; }