質問編集履歴

3

ご指摘を受けてwindow.addEventListener内のtry-catch文を外しました

2025/01/16 13:38

投稿

Kurimeshi
Kurimeshi

スコア15

test CHANGED
File without changes
test CHANGED
@@ -131,18 +131,14 @@
131
131
  column_number: event.colno || null,
132
132
  };
133
133
 
134
- try {
135
- await fetch('/log_error', {
134
+ await fetch('/log_error', {
136
- method: 'POST',
135
+ method: 'POST',
137
- headers: {
136
+ headers: {
138
- 'Content-Type': 'application/json',
137
+ 'Content-Type': 'application/json',
139
- },
138
+ },
140
- body: JSON.stringify(errorData),
139
+ body: JSON.stringify(errorData),
141
- });
140
+ });
142
- console.log('Error logged successfully:', errorData);
141
+ console.log('Error logged successfully:', errorData);
143
- } catch (err) {
144
- console.error('Failed to send error log:', err);
145
- }
146
142
  });
147
143
 
148
144
  // エラーハンドリングを追加

2

ご指摘を受けて、ソースコードの一部(変数名をcodeで統一、test_code関数をコメントアウトしindexでget,post両方に対応)を変更しました

2025/01/16 05:33

投稿

Kurimeshi
Kurimeshi

スコア15

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,7 @@
24
24
  ### 該当のソースコード
25
25
 
26
26
  ```test.py
27
+
27
28
  from flask import Flask, render_template, request, jsonify, session, redirect, url_for
28
29
  from flask_sqlalchemy import SQLAlchemy
29
30
  from datetime import datetime
@@ -39,6 +40,9 @@
39
40
  error_log = "sqlite:///" + os.path.join(base_dir, "error_log.sqlite")
40
41
  app.config["SQLALCHEMY_DATABASE_URI"] = error_log
41
42
  app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
43
+
44
+ from jinja2 import StrictUndefined
45
+ app.jinja_env.undefined = StrictUndefined
42
46
 
43
47
  db = SQLAlchemy(app)
44
48
 
@@ -58,16 +62,18 @@
58
62
  if request.method == "POST":
59
63
  code = request.form.get("code")
60
64
  session["code"] = code
61
- return redirect(url_for("test_code"))
65
+ print(code)
62
- return render_template("view_test.html")
66
+ return render_template("view_test.html", code=code)
67
+ return render_template("view_test.html", code = "")
63
68
 
64
- @app.route("/test_code", methods=["GET"])
69
+ # @app.route("/test_code", methods=["GET"])
65
- def test_code():
70
+ # def test_code():
66
- code = session.get("code", "")
71
+ # code = session.get("code", "")
67
- return render_template("view_test.html", code=code)
72
+ # return render_template("view_test.html", code=code)
68
73
 
69
74
  @app.route('/log_error', methods=['POST'])
70
75
  def log_error():
76
+ print("log_error")
71
77
  try:
72
78
  data = request.get_json()
73
79
  error_name = data.get('error_name', 'Unknown Error')
@@ -95,9 +101,11 @@
95
101
  with app.app_context():
96
102
  db.create_all()
97
103
  app.run(debug=True)
104
+
98
105
  ```
99
106
 
100
107
  ```view_test.html
108
+
101
109
  <!DOCTYPE html>
102
110
  <html lang="en">
103
111
  <head>
@@ -108,8 +116,8 @@
108
116
  <body>
109
117
  <h1>JavaScript Execution Test</h1>
110
118
  <form method="POST" enctype="multipart/form-data">
111
- <label for="js_code">JavaScript Code:</label><br>
119
+ <label for="code">JavaScript Code:</label><br>
112
- <textarea id="js_code" name="js_code" rows="10" cols="50"></textarea><br>
120
+ <textarea id="code" name="code" rows="10" cols="50"></textarea><br>
113
121
  <input type="submit" value="Execute">
114
122
  </form>
115
123
 
@@ -140,7 +148,7 @@
140
148
  // エラーハンドリングを追加
141
149
  try {
142
150
  console.log("test1");
143
- const jsCode = `{{ js_code|safe }}`;
151
+ const jsCode = `{{ code|safe }}`;
144
152
  eval(jsCode);
145
153
  } catch (error) {
146
154
  console.log("test2")

1

実行時のログを追記しました

2025/01/15 09:21

投稿

Kurimeshi
Kurimeshi

スコア15

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,19 @@
6
6
 
7
7
  ### エラーメッセージ
8
8
  ```error
9
- 特にエラーは発生しませんでした
9
+ 特にエラーは発生しなかったのですが,ログはこな感じでした
10
+ * Serving Flask app 'test'
11
+ * Debug mode: on
12
+ WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
13
+ * Running on http://127.0.0.1:5000
14
+ Press CTRL+C to quit
15
+ * Restarting with stat
16
+ * Debugger is active!
17
+ * Debugger PIN: 119-491-145
18
+ 127.0.0.1 - - [15/Jan/2025 15:16:39] "POST /test_code HTTP/1.1" 405 -
19
+ 127.0.0.1 - - [15/Jan/2025 15:16:44] "GET / HTTP/1.1" 200 -
20
+ 127.0.0.1 - - [15/Jan/2025 15:16:48] "POST / HTTP/1.1" 302 -
21
+ 127.0.0.1 - - [15/Jan/2025 15:16:48] "GET /test_code HTTP/1.1" 200 -
10
22
  ```
11
23
 
12
24
  ### 該当のソースコード