-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (84 loc) · 2.72 KB
/
Copy pathscript.js
File metadata and controls
85 lines (84 loc) · 2.72 KB
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
const { createApp } = Vue;
createApp({
data() {
return {
inputText: '',
outputText: '',
theme: localStorage.getItem('theme') || 'dark',
_updating: false
};
},
computed: {
inputChars() { return this.inputText.length; },
inputBytes() { return new Blob([this.inputText]).size; },
outputChars() { return this.outputText.length; },
outputBytes() { return new Blob([this.outputText]).size; },
isValidUrl() {
if (!this.inputText) return null;
try { new URL(this.inputText); return true; } catch { return false; }
}
},
watch: {
inputText(val) { this._sync('output', val); },
outputText(val) { this._sync('input', val); }
},
methods: {
_sync(target, val) {
if (this._updating) return;
this._updating = true;
const key = target + 'Text';
if (!val) { this[key] = ''; }
else if (/%(?:[0-9A-Fa-f]{2})+/.test(val)) {
try { this[key] = decodeURIComponent(val); } catch { this[key] = encodeURIComponent(val); }
} else {
this[key] = encodeURIComponent(val);
}
this._updating = false;
},
_flash(id) {
const el = document.getElementById(id);
if (!el) return;
el.classList.add('blink');
setTimeout(() => el.classList.remove('blink'), 1800);
},
encodeText() {
this._updating = true;
this.outputText = encodeURIComponent(this.inputText);
this._updating = false;
},
decodeText() {
this._updating = true;
try { this.outputText = decodeURIComponent(this.inputText); }
catch { this.outputText = 'Invalid encoded URL'; }
this._updating = false;
},
copyInput() {
if (!this.inputText) return;
navigator.clipboard.writeText(this.inputText)
.then(() => this._flash('input'))
.catch(err => console.error('Copy failed', err));
},
copyOutput() {
if (!this.outputText) return;
navigator.clipboard.writeText(this.outputText)
.then(() => this._flash('output'))
.catch(err => console.error('Copy failed', err));
},
clearInput() { this.inputText = ''; },
clearOutput() { this.outputText = ''; },
toggleTheme() {
this.theme = this.theme === 'dark' ? 'light' : 'dark';
localStorage.setItem('theme', this.theme);
document.body.className = this.theme;
}
},
mounted() {
document.body.className = this.theme;
window.addEventListener('keydown', (e) => {
const mod = e.metaKey || e.ctrlKey;
if (mod && e.key === 'e') { e.preventDefault(); this.encodeText(); }
if (mod && e.key === 'd') { e.preventDefault(); this.decodeText(); }
if (mod && e.shiftKey && e.key === 'C') { e.preventDefault(); this.copyOutput(); }
});
}
}).mount('#app');