Files
order-notification-automation/extension/capture.js
T

60 lines
4.2 KiB
JavaScript

// Self-contained function: injected only after the user clicks Export.
function captureOrder() {
const source = document.querySelector('form');
if (!source) throw new Error('当前页面没有订单表单,请进入 BPM 订单详情。');
const bindings = { order:'OFR_WY_Applicant.OrderNum', project:'OFR_WY_BInfo.ProductNum', model:'OFR_WY_BInfo.ProductModelNEW', serial:'OFR_WY_Applicant.SN' };
const labels = { order:'订单号', project:'项目编号', model:'产品型号', serial:'通知书编号' };
const metadata = {}, warnings = [];
const orderBindings = ['OFR_LSCP_Applicant.OrderNum', 'OFR_BS_Applicant.OrderNum', 'OFR_KD_Applicant.OrderNum', 'OFR_WY_Applicant.OrderNum'];
function visibleValue(el) {
for (let node = el; node; node = node.parentElement) {
const style = getComputedStyle(node), type = (node.getAttribute('type') || '').toLowerCase();
if (node.hidden || ['hidden','password'].includes(type) || style.display === 'none' || ['hidden','collapse'].includes(style.visibility)) return '';
}
return (['INPUT','TEXTAREA','SELECT'].includes(el.tagName) ? el.value : el.innerText).trim();
}
let bpmOrder = null;
for (const [key, binding] of Object.entries(bindings)) {
const candidates = (key === 'order' ? orderBindings : [binding]).flatMap(field =>
[...source.querySelectorAll(`[xdatabind="${field}"]`)].map(el => ({field, value:visibleValue(el)})).filter(item => item.value));
const values = [...new Set(candidates.map(item => item.value))];
metadata[key] = values.length === 1 ? values[0] : '';
if (values.length > 1) warnings.push(`BPM 表单中有多个不同的${labels[key]},请核实后重新导出。`);
if (key === 'order' && metadata.order) bpmOrder = candidates[0];
}
const clone = source.cloneNode(true);
const originals = [source, ...source.querySelectorAll('*')];
const copies = [clone, ...clone.querySelectorAll('*')];
for (let index = originals.length - 1; index >= 0; index--) {
const original = originals[index], copy = copies[index];
const style = getComputedStyle(original);
const type = (original.getAttribute('type') || '').toLowerCase();
const tag = original.tagName.toLowerCase();
if (['script','iframe','object','embed','button','link','meta'].includes(tag) || ['hidden','password','button','submit','reset','file'].includes(type) || style.display === 'none' || style.visibility === 'hidden' || original.hidden || original.getAttribute('printout') === 'false') {
copy.remove(); continue;
}
if (['input','textarea','select'].includes(tag)) {
const span = document.createElement('span');
span.className = original.className;
span.style.cssText = 'display:inline-block;max-width:100%;white-space:pre-wrap;overflow-wrap:break-word;min-height:1em;';
span.textContent = ['checkbox','radio'].includes(type) ? (original.checked ? '☑' : '☐') : tag === 'select' ? [...original.selectedOptions].map(option => option.textContent).join('、') : original.value;
copy.replaceWith(span);
} else {
for (const attribute of [...copy.attributes]) {
if (/^on/i.test(attribute.name) || ['action','srcdoc','srcset','formaction','value'].includes(attribute.name.toLowerCase())) copy.removeAttribute(attribute.name);
}
if (tag === 'img') copy.setAttribute('src', original.src);
}
}
for (const id of ['taskCommentForm','TaskCommentCount']) {
const el = clone.querySelector('#'+id);
if (el) el.remove();
}
const styles = [...document.querySelectorAll('style')].map(el => el.textContent);
const stylesheetUrls = [...document.querySelectorAll('link[rel="stylesheet"][href]')].map(el => el.href);
const styleSources = [...document.querySelectorAll('style,link[rel="stylesheet"][href]')].map(el => el.tagName.toLowerCase() === 'style' ? {text:el.textContent} : {url:el.href});
const imageUrls = [...clone.querySelectorAll('img[src]')].map(el => el.getAttribute('src'));
return { metadata, bpmOrder, html:clone.outerHTML, styles, styleSources, stylesheetUrls, imageUrls:[...new Set(imageUrls)], width:1264, missing:Object.keys(bindings).filter(key => !metadata[key]).map(key => labels[key]), warnings };
}
if (typeof module !== 'undefined') module.exports = { captureOrder };