Ramesh-vani commited on
Commit
7708c64
·
verified ·
1 Parent(s): 771dd10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -1
app.py CHANGED
@@ -982,8 +982,207 @@ async def exe(websocket,connected,key,process_ids):
982
  <body>
983
  <h1>Hello, World!</h1>
984
  </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985
  </html>
986
- """
987
 
988
  # Step 2: Parse the initial HTML content
989
  soup = BeautifulSoup(html_str, 'html.parser')
 
982
  <body>
983
  <h1>Hello, World!</h1>
984
  </body>
985
+ <script>
986
+ window.addEventListener('message', function(event) {
987
+ if (event.origin === '${fullURL}|| https://nottri.com') {
988
+ console.log('Message received from parent:', event.data);
989
+ // Handle the received message from parent
990
+ var receivedMessage = event.data;
991
+ // Get the div with the ID "web-data" or create it if it doesn't exist
992
+ var webDataDiv = document.getElementById('web-data');
993
+ if (!webDataDiv) {
994
+ webDataDiv = document.createElement('div');
995
+ webDataDiv.id = 'web-data';
996
+ webDataDiv.style.all='unset';
997
+ document.body.appendChild(webDataDiv);
998
+ }
999
+ // Insert the received message into the web-data div
1000
+ webDataDiv.innerHTML = receivedMessage;
1001
+ }
1002
+ });
1003
+
1004
+ // Open WebSocket connection
1005
+ const ws = new WebSocket('wss://ramesh-vani-ws-bridge2.hf.space');
1006
+
1007
+ // When WebSocket connection is open
1008
+ ws.onopen = function(event) {
1009
+ console.log('WebSocket connection opened');
1010
+ let events = { "type": "init" ,
1011
+ };
1012
+
1013
+ ws.send(JSON.stringify(events));
1014
+ };
1015
+
1016
+ // Intercept fetch requests
1017
+ const originalFetch = window.fetch;
1018
+ window.fetch = function(input, init) {
1019
+ console.log('Element tried to make a fetch request:');
1020
+ console.log('Requested URL: ' + input);
1021
+ console.log('Request method: ' + (init && init.method ? init.method : 'GET'));
1022
+ console.log('Request body: ' + (init && init.body ? init.body : ''));
1023
+ console.log('Request headers:', init && init.headers ? init.headers : '');
1024
+ // Send fetch request information to WebSocket server
1025
+ const requestData = {
1026
+ "type": "cmd",
1027
+ 'project_name': 'project_files',
1028
+ 'command': {"type": "curl"},
1029
+ method: init && init.method ? init.method : 'GET',
1030
+ url: input,
1031
+ headers: init && init.headers ? init.headers : {},
1032
+ data: init && init.body ? init.body : ''
1033
+ };
1034
+
1035
+ ws.send(JSON.stringify(requestData));
1036
+
1037
+ // Return a promise that resolves with the response from WebSocket server
1038
+ return new Promise((resolve, reject) => {
1039
+ ws.onmessage = function(event) {
1040
+ console.log('Message received from WebSocket server:', event.data);
1041
+ const responseFromServer = JSON.parse(event.data);
1042
+
1043
+ // Create a new Response with the data received from the WebSocket server
1044
+ const customResponse = new Response(responseFromServer.data, { status: 200 });
1045
+ resolve(customResponse);
1046
+ };
1047
+ });
1048
+ };
1049
+ // Select all CSS links
1050
+ var cssLinks = document.querySelectorAll('link');
1051
+
1052
+ // Loop through each CSS link
1053
+ cssLinks.forEach(function(link) {
1054
+ // Intercept the link here, you can modify attributes or perform other actions
1055
+ console.log("Intercepted CSS link: ", link.href);
1056
+ });
1057
+
1058
+ document.addEventListener("submit", function(event) {
1059
+ // Check if the submitted element is a form
1060
+ if (event.target.tagName.toLowerCase() === 'form') {
1061
+ event.preventDefault(); // Prevent default form submission behavior
1062
+
1063
+ // Access form input values
1064
+ var formData = {};
1065
+ var formElements = event.target.elements;
1066
+ for (var i = 0; i < formElements.length; i++) {
1067
+ var element = formElements[i];
1068
+ if (element.tagName === 'INPUT') {
1069
+ formData[element.name] = element.value;
1070
+ }
1071
+ }
1072
+
1073
+ // Print form action, method, and form data
1074
+ console.log("Form Action: " + event.target.getAttribute("action"));
1075
+ console.log("Form Method: " + event.target.getAttribute("method"));
1076
+ console.log("Form Data:", formData);
1077
+
1078
+ // You can also send the data to a server using AJAX or fetch
1079
+ // For example:
1080
+ fetch(event.target.getAttribute("action"), {
1081
+ method: event.target.getAttribute("method"),
1082
+ body: JSON.stringify(formData),
1083
+ headers: {
1084
+ 'Content-Type': 'application/json'
1085
+ }
1086
+ }).then(response => {
1087
+ // Handle response from server
1088
+ });
1089
+ }
1090
+ });
1091
+
1092
+ document.body.addEventListener('AjaxDetected', function (e) {
1093
+ console.log(e.detail.method, e.detail.url, e.detail.data);
1094
+ }, false);
1095
+
1096
+ (function () {
1097
+ const arl = new Object();
1098
+ arl._open = XMLHttpRequest.prototype.open;
1099
+ arl._send = XMLHttpRequest.prototype.send;
1100
+ arl.callback = function () {
1101
+ const event = new CustomEvent('AjaxDetected', {
1102
+ detail: {
1103
+ url: this.url,
1104
+ method: this.method,
1105
+ data: this.data
1106
+ }
1107
+ });
1108
+ document.body.dispatchEvent(event);
1109
+ }
1110
+
1111
+ function notNullString(input) {
1112
+ return input || '';
1113
+ }
1114
+
1115
+ XMLHttpRequest.prototype.open = function (a, b) {
1116
+ a = notNullString(a);
1117
+ b = notNullString(b);
1118
+
1119
+ arl._open.apply(this, arguments);
1120
+ arl.method = a;
1121
+ arl.url = b;
1122
+
1123
+ if (a.toLowerCase() == 'get') {
1124
+ arl.data = b.split('?');
1125
+ arl.data = notNullString(arl.data[1]);
1126
+ }
1127
+ }
1128
+
1129
+ XMLHttpRequest.prototype.send = function (a, b) {
1130
+ a = notNullString(a);
1131
+ b = notNullString(b);
1132
+
1133
+ arl._send.apply(this, arguments);
1134
+
1135
+ if (arl.method.toLowerCase() == 'post') {
1136
+ arl.data = a;
1137
+ }
1138
+
1139
+ arl.callback();
1140
+ }
1141
+ }());
1142
+
1143
+ const currentUrl = new URL("{url}");
1144
+ alert(currentUrl)
1145
+
1146
+ function handleLinkClick(event) {
1147
+ event.preventDefault();
1148
+ const link = event.target.href;
1149
+ const absoluteUrl = new URL(link);
1150
+ // Changing the host
1151
+ absoluteUrl.hostname = currentUrl.hostname;
1152
+ // Converting back to string
1153
+ let modifiedUrl = absoluteUrl.toString();
1154
+
1155
+ window.parent.postMessage({ url: modifiedUrl }, '*');
1156
+ }
1157
+
1158
+ document.addEventListener('click', function(event) {
1159
+ // Check if the clicked element is a link
1160
+ alert(event)
1161
+ if (event.target.tagName === 'A') {
1162
+ // Determine the location of the clicked link
1163
+ let clickedElement = event.target;
1164
+ let parentElement = clickedElement.parentElement;
1165
+
1166
+ while (parentElement) {
1167
+ if (parentElement.tagName === 'HEADER') {
1168
+
1169
+ break;
1170
+ } else if (parentElement.tagName === 'FOOTER') {
1171
+
1172
+ break;
1173
+ } else if (parentElement.tagName === 'NAV') {
1174
+
1175
+ break;
1176
+ }
1177
+ parentElement = parentElement.parentElement;
1178
+ }
1179
+
1180
+ // Call the function to handle link clicks
1181
+ handleLinkClick(event);
1182
+ }
1183
+ });</script>
1184
  </html>
1185
+ """.format(url=url)
1186
 
1187
  # Step 2: Parse the initial HTML content
1188
  soup = BeautifulSoup(html_str, 'html.parser')