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

47 lines
3.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 = {};
for (const [key, binding] of Object.entries(bindings)) {
const el = source.querySelector(`[xdatabind="${binding}"]`);
metadata[key] = (el?.value ?? el?.textContent ?? '').trim();
}
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, 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 };