Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
139 lines (124 loc) · 3.81 KB
/
Copy pathcontent.js
File metadata and controls
139 lines (124 loc) · 3.81 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
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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "paste-drops") {
executePaste();
}
});
async function executePaste() {
let text;
try {
text = await navigator.clipboard.readText();
} catch (e) {
showNotification("Clipboard access denied", "error");
return;
}
if (!text || !text.trim()) {
showNotification("Clipboard is empty", "error");
return;
}
let data;
try {
data = JSON.parse(text);
} catch (e) {
showNotification("Clipboard is not valid JSON", "error");
return;
}
if (!data.fields || !Array.isArray(data.fields)) {
showNotification('JSON must have a "fields" array', "error");
return;
}
let filled = 0;
let errors = [];
for (const field of data.fields) {
var hasValue = field.value !== undefined;
var hasChecked = field.checked === true;
if (!field.target || (!hasValue && !hasChecked)) {
errors.push('Invalid field: missing "target" or "value"/"checked"');
continue;
}
let el;
try {
el = document.querySelector(field.target);
} catch (e) {
errors.push("Invalid selector: " + field.target);
continue;
}
if (!el) {
errors.push("Not found: " + field.target);
continue;
}
if (hasChecked) {
el.checked = true;
el.dispatchEvent(new Event("click", { bubbles: true }));
el.dispatchEvent(new Event("change", { bubbles: true }));
} else {
setNativeValue(el, field.value);
el.dispatchEvent(new Event("input", { bubbles: true }));
el.dispatchEvent(new Event("change", { bubbles: true }));
el.dispatchEvent(new Event("blur", { bubbles: true }));
}
filled++;
}
if (errors.length > 0) {
showNotification(
"Filled " + filled + "/" + data.fields.length + " fields\n" + errors.join("\n"),
filled > 0 ? "warn" : "error"
);
} else {
showNotification("Filled " + filled + " fields", "success");
}
}
function setNativeValue(el, value) {
// React overrides the value setter on input/textarea elements.
// Using the native prototype setter ensures React's internal state
// gets updated properly.
var prototype;
if (el instanceof HTMLTextAreaElement) {
prototype = HTMLTextAreaElement.prototype;
} else if (el instanceof HTMLSelectElement) {
prototype = HTMLSelectElement.prototype;
} else {
prototype = HTMLInputElement.prototype;
}
var nativeSetter = Object.getOwnPropertyDescriptor(prototype, "value");
if (nativeSetter && nativeSetter.set) {
nativeSetter.set.call(el, value);
} else {
el.value = value;
}
}
function showNotification(message, type) {
var existing = document.getElementById("pastedrops-notification");
if (existing) existing.remove();
var colors = {
success: { bg: "#10b981", border: "#059669" },
warn: { bg: "#f59e0b", border: "#d97706" },
error: { bg: "#ef4444", border: "#dc2626" },
};
var color = colors[type] || colors.success;
var div = document.createElement("div");
div.id = "pastedrops-notification";
div.textContent = message;
Object.assign(div.style, {
position: "fixed",
top: "16px",
right: "16px",
zIndex: "2147483647",
padding: "12px 20px",
borderRadius: "8px",
backgroundColor: color.bg,
border: "2px solid " + color.border,
color: "#fff",
fontSize: "14px",
fontFamily: "system-ui, sans-serif",
fontWeight: "600",
boxShadow: "0 4px 12px rgba(0,0,0,0.3)",
whiteSpace: "pre-line",
maxWidth: "400px",
transition: "opacity 0.3s ease",
});
document.body.appendChild(div);
setTimeout(function () {
div.style.opacity = "0";
setTimeout(function () { div.remove(); }, 300);
}, 3000);
}