Fahim dev #2

Merged
smfahimhossen merged 2 commits from fahim-dev into main 2024-12-10 10:45:05 +00:00
2 changed files with 174 additions and 125 deletions
Showing only changes of commit 9786a9872f - Show all commits

1
.idea/misc.xml generated
View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager"> <component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />

View file

@ -1,154 +1,202 @@
import { Image, StyleSheet, Platform } from "react-native";
import { WebView } from "react-native-webview"; import { WebView } from "react-native-webview";
import { useRef } from "react"; import { useRef } from "react";
export default function HomeScreen() { export default function HomeScreen() {
const webviewRef = useRef(null); const webviewRef = useRef(null);
const injectedJavaScript = ` const injectedJavaScript = ` let lastClickTime = 0;
function collectPostData(post) { const COOLDOWN_DURATION = 60000;
if (post.dataset.processed === "true") return; let buttonClicked = false;
const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text"); let fullCaption = "";
let caption = captionElement ? captionElement.textContent.trim() : null; let imageUrls = [];
const imageElements = post.querySelectorAll("div.m.bg-s13 img"); function collectPostData(post) {
let imageURLs = []; if (post.dataset.processed === "true") return;
if (imageElements.length > 1) {
imageElements.forEach((imageElement) => {
if (imageElement.src) imageURLs.push(imageElement.src);
});
} else if (imageElements.length === 1) {
const singleImageSrc = imageElements[0].src;
if (singleImageSrc) imageURLs.push(singleImageSrc);
}
console.log("Caption:", caption);
console.log("Image URLs:", imageURLs);
post.dataset.processed = "true"; const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text");
let caption = captionElement ? captionElement.textContent.trim() : null;
const button = document.createElement("button"); const imageElements = post.querySelectorAll("div.m.bg-s13 img");
button.textContent = "Check"; let imageURLs = [];
button.innerHTML = button.textContent; imageElements.forEach((imageElement) => {
if (imageElement.src) imageURLs.push(imageElement.src);
});
button.style.position = "absolute"; if (buttonClicked) {
button.style.fontSize = "20px"; fullCaption = caption;
button.style.right = "10px"; imageUrls.push(...imageURLs);
button.style.bottom = "10px"; }
button.style.background = "linear-gradient(135deg, #6a11cb, #2575fc)"; post.dataset.processed = "true";
button.style.color = "#fff";
button.style.border = "none";
button.style.padding = "12px 20px";
button.style.borderRadius = "8px";
button.style.cursor = "pointer";
button.style.zIndex = 999999;
button.style.pointerEvents = "auto";
button.style.display = "flex";
button.style.justifyContent = "center";
button.style.alignItems = "center";
button.style.gap = "8px";
button.addEventListener("click", async () => { const button = createButton(post, caption, imageURLs);
button.disabled = true;
button.textContent = "Loading...";
const seeMoreElement = post.querySelector("span[style*='color:#65676b']");
if (seeMoreElement) {
handleSeeMoreClick(seeMoreElement, post, button);
} else {
collectDataImmediately(post, button);
}
});
if (caption && imageURLs.length > 0) { if (caption && imageURLs.length > 0) {
post.style.position = "relative"; post.style.position = "relative";
post.appendChild(button); post.appendChild(button);
} }
} }
function collectDataImmediately(post, button) { function createButton(post, initialCaption, initialImageURLs) {
const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text"); const button = document.createElement("button");
let caption = captionElement ? captionElement.textContent.trim() : null; button.textContent = "Check";
const imageElements = post.querySelectorAll("div.m img");
let imageURLs = [];
imageElements.forEach((imageElement) => {
if (imageElement.src) imageURLs.push(imageElement.src);
});
if(caption && imageURLs){
alert("good");
}
console.log("Caption:", caption);
console.log("Image URLs:", imageURLs);
button.remove(); Object.assign(button.style, {
} position: "absolute",
fontSize: "20px",
right: "10px",
bottom: "10px",
background: "linear-gradient(135deg, #6a11cb, #2575fc)",
color: "#fff",
border: "none",
padding: "12px 20px",
borderRadius: "8px",
cursor: "pointer",
zIndex: 1000000,
display: "flex",
justifyContent: "center",
alignItems: "center",
gap: "8px",
pointerEvents: "auto",
transition: "all 0.3s",
});
function handleSeeMoreClick(seeMoreElement, post, button) { button.addEventListener("click", (event) => {
setTimeout(() => { event.stopPropagation();
seeMoreElement.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); const currentTime = new Date().getTime();
seeMoreElement.dispatchEvent(new PointerEvent("pointerup", { bubbles: true }));
seeMoreElement.click();
setTimeout(() => { if (currentTime - lastClickTime < COOLDOWN_DURATION) {
collectDataImmediately(post, button); const remainingTime = Math.ceil((COOLDOWN_DURATION - (currentTime - lastClickTime)) / 1000);
}, 1000);
}, 100);
}
function handleMutations(mutationsList) { // Fix template literal issue by using escape characters
mutationsList.forEach((mutation) => { button.textContent = "Wait " + remainingTime + "s"; // Change here
if (mutation.type === "childList" || mutation.type === "subtree") { button.style.background = "linear-gradient(135deg, #a0a0a0, #c0c0c0)";
const posts = document.querySelectorAll("div.m.bg-s3"); button.style.cursor = "not-allowed";
posts.forEach((post) => { button.disabled = true;
if (!post.querySelector("button")) {
collectPostData(post);
}
});
}
});
}
const observer = new MutationObserver(handleMutations); const cooldownTimer = setInterval(() => {
observer.observe(document.querySelector(".m"), { childList: true, subtree: true }); const updatedRemainingTime = Math.ceil((COOLDOWN_DURATION - (new Date().getTime() - lastClickTime)) / 1000);
document.addEventListener("DOMContentLoaded", () => { if (updatedRemainingTime <= 0) {
const posts = document.querySelectorAll("div.m.bg-s3"); clearInterval(cooldownTimer);
posts.forEach((post) => { button.textContent = "Check";
if (!post.dataset.processed) { button.style.background = "linear-gradient(135deg, #6a11cb, #2575fc)";
collectPostData(post); button.style.cursor = "pointer";
} button.disabled = false;
}); return;
}); }
button.textContent = "Wait " + updatedRemainingTime + "s"; // Change here
}, 1000);
return;
}
lastClickTime = currentTime;
button.disabled = true;
button.textContent = "Processing...";
button.style.opacity = "0.5";
const seeMoreElement = post.querySelector("span[style*='color:#65676b']");
if (seeMoreElement) {
handleSeeMoreClick(seeMoreElement, post, button, initialCaption, initialImageURLs);
} else {
collectDataImmediately(post, button, initialCaption, initialImageURLs);
}
});
return button;
}
function handleSeeMoreClick(seeMoreElement, post, button, initialCaption, initialImageURLs) {
const currentTime = new Date().getTime();
setTimeout(() => {
seeMoreElement.click();
if (currentTime - lastClickTime < COOLDOWN_DURATION) {
buttonClicked = true;
}
setTimeout(() => {
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
})
);
}
alert(
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
}));
button.textContent = "Processed";
button.style.opacity = "0.7";
button.disabled = true;
}, 2000);
}, 100);
}
function collectDataImmediately(post, button, initialCaption, initialImageURLs) {
const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text");
let caption = captionElement ? captionElement.textContent.trim() : initialCaption;
const imageElements = post.querySelectorAll("div.m img");
let imageURLs = [];
imageElements.forEach((imageElement) => {
if (imageElement.src) imageURLs.push(imageElement.src);
});
if (imageURLs.length === 0) {
imageURLs = initialImageURLs;
}
alert(
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
}));
button.textContent = "Processed";
button.style.opacity = "0.7";
button.disabled = true;
}
// Mutation observer to handle new posts
function handleMutations(mutationsList) {
mutationsList.forEach((mutation) => {
if (mutation.type === "childList" || mutation.type === "subtree") {
document.querySelectorAll("div.m.bg-s3").forEach((post) => {
if (!post.querySelector("button")) {
collectPostData(post);
}
});
}
});
}
const observer = new MutationObserver(handleMutations);
observer.observe(document.querySelector(".m"), {
childList: true,
subtree: true,
});
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll("div.m.bg-s3").forEach((post) => {
if (!post.dataset.processed) {
collectPostData(post);
}
});
});
`; `;
return ( return (
<WebView <WebView
ref={webviewRef} ref={webviewRef}
source={{ uri: "https://www.facebook.com" }} source={{ uri: "https://m.facebook.com/?__n=K" }}
injectedJavaScript={injectedJavaScript} injectedJavaScript={injectedJavaScript}
startInLoadingState={true} startInLoadingState={true}
onLoadEnd={() => {
if (webviewRef.current) {
webviewRef.current.injectJavaScript(injectedJavaScript);
}
}}
/> />
); );
} }
const styles = StyleSheet.create({
titleContainer: {
flexDirection: "row",
alignItems: "center",
gap: 8,
},
stepContainer: {
gap: 8,
marginBottom: 8,
},
reactLogo: {
height: 178,
width: 290,
bottom: 0,
left: 0,
position: "absolute",
},
});