File size: 8,323 Bytes
494c89b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Script Collector - collects all JS scripts from pages during registration.

Usage:
    from debugger.collectors.script_collector import ScriptCollector
    
    collector = ScriptCollector()
    collector.collect(page)  # Call after each page load
    collector.save()  # Save at the end
"""

import json
import hashlib
import base64
from pathlib import Path
from datetime import datetime
from urllib.parse import urlparse
from typing import Optional


class ScriptCollector:
    """Collects all JS scripts from browser pages."""
    
    _instance: Optional['ScriptCollector'] = None
    
    def __init__(self, output_dir: Path = None):
        if output_dir is None:
            from core.paths import get_paths
            paths = get_paths()
            timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
            output_dir = paths.debug_sessions_dir / 'aws_scripts' / timestamp
        
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(parents=True, exist_ok=True)
        self.collected = {}  # url -> filepath
        self.page_count = 0
        self.enabled = True
        
        print(f"[ScriptCollector] Output: {self.output_dir}")
    
    @classmethod
    def get_instance(cls) -> 'ScriptCollector':
        """Get singleton instance."""
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance
    
    @classmethod
    def reset(cls):
        """Reset singleton (for new registration session)."""
        if cls._instance:
            cls._instance.save()
        cls._instance = None
    
    def collect(self, page, page_name: str = None) -> int:
        """
        Collect all JS scripts from current page.
        
        Args:
            page: DrissionPage ChromiumPage instance
            page_name: Optional name for this page (auto-generated if not provided)
            
        Returns:
            Number of new scripts collected
        """
        if not self.enabled:
            return 0
            
        self.page_count += 1
        url = page.url
        
        if not page_name:
            parsed = urlparse(url)
            page_name = f"{self.page_count:02d}_{parsed.netloc.replace('.', '_')}"
        
        print(f"[ScriptCollector] Page {self.page_count}: {url[:60]}...")
        
        # Get all script URLs from page
        try:
            js_urls = page.run_js('''
                const urls = new Set();
                
                // Script tags with src
                document.querySelectorAll('script[src]').forEach(s => {
                    if (s.src) urls.add(s.src);
                });
                
                // Performance API - catches dynamically loaded scripts
                performance.getEntriesByType('resource').forEach(r => {
                    if (r.name.includes('.js') || r.initiatorType === 'script') {
                        urls.add(r.name);
                    }
                });
                
                return Array.from(urls);
            ''') or []
        except Exception as e:
            print(f"[ScriptCollector] Error getting scripts: {e}")
            js_urls = []
        
        print(f"[ScriptCollector] Found {len(js_urls)} script URLs")
        
        # Create page directory
        page_dir = self.output_dir / page_name
        page_dir.mkdir(exist_ok=True)
        
        new_count = 0
        for js_url in js_urls:
            if js_url in self.collected:
                continue
            
            try:
                # Use CDP to fetch script content (bypasses CORS)
                result = page.run_cdp('Network.loadNetworkResource', 
                    frameId=page.run_cdp('Page.getFrameTree')['frameTree']['frame']['id'],
                    url=js_url,
                    options={'disableCache': False, 'includeCredentials': False}
                )
                
                content = None
                if result.get('resource', {}).get('success'):
                    # Content might be base64 encoded
                    stream = result['resource'].get('stream')
                    if stream:
                        # Read from stream
                        data = page.run_cdp('IO.read', handle=stream)
                        content = data.get('data', '')
                        if data.get('base64Encoded'):
                            content = base64.b64decode(content).decode('utf-8', errors='ignore')
                        page.run_cdp('IO.close', handle=stream)
                    else:
                        content = result['resource'].get('content', '')
                
                if not content or len(content) < 50:
                    # Fallback: try regular fetch
                    content = page.run_js(f'''
                        return await fetch("{js_url}").then(r => r.text()).catch(() => null);
                    ''')
                
                if content and len(content) > 50:
                    # Generate safe filename
                    parsed = urlparse(js_url)
                    filename = parsed.path.split('/')[-1] or 'script.js'
                    filename = filename.split('?')[0]
                    if not filename.endswith('.js'):
                        filename += '.js'
                    
                    # Add hash for uniqueness
                    url_hash = hashlib.md5(js_url.encode()).hexdigest()[:8]
                    filename = f"{url_hash}_{filename}"
                    
                    # Save script
                    filepath = page_dir / filename
                    filepath.write_text(content, encoding='utf-8')
                    
                    self.collected[js_url] = str(filepath)
                    new_count += 1
                    
                    size_kb = len(content) / 1024
                    print(f"[ScriptCollector] ✓ {filename} ({size_kb:.1f}KB)")
                    
            except Exception as e:
                # Try simple approach as last resort
                try:
                    content = page.run_js(f'return await fetch("{js_url}").then(r=>r.text()).catch(()=>null)')
                    if content and len(content) > 50:
                        parsed = urlparse(js_url)
                        filename = parsed.path.split('/')[-1] or 'script.js'
                        filename = filename.split('?')[0]
                        if not filename.endswith('.js'):
                            filename += '.js'
                        url_hash = hashlib.md5(js_url.encode()).hexdigest()[:8]
                        filename = f"{url_hash}_{filename}"
                        filepath = page_dir / filename
                        filepath.write_text(content, encoding='utf-8')
                        self.collected[js_url] = str(filepath)
                        new_count += 1
                        print(f"[ScriptCollector] ✓ {filename} (fallback)")
                except:
                    pass
        
        print(f"[ScriptCollector] Downloaded {new_count} new scripts")
        return new_count
    
    def save(self) -> Path:
        """Save index of all collected scripts."""
        index = {
            'timestamp': datetime.now().isoformat(),
            'pages': self.page_count,
            'total_scripts': len(self.collected),
            'scripts': self.collected
        }
        
        index_file = self.output_dir / 'index.json'
        with open(index_file, 'w', encoding='utf-8') as f:
            json.dump(index, f, indent=2, ensure_ascii=False)
        
        print(f"[ScriptCollector] Saved index: {index_file}")
        print(f"[ScriptCollector] Total: {len(self.collected)} scripts from {self.page_count} pages")
        
        return index_file
    
    def disable(self):
        """Disable collection."""
        self.enabled = False
    
    def enable(self):
        """Enable collection."""
        self.enabled = True


# Global function for easy access
def collect_scripts(page, page_name: str = None) -> int:
    """Collect scripts from page using singleton collector."""
    return ScriptCollector.get_instance().collect(page, page_name)


def save_collected_scripts() -> Path:
    """Save all collected scripts."""
    return ScriptCollector.get_instance().save()