File size: 3,735 Bytes
857cdcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// preload-webview.js

const { contextBridge, ipcRenderer } = require('electron');

console.log('[PRELOAD] Preload script for Webview is running!');

// --- สร้างสะพานสื่อสารระหว่าง Webview กับแอปหลัก (Host) ---
const WebViewBridge = {
  // ฟังก์ชันสำหรับส่งข้อความจาก "เว็บ" ไปยัง "แอป"
  sendToApp: (channel, data) => {
    console.log(`[WebView -> App] Sending message on channel "${channel}"`, data);
    ipcRenderer.sendToHost(channel, data);
  },
  // ฟังก์ชันสำหรับรับข้อความจาก "แอป" มายัง "เว็บ"
  receiveFromApp: (channel, func) => {
    ipcRenderer.on(channel, (event, ...args) => func(...args));
  },

  // [CLEANED] Removed all authentication functions - will be reimplemented

  // Token Injection System
  injectAuthTokens: (tokens) => {
    console.log('[WebView Bridge] Injecting auth tokens');
    if (tokens && tokens.accessToken) {
      localStorage.setItem('auth_token', tokens.accessToken);
      localStorage.setItem('user_info', JSON.stringify(tokens.user));
      // Trigger auth update event
      window.dispatchEvent(new CustomEvent('auth:tokens-injected', {
        detail: tokens
      }));
      console.log('[WebView Bridge] Tokens injected successfully');
    }
  },

  // Clear stored tokens
  clearAuthTokens: () => {
    console.log('[WebView Bridge] Clearing auth tokens');
    localStorage.removeItem('auth_token');
    localStorage.removeItem('user_info');
    window.dispatchEvent(new CustomEvent('auth:tokens-cleared'));
  },

  // เพิ่มฟังก์ชันการรีเฟรชหน้า
  reloadPage: () => {
    console.log('[WebView Bridge] Reloading page...');
    window.location.reload();
  }
};

// เปิด API ให้หน้าเว็บใช้ได้
contextBridge.exposeInMainWorld('desktopApp', WebViewBridge);

// สำหรับ backward compatibility
contextBridge.exposeInMainWorld('electronBridge', {
  sendToHost: (channel, data) => {
    ipcRenderer.sendToHost(channel, data);
  },
  onMessageFromHost: (channel, func) => {
    ipcRenderer.on(channel, (event, ...args) => func(...args));
  }
});

//  [CLEANED] Removed authentication bridge API - will be reimplemented

//  เพิ่ม: Global Desktop API สำหรับตรวจจับ Desktop Environment
contextBridge.exposeInMainWorld('desktopEnvironment', {
  isDesktop: true,
  platform: process.platform,
  version: '1.0.0'
});

console.log('[SUCCESS] [Preload-Webview] Bridge is ready.');

// [CLEANED] Removed OAuth button interceptor and token sync system - will be reimplemented

// Auto-inject tokens when webview loads
ipcRenderer.on('auth:inject-tokens', (event, tokens) => {
  console.log('[Webview] Received tokens to inject:', tokens);
  WebViewBridge.injectAuthTokens(tokens);
});

// Listen for auth clear requests
ipcRenderer.on('auth:clear-tokens', () => {
  console.log('[Webview] Clearing tokens');
  WebViewBridge.clearAuthTokens();
});

// Auto-inject on page load if chahuadev.com
window.addEventListener('DOMContentLoaded', () => {
  if (window.location.hostname.includes('chahuadev')) {
    console.log('[Webview] On chahuadev.com - requesting token injection');
    ipcRenderer.sendToHost('webview:request-auth-tokens');
  }
});

console.log('[INIT] Preload script initialized successfully');

console.log('[WEBVIEW PRELOAD] Bridge initialized!');
console.log('[WEBVIEW PRELOAD] Desktop API available at window.desktopApp');
console.log('[WEBVIEW PRELOAD] Desktop Environment available at window.desktopEnvironment');