|
1 | 1 | <script lang="ts"> |
2 | | - import AppWrapper from '../AppWrapper/AppWrapper.svelte'; |
| 2 | + import AppWrapper from './AppWrapper.svelte'; |
3 | 3 | import axios from 'axios'; |
| 4 | + import ipaddr from 'ipaddr.js'; |
4 | 5 |
|
5 | | - const publicIp = require('public-ip'); |
6 | | - let ipv4 = '...'; |
7 | | - let ipv6 = '...'; |
8 | | - let textBtnV4 = 'copy'; |
9 | | - let textBtnV6 = 'copy'; |
10 | | - // let textBtnServer = 'copy'; |
11 | | - let classBtnV4 = 'btn-primary'; |
12 | | - let classBtnV6 = 'btn-primary'; |
13 | | - // let classBtnServer = 'btn-primary'; |
| 6 | + let ipv4 = ''; |
| 7 | + let ipv6 = ''; |
| 8 | + let ipServer = []; |
| 9 | + let canonicalName = ''; |
14 | 10 |
|
15 | | - (async () => { |
16 | | - ipv4 = await publicIp.v4(); |
17 | | - ipv6 = await publicIp.v6(); |
18 | | - })(); |
| 11 | + // récupérer l'IPv4 client |
| 12 | + axios.get('https://api.ipify.org').then(function (response) { |
| 13 | + ipv4 = response.data; |
| 14 | + }); |
19 | 15 |
|
20 | | - function copyIpOnClipboard(typeIP) { |
21 | | - const type = typeIP.target.dataset.type; |
| 16 | + // récupérer l'IPv6 client |
| 17 | + axios.get('https://api64.ipify.org').then(function (response) { |
| 18 | + ipv6 = response.data; |
| 19 | + }); |
22 | 20 |
|
23 | | - textBtnV4 = 'copy'; |
24 | | - textBtnV6 = 'copy'; |
25 | | - classBtnV4 = 'btn-primary'; |
26 | | - classBtnV6 = 'btn-primary'; |
| 21 | + // récupérer les IPs server |
| 22 | + browser.tabs |
| 23 | + .query({ |
| 24 | + currentWindow: true, |
| 25 | + active: true, |
| 26 | + }) |
| 27 | + .then(function (tabInfo) { |
| 28 | + let domain = new URL(tabInfo[0].url).hostname.replace('www.', ''); |
| 29 | + if (domain) { |
| 30 | + let resolving = browser.dns.resolve(domain, ['bypass_cache', 'canonical_name']); |
| 31 | + resolving.then((resolved) => { |
| 32 | + console.log(resolved); |
| 33 | + ipServer = resolved.addresses; |
| 34 | + canonicalName = resolved.canonicalName; |
| 35 | + }); |
| 36 | + } |
| 37 | + }); |
27 | 38 |
|
28 | | - switch (type) { |
29 | | - case 'IPv4': |
30 | | - navigator.clipboard.writeText(ipv4).then( |
31 | | - function () { |
32 | | - textBtnV4 = 'copied'; |
33 | | - classBtnV4 = 'btn-success'; |
34 | | - setTimeout(function () { |
35 | | - textBtnV4 = 'copy'; |
36 | | - classBtnV4 = 'btn-primary'; |
37 | | - }, 3000); |
38 | | - }, |
39 | | - function () { |
40 | | - console.log('failed'); |
41 | | - } |
42 | | - ); |
43 | | - break; |
44 | | - case 'IPv6': |
45 | | - navigator.clipboard.writeText(ipv6).then( |
46 | | - function () { |
47 | | - textBtnV6 = 'copied'; |
48 | | - classBtnV6 = 'btn-success'; |
49 | | - setTimeout(function () { |
50 | | - textBtnV6 = 'copy'; |
51 | | - classBtnV6 = 'btn-primary'; |
52 | | - }, 3000); |
53 | | - }, |
54 | | - function () { |
55 | | - console.log('failed'); |
56 | | - } |
57 | | - ); |
58 | | - break; |
59 | | - default: |
60 | | - console.log(`Sorry, we are out of ${type}.`); |
61 | | - } |
| 39 | + function copyIpOnClipboard(data) { |
| 40 | + navigator.clipboard.writeText(data.target.dataset.ip).then( |
| 41 | + function () { |
| 42 | + data.target.innerText = 'copied'; |
| 43 | + data.target.className = 'btn-success'; |
| 44 | + setTimeout(function () { |
| 45 | + data.target.innerText = 'copy'; |
| 46 | + data.target.className = 'btn-primary'; |
| 47 | + }, 3000); |
| 48 | + }, |
| 49 | + function () { |
| 50 | + console.log('failed'); |
| 51 | + } |
| 52 | + ); |
62 | 53 | } |
63 | 54 | </script> |
64 | 55 |
|
|
67 | 58 | <h1 class="d-none">Get IPs</h1> |
68 | 59 | <div class="kaomoji">°˖✧◝(⁰▿⁰)◜✧˖°</div> |
69 | 60 | <h2>Your public IPs :</h2> |
70 | | - <strong>IPv4 :</strong> |
71 | | - {ipv4}<button class={classBtnV4} on:click={copyIpOnClipboard} data-type="IPv4">{textBtnV4}</button> |
72 | | - <br /> |
73 | | - <strong>IPv6 :</strong> |
74 | | - {ipv6}<button class={classBtnV6} on:click={copyIpOnClipboard} data-type="IPv6">{textBtnV6}</button> |
75 | | - <h2>The current URL IP :</h2> |
76 | | - Soon... |
77 | | - <div class="version">getIPs <strong>1.0.0</strong></div> |
| 61 | + {#if ipv4 !== ''} |
| 62 | + <strong>IPv4 :</strong> |
| 63 | + {ipv4}<button class="btn-primary" on:click={copyIpOnClipboard} data-ip={ipv4}>copy</button> |
| 64 | + {:else} |
| 65 | + <strong>IPv4</strong> unavailable |
| 66 | + {/if} |
| 67 | + {#if ipv6 !== ''} |
| 68 | + <br /> |
| 69 | + <strong>IPv6 :</strong> |
| 70 | + {ipv6}<button class="btn-primary" on:click={copyIpOnClipboard} data-ip={ipv6}>copy</button> |
| 71 | + {:else} |
| 72 | + <br /> |
| 73 | + <strong>IPv6</strong> unavailable |
| 74 | + {/if} |
| 75 | + <h2>The current URL IPs :</h2> |
| 76 | + {#if ipServer.length} |
| 77 | + <strong>Canonical name :</strong> |
| 78 | + {canonicalName} |
| 79 | + <br /> |
| 80 | + {#each ipServer as ip} |
| 81 | + {#if ipaddr.IPv4.isValid(ip)} |
| 82 | + <strong>IPv4 :</strong> |
| 83 | + {:else if ipaddr.IPv6.isValid(ip)} |
| 84 | + <strong>IPv6 :</strong> |
| 85 | + {:else} |
| 86 | + <strong>IP no valid :</strong> |
| 87 | + {/if} |
| 88 | + {ip}<button class="btn-primary" on:click={copyIpOnClipboard} data-ip={ip}>copy</button><br /> |
| 89 | + {/each} |
| 90 | + {:else} |
| 91 | + <strong>IP server</strong> unavailable |
| 92 | + {/if} |
| 93 | + <div class="version">getIPs <strong>1.2.0</strong></div> |
78 | 94 | </div> |
79 | 95 | </AppWrapper> |
80 | 96 |
|
|
0 commit comments