Ramesh-vani commited on
Commit
6379148
·
verified ·
1 Parent(s): 27992d9

Update app.py

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