soiz1 commited on
Commit
f3de9cc
·
verified ·
1 Parent(s): dcf2a20

Update src/addons/addons/save-to-google/userscript.js

Browse files
src/addons/addons/save-to-google/userscript.js CHANGED
@@ -30,16 +30,28 @@ export default async ({ addon, console, msg }) => {
30
 
31
  const loginButton = modal.content.querySelector("#google-login-button");
32
  if (loginButton) {
33
- loginButton.addEventListener("click", async () => {
34
- try {
35
- const token = await authenticateWithGoogle();
36
- if (token) {
37
- await saveToGoogleDrive(token, modal.remove);
 
 
 
 
 
38
  }
39
- } catch (error) {
40
- console.error("認証エラー:", error);
41
- addon.tab.showAlert("Googleドライブへの保存に失敗しました", { type: "error" });
42
- }
 
 
 
 
 
 
 
43
  });
44
  }
45
 
@@ -51,35 +63,6 @@ export default async ({ addon, console, msg }) => {
51
  }
52
  }
53
 
54
- async function authenticateWithGoogle() {
55
- return new Promise((resolve) => {
56
- const authUrl = `https://accounts.google.com/o/oauth2/auth?` +
57
- `client_id=${CLIENT_ID}` +
58
- `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
59
- `&response_type=token` +
60
- `&scope=${encodeURIComponent(SCOPES)}`;
61
-
62
- const authWindow = window.open(authUrl, "_blank", "width=500,height=600");
63
-
64
- const checkAuth = setInterval(() => {
65
- try {
66
- if (authWindow.closed) {
67
- clearInterval(checkAuth);
68
- const token = getAccessTokenFromUrl();
69
- if (token) {
70
- resolve(token);
71
- } else {
72
- resolve(null);
73
- }
74
- }
75
- } catch (error) {
76
- clearInterval(checkAuth);
77
- resolve(null);
78
- }
79
- }, 500);
80
- });
81
- }
82
-
83
  async function saveToGoogleDrive(accessToken, onSuccess) {
84
  try {
85
  const blob = await window.vm.saveProjectSb3();
@@ -116,11 +99,4 @@ export default async ({ addon, console, msg }) => {
116
  throw error;
117
  }
118
  }
119
-
120
- function getAccessTokenFromUrl() {
121
- // リダイレクトURIからトークンを取得するロジックを実装
122
- // 実際にはこの方法は推奨されませんが、元の機能を維持するために残しています
123
- const params = new URLSearchParams(window.location.hash.substring(1));
124
- return params.get("access_token");
125
- }
126
  };
 
30
 
31
  const loginButton = modal.content.querySelector("#google-login-button");
32
  if (loginButton) {
33
+ loginButton.addEventListener("click", () => {
34
+ // メッセージイベントリスナーを追加
35
+ const messageListener = (event) => {
36
+ if (event.origin === "https://soiz1-penguin-upload.hf.space" && event.data.token) {
37
+ window.removeEventListener("message", messageListener);
38
+ saveToGoogleDrive(event.data.token, modal.remove)
39
+ .catch(error => {
40
+ console.error("保存エラー:", error);
41
+ addon.tab.showAlert("Googleドライブへの保存に失敗しました", { type: "error" });
42
+ });
43
  }
44
+ };
45
+ window.addEventListener("message", messageListener);
46
+
47
+ // OAuthポップアップを開く
48
+ const authUrl = `https://accounts.google.com/o/oauth2/auth?` +
49
+ `client_id=${CLIENT_ID}` +
50
+ `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
51
+ `&response_type=token` +
52
+ `&scope=${encodeURIComponent(SCOPES)}`;
53
+
54
+ window.open(authUrl, "_blank", "width=500,height=600");
55
  });
56
  }
57
 
 
63
  }
64
  }
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  async function saveToGoogleDrive(accessToken, onSuccess) {
67
  try {
68
  const blob = await window.vm.saveProjectSb3();
 
99
  throw error;
100
  }
101
  }
 
 
 
 
 
 
 
102
  };