mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
chrome extensions (#4308)
Build chrome extensions that allow interaction with browser content
This commit is contained in:
68
intergrations/extension_chrome/content.js
Normal file
68
intergrations/extension_chrome/content.js
Normal file
@ -0,0 +1,68 @@
|
||||
(function () {
|
||||
const extractElementData = (el) => {
|
||||
const tag = el.tagName.toLowerCase();
|
||||
if (
|
||||
tag === "input" &&
|
||||
el.name !== "DXScript" &&
|
||||
el.name !== "DXMVCEditorsValues" &&
|
||||
el.name !== "DXCss"
|
||||
) {
|
||||
return {
|
||||
type: "input",
|
||||
name: el.name,
|
||||
value:
|
||||
el.type === "checkbox" || el.type === "radio"
|
||||
? el.checked
|
||||
? el.value
|
||||
: null
|
||||
: el.value,
|
||||
};
|
||||
} else if (tag === "select") {
|
||||
const selectedOption = el.querySelector("option:checked");
|
||||
return {
|
||||
type: "select",
|
||||
name: el.name,
|
||||
value: selectedOption ? selectedOption.value : null,
|
||||
};
|
||||
} else if (tag.startsWith("h") && el.textContent.trim()) {
|
||||
return { type: "header", tag, content: el.textContent.trim() };
|
||||
} else if (
|
||||
["label", "span", "p", "b", "strong"].includes(tag) &&
|
||||
el.textContent.trim()
|
||||
) {
|
||||
return { type: tag, content: el.textContent.trim() };
|
||||
}
|
||||
};
|
||||
|
||||
const getElementValues = (els) =>
|
||||
Array.from(els).map(extractElementData).filter(Boolean);
|
||||
|
||||
const getIframeInputValues = (iframe) => {
|
||||
try {
|
||||
const iframeDoc = iframe.contentWindow.document;
|
||||
return getElementValues(
|
||||
iframeDoc.querySelectorAll("input, select, header, label, span, p")
|
||||
);
|
||||
} catch (e) {
|
||||
console.error("Can't access iframe:", e);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const inputValues = getElementValues(
|
||||
document.querySelectorAll("input, select, header, label, span, p")
|
||||
);
|
||||
const iframeInputValues = Array.from(document.querySelectorAll("iframe")).map(
|
||||
getIframeInputValues
|
||||
);
|
||||
|
||||
return `
|
||||
## input values\n
|
||||
\`\`\`json\n
|
||||
${JSON.stringify(inputValues)}\n
|
||||
\`\`\`\n
|
||||
## iframe input values\n
|
||||
\`\`\`json\n
|
||||
${JSON.stringify(iframeInputValues)}\n
|
||||
\`\`\``;
|
||||
})();
|
||||
Reference in New Issue
Block a user