Path: blob/master/AndroidRunner/Plugins/perfume_js/AddJS.py
630 views
import os, sys1import subprocess2from distutils.dir_util import copy_tree3from bs4 import BeautifulSoup45def bfs_dirs(root):6paths = [root]78while len(paths) > 0:9children = []1011for parent in paths:12if parent.endswith(".gitignore"):13continue1415for element in os.listdir(parent):16found_element = os.path.join(parent, element)17if os.path.isdir(found_element) and "googletagmanager" not in found_element:18children.append(found_element)19elif found_element.endswith("index.html") or found_element.endswith("index.htm"):#The entrance html file to the webpage needs to be called index.html or index.htm otherwise it won't add the JS code to read load times20if("index.html" in os.listdir(parent) and not(found_element.endswith("index.html"))):21continue22else:23return found_element24paths = children2526return root2728def add_js(directory, ip):29for cat in os.listdir(directory):30if cat == ".DS_Store" or cat == "RUN_WEBAPPS_README.md" or cat == "START_SERVER_IN_THIS_DIR":31continue3233for site in os.listdir(directory + cat):34if site == ".DS_Store":35continue3637path_to_html = bfs_dirs('{}{}/{}'.format(directory, cat, site))38if(path_to_html == '{}{}/{}'.format(directory, cat, site)):39continue40path_to_src = os.path.dirname(path_to_html)41print(path_to_html)42soup = BeautifulSoup(open(path_to_html, 'rb'), "lxml")43if(soup.find('head')):44perfumeSource = soup.new_tag('script')45perfumeSource['src'] ="/node_modules/perfume.js/dist/perfume.umd.min.js"46script = soup.new_tag('script')47script.string = "perfumeResults = []; function xml_http_post(url, data, callback) {var req = new XMLHttpRequest(); req.open(\"POST\", url, true); req.send(data);} const perfume = new Perfume({ analyticsTracker: (options) => { const { metricName, data, eventProperties, navigatorInformation } = options; perfumeResults.push(options); } }); function load_log() { setTimeout(function(){ objectToSend = \"{'perfumeResults':\"+JSON.stringify(perfumeResults)+\"}\"; xml_http_post(\""+ip+"\",objectToSend,null); },5000); };window.addEventListener ?window.addEventListener(\"load\",load_log, true) : window.attachEvent && window.attachEvent(\"onload\", load_log);"48soup.head.insert(0, perfumeSource)49soup.head.insert(1,script)5051with open(path_to_html, "w") as file:52file.write(str(soup))5354if __name__ == '__main__':55#USAGE: (python3 AddJS.py path/To/Directory/With/All/WebApplication/ IPADRRESS)56#NOTE: IPADDRESS should be in the form of http://IP:8080/ and IP is the ip address57add_js(sys.argv[1], sys.argv[2])585960