File size: 24,205 Bytes
95844bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f57838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95844bd
 
 
 
 
 
 
 
 
e557612
95844bd
 
 
e557612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95844bd
 
 
44472af
 
 
95844bd
 
 
 
 
 
 
 
 
 
44472af
 
 
95844bd
 
 
e557612
44472af
e557612
44472af
e557612
 
 
 
44472af
 
95844bd
 
 
 
 
 
 
 
44472af
 
 
 
 
 
 
 
 
95844bd
44472af
 
 
 
 
 
95844bd
 
 
 
 
 
44472af
95844bd
 
44472af
 
95844bd
 
 
 
44472af
95844bd
 
 
44472af
95844bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44472af
95844bd
 
 
 
44472af
 
 
95844bd
 
 
44472af
0f57838
95844bd
 
44472af
 
e557612
 
 
44472af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95844bd
 
44472af
 
95844bd
 
44472af
 
 
95844bd
 
 
 
44472af
 
95844bd
 
 
 
 
 
44472af
 
95844bd
 
44472af
 
 
95844bd
44472af
 
 
95844bd
 
 
 
 
44472af
 
95844bd
 
 
 
44472af
 
95844bd
44472af
 
95844bd
 
 
44472af
 
 
95844bd
 
 
 
 
44472af
 
95844bd
 
 
44472af
 
 
 
 
 
 
95844bd
 
 
44472af
 
 
 
95844bd
 
 
 
44472af
95844bd
44472af
 
 
e557612
 
44472af
 
 
 
 
e557612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95844bd
 
 
 
 
 
 
e557612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95844bd
 
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Google Dorks Database
const dorks = [
    // Sensitive Files
    {
        id: 1,
        category: 'sensitive-files',
        title: 'SQL Database Files',
        query: 'site:{target} ext:sql | ext:db | ext:sqlite | ext:sqlite3 | ext:mdb | ext:dbf',
        description: 'Find exposed database files',
        icon: 'database'
    },
    {
        id: 2,
        category: 'sensitive-files',
        title: 'Configuration Files',
        query: 'site:{target} ext:xml | ext:conf | ext:cnf | ext:reg | ext:inf | ext:rdp | ext:cfg | ext:ini | ext:env',
        description: 'Exposed configuration and settings files',
        icon: 'file-cog'
    },
    {
        id: 3,
        category: 'sensitive-files',
        title: 'Log Files',
        query: 'site:{target} ext:log',
        description: 'Find log files with sensitive data',
        icon: 'file-text'
    },
    {
        id: 4,
        category: 'sensitive-files',
        title: 'Backup & Old Files',
        query: 'site:{target} ext:bkf | ext:bkp | ext:bak | ext:old | ext:backup | ext:save',
        description: 'Backup and old file versions',
        icon: 'archive'
    },
    {
        id: 5,
        category: 'sensitive-files',
        title: 'Password Files',
        query: 'site:{target} intitle:index.of "password.txt" | "passwd" | "credentials"',
        description: 'Search for password files in directory listings',
        icon: 'key'
    },
    {
        id: 6,
        category: 'sensitive-files',
        title: 'Git Repositories',
        query: 'inurl:.git site:{target} "index of" | intitle:"Index of /.git"',
        description: 'Exposed Git repositories',
        icon: 'git-branch'
    },
    
    // Vulnerabilities
    {
        id: 7,
        category: 'vulnerabilities',
        title: 'SQL Injection Parameters',
        query: 'site:{target} inurl:".php?id=" | inurl:".asp?id=" | inurl:"/index.php?id=" | inurl:"/product.php?id="',
        description: 'Potential SQL injection points',
        icon: 'alert-triangle'
    },
    {
        id: 8,
        category: 'vulnerabilities',
        title: 'XSS Parameters',
        query: 'site:{target} inurl:"?q=" | inurl:"?search=" | inurl:"?query=" | inurl:"?s=" | inurl:"?keyword="',
        description: 'Parameters that might be vulnerable to XSS',
        icon: 'crosshair'
    },
    {
        id: 9,
        category: 'vulnerabilities',
        title: 'LFI/RFI Parameters',
        query: 'site:{target} inurl:"?file=" | inurl:"?page=" | inurl:"?path=" | inurl:"?folder=" | inurl:"?include="',
        description: 'File inclusion vulnerability parameters',
        icon: 'file-input'
    },
    {
        id: 10,
        category: 'vulnerabilities',
        title: 'Open Redirects',
        query: 'site:{target} inurl:"?url=" | inurl:"?redirect=" | inurl:"?return=" | inurl:"?next=" | inurl:"?redirect_uri="',
        description: 'Potential open redirect parameters',
        icon: 'link'
    },
    {
        id: 11,
        category: 'vulnerabilities',
        title: 'phpinfo() Pages',
        query: 'site:{target} "PHP Version" "phpinfo()" "Zend Scripting Language Engine"',
        description: 'Exposed PHP configuration pages',
        icon: 'info'
    },
    {
        id: 12,
        category: 'vulnerabilities',
        title: 'API Keys & Tokens',
        query: 'site:{target} "api_key" | "apikey" | "api_secret" | "access_token" | "auth_token" | "client_secret"',
        description: 'Hardcoded API keys and tokens',
        icon: 'key-round'
    },

    // Directories
    {
        id: 13,
        category: 'directories',
        title: 'Directory Listings',
        query: 'site:{target} intitle:"Index of" "Parent Directory" "Directory Listing"',
        description: 'Open directory indexes',
        icon: 'folder-open'
    },
    {
        id: 14,
        category: 'directories',
        title: 'Admin Panels',
        query: 'site:{target} inurl:"/admin" | inurl:"/administrator" | inurl:"/adminpanel" | inurl:"/controlpanel" | inurl:"/dashboard"',
        description: 'Potential administrative interfaces',
        icon: 'shield'
    },
    {
        id: 15,
        category: 'directories',
        title: 'Upload Folders',
        query: 'site:{target} inurl:"/upload" | inurl:"/uploads" | inurl:"/file" | inurl:"/files" intitle:"index of"',
        description: 'Exposed upload directories',
        icon: 'upload-cloud'
    },
    {
        id: 16,
        category: 'directories',
        title: 'Backup Directories',
        query: 'site:{target} inurl:"/backup" | inurl:"/backups" | inurl:"/dump" | inurl:"/dumps" | inurl:"/archive"',
        description: 'Backup and dump directories',
        icon: 'archive-restore'
    },
    {
        id: 17,
        category: 'directories',
        title: 'Source Code',
        query: 'site:{target} ext:zip | ext:tar | ext:gz | ext:rar | ext:7z "source" | "backup" | "code"',
        description: 'Compressed source code archives',
        icon: 'code-2'
    },

    // Cameras/IoT
    {
        id: 18,
        category: 'cameras',
        title: 'Live Cameras',
        query: 'inurl:"view.shtml" | inurl:"viewerframe?mode=" | inurl:"view/index.shtml" | inurl:"camera"',
        description: 'Potentially exposed live cameras',
        icon: 'video'
    },
    {
        id: 19,
        category: 'cameras',
        title: 'Webcam Interfaces',
        query: 'intitle:"webcam" | inurl:"webcam" | intitle:"live view" | inurl:"lvappl" | inurl:"live_view"',
        description: 'Webcam control interfaces',
        icon: 'webcam'
    },
    {
        id: 20,
        category: 'cameras',
        title: 'CCTV Systems',
        query: 'intitle:"CCTV" | inurl:"cctv" | intitle:"DVR" | inurl:"dvr" | intitle:"NVR" | inurl:"nvr"',
        description: 'CCTV and DVR systems',
        icon: 'scan-eye'
    },
    {
        id: 21,
        category: 'cameras',
        title: 'Network Printers',
        query: 'inurl:"printer" | intitle:"printer" | inurl:"ipp" | inurl:"print" | intitle:"HP Printer" | intitle:"Epson"',
        description: 'Exposed network printers',
        icon: 'printer'
    },
    {
        id: 22,
        category: 'cameras',
        title: 'Routers/Modems',
        query: 'intitle:"router" | inurl:"router" | intitle:"modem" | inurl:"modem" | intitle:"gateway" | inurl:"gateway"',
        description: 'Router and modem admin pages',
        icon: 'wifi'
    },

    // Login Pages
    {
        id: 23,
        category: 'login',
        title: 'Login Portals',
        query: 'site:{target} inurl:"login" | inurl:"signin" | inurl:"auth" | inurl:"account" | intitle:"login"',
        description: 'Login and authentication pages',
        icon: 'log-in'
    },
    {
        id: 24,
        category: 'login',
        title: 'WordPress Admin',
        query: 'site:{target} inurl:"wp-login" | inurl:"wp-admin" | inurl:"wp-config"',
        description: 'WordPress administration pages',
        icon: 'type'
    },
    {
        id: 25,
        category: 'login',
        title: 'cPanel/WebHost',
        query: 'site:{target} inurl:"cpanel" | inurl:"webmail" | inurl:"whm" | inurl:"webhost" | intitle:"cPanel"',
        description: 'Hosting control panels',
        icon: 'server'
    },
    {
        id: 26,
        category: 'login',
        title: 'Joomla Admin',
        query: 'site:{target} inurl:"/administrator" "Joomla" | inurl:"joomla"',
        description: 'Joomla administration panels',
        icon: 'layout-template'
    },
    {
        id: 27,
        category: 'login',
        title: 'phpMyAdmin',
        query: 'site:{target} inurl:"phpmyadmin" | inurl:"phpMyAdmin" | intitle:"phpMyAdmin"',
        description: 'Database management interfaces',
        icon: 'table'
    },

    // Active Exploits & Redirects
    {
        id: 28,
        category: 'exploits',
        title: 'SSRF Parameters',
        query: 'site:{target} inurl:"?fetch=" | inurl:"?src=" | inurl:"?load=" | inurl:"?proxy=" | inurl:"?dest=" | inurl:"?target="',
        description: 'Server-Side Request Forgery vulnerable parameters',
        icon: 'globe'
    },
    {
        id: 29,
        category: 'exploits',
        title: 'Google API Key Exposure',
        query: 'site:{target} "AIzaSy" | "google_api_key" | "GOOGLE_MAPS_API_KEY" | "gmaps_key" | "firebase_apiKey"',
        description: 'Exposed Google API keys (Maps, Firebase, etc.)',
        icon: 'key-round'
    },
    {
        id: 30,
        category: 'exploits',
        title: 'JWT & Auth Tokens',
        query: 'site:{target} "eyJ" | "jwt_secret" | "json-web-token" | "session_secret" | "SECRET_KEY" ext:env | ext:yml | ext:json',
        description: 'Exposed JWT tokens and auth secrets in files',
        icon: 'lock'
    },
    {
        id: 31,
        category: 'exploits',
        title: 'Debug/Actuator Endpoints',
        query: 'site:{target} inurl:"/debug" | inurl:"/trace" | inurl:"/env" | inurl:"/actuator" | inurl:"/metrics" | inurl:"/info"',
        description: 'Spring Boot Actuator and debug endpoints',
        icon: 'bug'
    },
    {
        id: 32,
        category: 'exploits',
        title: 'GraphQL Interfaces',
        query: 'site:{target} inurl:"/graphql" | inurl:"/graphiql" | inurl:"/playground" | intitle:"GraphiQL"',
        description: 'Exposed GraphQL interfaces and playgrounds',
        icon: 'git-graph'
    },
    {
        id: 33,
        category: 'exploits',
        title: 'Firebase Config',
        query: 'site:{target} "firebase" "apiKey" | "projectId" | "messagingSenderId" | "appId"',
        description: 'Exposed Firebase configuration with credentials',
        icon: 'flame'
    },
    {
        id: 34,
        category: 'exploits',
        title: 'OAuth Redirect Abuse',
        query: 'site:{target} inurl:"/oauth/authorize" | inurl:"/oauth/callback" | inurl:"redirect_uri=" | inurl:"response_type="',
        description: 'OAuth endpoints vulnerable to redirect abuse',
        icon: 'arrow-right-left'
    },
    {
        id: 35,
        category: 'exploits',
        title: 'Webhook URLs',
        query: 'site:{target} "https://hooks.slack.com" | "https://discord.com/api/webhooks" | "https://hooks.zapier.com" | "webhook_url"',
        description: 'Exposed webhook URLs for Slack, Discord, Zapier',
        icon: 'link-2'
    },
    {
        id: 36,
        category: 'exploits',
        title: 'Environment Files',
        query: 'site:{target} ext:env | ext:ini | ext:yml | ext:yaml | ext:toml | ext:conf "DB_PASSWORD" | "SECRET" | "PRIVATE_KEY"',
        description: 'Environment and config files with secrets',
        icon: 'file-warning'
    }
];

let currentCategory = 'all';
let searchTerm = '';

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    lucide.createIcons();
    loadTargetFromStorage();
    renderDorks();
});

// Keyboard shortcut: Ctrl+K to focus search
document.addEventListener('keydown', (e) => {
    if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
        e.preventDefault();
        const searchInput = document.getElementById('searchInput');
        if (searchInput) searchInput.focus();
    }
});

// LocalStorage for target domain
function loadTargetFromStorage() {
    const saved = localStorage.getItem('gdorks_target');
    if (saved) {
        const input = document.getElementById('targetInput');
        if (input) input.value = saved;
    }
}

function saveTargetToStorage(domain) {
    try {
        localStorage.setItem('gdorks_target', domain);
    } catch (e) {
        // localStorage might be unavailable
    }
}

function renderDorks() {
    const grid = document.getElementById('dorksGrid');
    const emptyState = document.getElementById('emptyState');
    const countNum = document.getElementById('dorkCountNum');
    
    if (!grid || !emptyState) return;
    
    let filtered = dorks;
    
    if (currentCategory !== 'all') {
        filtered = filtered.filter(d => d.category === currentCategory);
    }
    
    if (searchTerm) {
        const term = searchTerm.toLowerCase();
        filtered = filtered.filter(d => 
            (d.title && d.title.toLowerCase().includes(term)) || 
            (d.description && d.description.toLowerCase().includes(term)) ||
            (d.query && d.query.toLowerCase().includes(term))
        );
    }
    
    // Update counter with bump animation
    if (countNum) {
        const prev = parseInt(countNum.textContent) || 0;
        countNum.textContent = filtered.length;
        if (prev !== filtered.length) {
            countNum.classList.add('bump');
            setTimeout(() => countNum.classList.remove('bump'), 200);
        }
    }
    
    if (filtered.length === 0) {
        grid.innerHTML = '';
        emptyState.classList.remove('hidden');
        return;
    }
    
    emptyState.classList.add('hidden');
    
    const categoryLabels = {
        'sensitive-files': 'Sensitive Files',
        'vulnerabilities': 'Vulnerabilities',
        'directories': 'Directories',
        'cameras': 'Cameras/IoT',
        'login': 'Login Pages',
        'exploits': 'Exploits & Redirects'
    };
    
    grid.innerHTML = filtered.map(dork => {
        const badgeClass = `badge-${dork.category || 'error'}`;
        const label = categoryLabels[dork.category] || 'Unknown';
        const icon = dork.icon || 'circle';
        const title = dork.title || 'Untitled';
        const desc = dork.description || '';
        const query = dork.query || '';
        
        return `
            <div class="dork-card group relative bg-slate-900/50 border border-slate-800/80 rounded-xl p-5 hover:border-slate-700 hover:bg-slate-900/80">
                <div class="flex items-start justify-between mb-3">
                    <div class="flex items-center gap-3">
                        <div class="w-10 h-10 rounded-lg bg-slate-800/80 flex items-center justify-center group-hover:bg-slate-800 transition-colors">
                            <i data-lucide="${icon}" class="w-5 h-5 text-slate-400 group-hover:text-blue-400 transition-colors"></i>
                        </div>
                        <div>
                            <h3 class="font-semibold text-slate-100 text-sm leading-tight">${title}</h3>
                            <span class="inline-block mt-1 px-2 py-0.5 rounded text-xs font-medium border ${badgeClass}">${label}</span>
                        </div>
                    </div>
                </div>
                
                <p class="text-slate-500 text-xs mb-3 leading-relaxed">${desc}</p>
                
                <div class="relative">
                    <div class="bg-slate-950 rounded-lg border border-slate-800/80 p-3 font-mono text-xs text-slate-400 break-all leading-relaxed group-hover:border-slate-700 transition-colors">
                        ${highlightQuery(query)}
                    </div>
                </div>
                
                <div class="flex gap-2 mt-3">
                    <button onclick="copyDork('${dork.id}')" class="flex-1 flex items-center justify-center gap-2 bg-slate-800/50 hover:bg-slate-800 border border-slate-700/50 hover:border-slate-600 rounded-lg py-2 text-xs font-medium text-slate-300 hover:text-white transition-all">
                        <i data-lucide="copy" class="w-3.5 h-3.5"></i>
                        Copy
                    </button>
                    <button onclick="searchDork('${dork.id}')" class="flex-1 flex items-center justify-center gap-2 bg-blue-600/10 hover:bg-blue-600/20 border border-blue-500/20 hover:border-blue-500/30 rounded-lg py-2 text-xs font-medium text-blue-400 hover:text-blue-300 transition-all">
                        <i data-lucide="external-link" class="w-3.5 h-3.5"></i>
                        Search
                    </button>
                </div>
            </div>
        `;
    }).join('');
    
    lucide.createIcons();
}

function highlightQuery(query) {
    if (!query) return '';
    return query.replace(/\{target\}/g, '<span class="text-blue-400 font-semibold">{target}</span>');
}

function getQueryById(id) {
    if (id == null) return '';
    const dork = dorks.find(d => d.id == id);
    return dork && dork.query ? dork.query : '';
}

function getDorkById(id) {
    if (id == null) return null;
    return dorks.find(d => d.id == id) || null;
}

function getTargetDomain() {
    const input = document.getElementById('targetInput');
    const value = (input && input.value && input.value.trim()) ? input.value.trim() : 'example.com';
    saveTargetToStorage(value);
    return value;
}

function copyToClipboard(text) {
    if (!text) return Promise.reject('Empty text');
    if (navigator.clipboard && navigator.clipboard.writeText) {
        return navigator.clipboard.writeText(text);
    }
    // Fallback for non-HTTPS or older browsers
    return new Promise((resolve, reject) => {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';
        textarea.style.opacity = '0';
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            resolve();
        } catch (err) {
            reject(err);
        } finally {
            document.body.removeChild(textarea);
        }
    });
}

function copyDork(id) {
    const dork = getDorkById(id);
    if (!dork || !dork.query) return;
    const target = getTargetDomain();
    const query = dork.query.replace(/\{target\}/g, target);
    
    copyToClipboard(query)
        .then(() => showToast())
        .catch(() => showToast('Failed to copy', true));
}

function searchDork(id) {
    const dork = getDorkById(id);
    if (!dork || !dork.query) return;
    const target = getTargetDomain();
    const query = dork.query.replace(/\{target\}/g, target);
    
    const url = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
    window.open(url, '_blank');
}

function filterByCategory(category, btn) {
    if (!category) return;
    currentCategory = category;
    
    // Update buttons — use btn parameter instead of event.target
    document.querySelectorAll('.category-btn').forEach(b => {
        b.classList.remove('active');
    });
    if (btn) {
        btn.classList.add('active');
    }
    
    renderDorks();
}

function filterDorks() {
    const input = document.getElementById('searchInput');
    searchTerm = input ? input.value : '';
    renderDorks();
}

function buildCustomDork() {
    const target = document.getElementById('targetInput');
    const keywords = document.getElementById('keywordInput');
    
    if (!target || !target.value || !target.value.trim()) {
        showToast('Enter a target domain first', true);
        return;
    }
    
    let query = `site:${target.value.trim()}`;
    if (keywords && keywords.value && keywords.value.trim()) {
        query += ` ${keywords.value.trim()}`;
    }
    
    const resultDiv = document.getElementById('customResult');
    const queryText = document.getElementById('customQueryText');
    
    if (queryText) queryText.textContent = query;
    if (resultDiv) resultDiv.classList.remove('hidden');
}

function copyCustomDork() {
    const queryEl = document.getElementById('customQueryText');
    if (!queryEl) return;
    const query = queryEl.textContent;
    
    copyToClipboard(query)
        .then(() => showToast())
        .catch(() => showToast('Failed to copy', true));
}

function searchCustomDork() {
    const queryEl = document.getElementById('customQueryText');
    if (!queryEl) return;
    const query = queryEl.textContent;
    
    const url = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
    window.open(url, '_blank');
}

function showToast(message, isError) {
    const toast = document.getElementById('toast');
    if (!toast) return;
    
    const textEl = toast.querySelector('span');
    const iconContainer = toast.querySelector('.rounded-lg');
    const iconEl = iconContainer ? iconContainer.querySelector('i') : null;
    
    if (textEl) {
        textEl.textContent = message || 'Copied to clipboard!';
    }
    
    // Update icon style for error vs success
    if (iconContainer) {
        if (isError) {
            iconContainer.className = 'w-8 h-8 bg-red-500/20 rounded-lg flex items-center justify-center';
        } else {
            iconContainer.className = 'w-8 h-8 bg-green-500/20 rounded-lg flex items-center justify-center';
        }
    }
    if (iconEl) {
        iconEl.setAttribute('data-lucide', isError ? 'x' : 'check');
        iconEl.className = isError ? 'w-4 h-4 text-red-400' : 'w-4 h-4 text-green-400';
    }
    
    lucide.createIcons();
    
    toast.classList.add('show');
    
    setTimeout(() => {
        toast.classList.remove('show');
    }, 2000);
}

// Copy all visible dorks to clipboard
function copyAllVisible() {
    const grid = document.getElementById('dorksGrid');
    if (!grid) return;
    
    const target = getTargetDomain();
    let filtered = dorks;
    
    if (currentCategory !== 'all') {
        filtered = filtered.filter(d => d.category === currentCategory);
    }
    if (searchTerm) {
        const term = searchTerm.toLowerCase();
        filtered = filtered.filter(d => 
            (d.title && d.title.toLowerCase().includes(term)) || 
            (d.description && d.description.toLowerCase().includes(term)) ||
            (d.query && d.query.toLowerCase().includes(term))
        );
    }
    
    if (filtered.length === 0) {
        showToast('No dorks to copy', true);
        return;
    }
    
    const text = filtered.map(d => {
        const query = (d.query || '').replace(/\{target\}/g, target);
        return `# ${d.title || 'Untitled'}\n${query}`;
    }).join('\n\n');
    
    copyToClipboard(text)
        .then(() => showToast(`${filtered.length} dorks copied!`))
        .catch(() => showToast('Failed to copy', true));
}

// Export all visible dorks as text file
function exportDorks() {
    const target = getTargetDomain();
    let filtered = dorks;
    
    if (currentCategory !== 'all') {
        filtered = filtered.filter(d => d.category === currentCategory);
    }
    if (searchTerm) {
        const term = searchTerm.toLowerCase();
        filtered = filtered.filter(d => 
            (d.title && d.title.toLowerCase().includes(term)) || 
            (d.description && d.description.toLowerCase().includes(term)) ||
            (d.query && d.query.toLowerCase().includes(term))
        );
    }
    
    if (filtered.length === 0) {
        showToast('No dorks to export', true);
        return;
    }
    
    const categoryLabels = {
        'sensitive-files': 'Sensitive Files',
        'vulnerabilities': 'Vulnerabilities',
        'directories': 'Directories',
        'cameras': 'Cameras/IoT',
        'login': 'Login Pages',
        'exploits': 'Exploits & Redirects'
    };
    
    let content = `GDorks Export\n`;
    content += `Target: ${target}\n`;
    content += `Category: ${currentCategory === 'all' ? 'All' : (categoryLabels[currentCategory] || currentCategory)}\n`;
    content += `Total: ${filtered.length} dorks\n`;
    content += `${'='.repeat(60)}\n\n`;
    
    filtered.forEach((d, i) => {
        const query = (d.query || '').replace(/\{target\}/g, target);
        content += `[${i + 1}] ${d.title || 'Untitled'}\n`;
        content += `    Category: ${categoryLabels[d.category] || 'Unknown'}\n`;
        content += `    Description: ${d.description || 'N/A'}\n`;
        content += `    Query: ${query}\n\n`;
    });
    
    content += `${'='.repeat(60)}\n`;
    content += `Generated by GDorks - https://github.com/Ishanoshada/GDorks\n`;
    
    const blob = new Blob([content], { type: 'text/plain' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `gdorks-${target.replace(/[^a-zA-Z0-9]/g, '_')}-${currentCategory}.txt`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
    
    showToast(`Exported ${filtered.length} dorks!`);
}

// Initialize Lucide icons on load
lucide.createIcons();