51 lines
4.6 KiB
JavaScript
51 lines
4.6 KiB
JavaScript
'use strict';
|
|
const LOGIN_FORMS = {
|
|
'https://sso.skyworthdigital.com': { username:'#username', password:'#password', submit:'#kc-form-login button[type="submit"]', form:'#kc-form-login', actionPath:'/realms/skyworthdigital/login-actions/authenticate' }
|
|
};
|
|
async function fillSavedForm(lease,stored,loginForms) {
|
|
const origin=new URL(lease.page.url()).origin, form=loginForms[origin];
|
|
if (!form || lease.blocked) return {filled:false,message:'请打开支持的 BPM 登录页面。'};
|
|
try {
|
|
await Promise.all([lease.page.locator(form.username).waitFor({state:'visible',timeout:1500}),lease.page.locator(form.password).waitFor({state:'visible',timeout:1500})]);
|
|
// Validate and set both values in one browser task: a navigation or DOM replacement
|
|
// between separate locator.fill calls must never redirect a secret to another field.
|
|
const filled=await lease.page.evaluate(({origin,form,username,password})=>{
|
|
if (location.origin!==origin || window!==window.top) return false;
|
|
const user=document.querySelector(form.username), secret=document.querySelector(form.password);
|
|
const usable=(node,type)=>node instanceof HTMLInputElement && node.type===type && !node.disabled && !node.readOnly && node.getClientRects().length>0 && getComputedStyle(node).visibility==='visible' && getComputedStyle(node).display!=='none';
|
|
if (!usable(user,'text') || !usable(secret,'password') || user===secret) return false;
|
|
const set=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set;
|
|
set.call(user,username);set.call(secret,password);
|
|
for (const node of [user,secret]) { node.dispatchEvent(new Event('input',{bubbles:true})); node.dispatchEvent(new Event('change',{bubbles:true})); }
|
|
return true;
|
|
},{origin,form,username:stored.username,password:stored.password});
|
|
return {filled,message:filled?'已填入托管账号,请完成登录。':'请打开支持的 BPM 登录表单。'};
|
|
} catch { return {filled:false,message:'请等待登录表单显示后重试。'}; }
|
|
}
|
|
async function automaticSubmitForm(lease,stored,submit,loginForms) {
|
|
const origin=new URL(lease.page.url()).origin, form=loginForms[origin];
|
|
if (!form?.submit || lease.blocked) return false;
|
|
return lease.page.evaluate(({origin,form,stored,submit})=>{
|
|
if (location.origin!==origin || window!==window.top) return false;
|
|
const visible=node=>!!node && node.getClientRects().length>0 && getComputedStyle(node).visibility==='visible' && getComputedStyle(node).display!=='none';
|
|
const user=document.querySelector(form.username),secret=document.querySelector(form.password),button=document.querySelector(form.submit);
|
|
if (!(user instanceof HTMLInputElement) || user.type!=='text' || !visible(user) || user.disabled || user.readOnly || !(secret instanceof HTMLInputElement) || secret.type!=='password' || !visible(secret) || secret.disabled || secret.readOnly || !visible(button) || button.disabled || button.type!=='submit') return false;
|
|
const login=user.form;
|
|
if (!login || secret.form!==login || button.form!==login || login.method.toLowerCase()!=='post' || (form.form && !login.matches(form.form))) return false;
|
|
if ((button.getAttribute('formmethod') || login.method).toLowerCase()!=='post' || !['','_self'].includes(button.getAttribute('formtarget') || login.target)) return false;
|
|
const action=new URL(button.getAttribute('formaction') || login.action,location.href);
|
|
if (action.origin!==origin || (form.actionPath && action.pathname!==form.actionPath)) return false;
|
|
// Any additional visible field or challenge requires the user's intervention.
|
|
const challenge=/captcha|recaptcha|hcaptcha|turnstile|otp|one.?time|totp|verification|verify.?code/i;
|
|
if (Array.from(document.querySelectorAll('input,iframe,img,[id],[class]')).some(node=>visible(node) && challenge.test([node.id,node.className,node.getAttribute('name'),node.getAttribute('autocomplete'),node.getAttribute('src'),node.getAttribute('title')].join(' ')))) return false;
|
|
if (Array.from(login.elements).some(node=>visible(node) && node!==user && node!==secret && !['submit','button','hidden','reset'].includes(node.type))) return false;
|
|
if (submit) {
|
|
const set=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set;
|
|
set.call(user,stored.username);set.call(secret,stored.password);
|
|
button.click();
|
|
}
|
|
return true;
|
|
},{origin,form,stored:{username:stored.username,password:stored.password},submit}).catch(()=>false);
|
|
}
|
|
module.exports={LOGIN_FORMS,fillSavedForm,automaticSubmitForm};
|