-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontextScript.js
More file actions
130 lines (119 loc) · 4.61 KB
/
contextScript.js
File metadata and controls
130 lines (119 loc) · 4.61 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
console.log("Guardium is loaded");
function setNativeValue(element, value) {
const prototype = Object.getPrototypeOf(element);
let descriptor = Object.getOwnPropertyDescriptor(prototype, "value");
if(!descriptor){
descriptor = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value");
}
if (descriptor && descriptor.set) {
descriptor.set.call(element, value);
}else{
element.value = value;
}
element.dispatchEvent(new Event("input", { bubbles: true }));
element.dispatchEvent(new Event("change", { bubbles: true }));
}
function isExtentionContextValid() {
return typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.id;
}
function fillCredentials(passwordInput, credentials) {
if (!credentials) return;
console.log("Guardium: Filling password");
setNativeValue(passwordInput, credentials.password);
// Heuristic: Look for the username field strictly before the password field
const inputs = Array.from(document.querySelectorAll('input'));
const passIndex = inputs.indexOf(passwordInput);
if (passIndex > 0) {
// Search backwards for the nearest visible text/email input
for (let i = passIndex - 1; i >= 0; i--) {
const prevInput = inputs[i];
if (prevInput.type === "text" || prevInput.type === "email") {
// Check visibility (simple check)
if (prevInput.offsetParent !== null) {
console.log("Guardium: Filling username");
setNativeValue(prevInput, credentials.username);
break; // Stop after filling one username
}
}
}
}
}
function attachlisteners(passwordInput) {
if(passwordInput.dataset.guardiumAttached) return;
passwordInput.dataset.guardiumAttached = "true";
passwordInput.addEventListener("focus", async () => {
console.log("Guardium: Password field focused");
try {
chrome.runtime.sendMessage({type:"REQUEST_CREDEDENTIALS",site: window.location.hostname},
(response)=>{
if(chrome.runtime.lastError){
return;
}
if(response && response.credentials){
fillCredentials(passwordInput, response.credentials);
}
});
} catch (error) {
}
});
}
function detectPasswordInputs() {
document.querySelectorAll('input[type="password"]').forEach((attachlisteners));
}
const observer = new MutationObserver(detectPasswordInputs);
observer.observe(document.documentElement, { childList: true, subtree: true });
detectPasswordInputs();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if(message.type === "FILL_CREDENTIALS" && message.credentials){
if(message.credentials && message.credentials.password){
const passwordInput=document.querySelector('input[type="password"]');
if(passwordInput){
fillCredentials(passwordInput, message.credentials);
sendResponse({filled: "true"});
}
}else{
console.warn("No passwrod field is found");
sendResponse({filled: "false"});
}
}
});
function findUsernameField(passwordInput) {
const inputs = Array.from(document.querySelectorAll('input'));
const passIndex=inputs.indexOf(passwordInput);
for (let i = passIndex - 1; i >= 0; i--) {
const prevInput = inputs[i];
if ((prevInput.type === "text" || prevInput.type === "email")&&prevInput.offsetParent!== null) {
return prevInput;
}
}
return null;
}
let typingTimer;
function syncToBackground(passwordInput) {
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
if(passwordInput.value.length===0)return;
const usernameInput=findUsernameField(passwordInput);
const credentials={
username:usernameInput?usernameInput.value:"",
password:passwordInput.value,
site:window.location.hostname
};
console.log("Syncing",credentials.username);
try {
chrome.runtime.sendMessage({type:"Queueing_credentials",data:credentials});
} catch (error) {
console.error("Error syncing credentials",error);
}
},500);
}
document.addEventListener("input", (event) => {
if(event.target.type==="password"){
syncToBackground(event.target);
}
},true);
document.addEventListener("change", (event) => {
if(event.target.type==="password"){
syncToBackground(event.target);
}
},true);