integrate api

This commit is contained in:
smfahim25 2024-12-18 17:33:58 +06:00
parent d5da713743
commit a8591c027b
4 changed files with 295 additions and 158 deletions

View file

@ -14,6 +14,17 @@
<option name="screenX" value="720" /> <option name="screenX" value="720" />
<option name="screenY" value="1280" /> <option name="screenY" value="1280" />
</PersistentDeviceSelectionData> </PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OPPO" />
<option name="codename" value="OP573DL1" />
<option name="id" value="OP573DL1" />
<option name="manufacturer" value="OPPO" />
<option name="name" value="CPH2557" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData> <PersistentDeviceSelectionData>
<option name="api" value="28" /> <option name="api" value="28" />
<option name="brand" value="DOCOMO" /> <option name="brand" value="DOCOMO" />

View file

@ -15,7 +15,8 @@
"adaptiveIcon": { "adaptiveIcon": {
"foregroundImage": "./assets/images/adaptive-icon.png", "foregroundImage": "./assets/images/adaptive-icon.png",
"backgroundColor": "#ffffff" "backgroundColor": "#ffffff"
} },
"package": "com.smfahimhossen.android"
}, },
"web": { "web": {
"bundler": "metro", "bundler": "metro",
@ -36,6 +37,14 @@
], ],
"experiments": { "experiments": {
"typedRoutes": true "typedRoutes": true
},
"extra": {
"router": {
"origin": false
},
"eas": {
"projectId": "aefb36d8-7851-4727-8f44-8968f1d51787"
}
} }
} }
} }

View file

@ -4,16 +4,130 @@ import { useRef } from "react";
export default function HomeScreen() { export default function HomeScreen() {
const webviewRef = useRef(null); const webviewRef = useRef(null);
const injectedJavaScript = ` let lastClickTime = 0; const injectedJavaScript = `
(function() {
try {
// Inject CSS for toast notifications
const style = document.createElement('style');
style.innerHTML = \`
#toast-container {
position: fixed;
top: 50px;
right: 20px;
left: 20px;
z-index: 999999;
display: flex;
flex-direction: column;
gap: 10px;
pointer-events: auto;
}
.toast {
background-color: #1db146;
color: #fff;
font-weight: 800;
padding: 12px 16px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
font-size: 16px;
opacity: 1;
position: relative;
width: 90%;
z-index: 99999999999;
transition: opacity 0.5s ease;
}
.toast.fade-out {
opacity: 0;
}
\`;
document.head.appendChild(style);
// Fact-checking logic
let lastClickTime = 0;
const COOLDOWN_DURATION = 60000; const COOLDOWN_DURATION = 60000;
let buttonClicked = false; let buttonClicked = false;
let fullCaption = ""; let fullCaption = "";
let imageUrls = []; let imageUrls = [];
let isApiCallInProgress = false;
// Toast notification function
function showToast(message, duration = 5000) {
const container = document.getElementById('toast-container') ||
(() => {
const cont = document.createElement('div');
cont.id = 'toast-container';
document.body.appendChild(cont);
return cont;
})();
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
container.appendChild(toast);
setTimeout(() => {
toast.classList.add('fade-out');
toast.addEventListener('transitionend', () => {
toast.remove();
});
}, duration);
}
// Function to disable all buttons
function disableAllButtons() {
document.querySelectorAll("button").forEach((btn) => {
btn.disabled = true;
btn.style.opacity = "0.5";
btn.style.cursor = "not-allowed";
});
}
// Function to enable all buttons
function enableAllButtons() {
document.querySelectorAll("button").forEach((btn) => {
btn.disabled = false;
btn.style.opacity = "1";
btn.style.cursor = "pointer";
});
}
// Fact-checking API call
async function checkFacts(caption, button) {
try {
isApiCallInProgress = true;
disableAllButtons();
const response = await fetch('http://localhost:8000/check-facts', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: caption }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const result = await response.json();
showToast(result.evidence);
button.textContent = 'Processed';
} catch (error) {
console.error('Error:', error);
showToast('An error occurred while checking facts');
} finally {
isApiCallInProgress = false;
enableAllButtons();
button.disabled = false;
button.style.opacity = "1";
}
}
function collectPostData(post) { function collectPostData(post) {
if (post.dataset.processed === "true") return; if (post.dataset.processed === "true") return;
const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text"); const captionElement = post.querySelector(
"div.m div.m div[data-type='text'] div.native-text"
);
let caption = captionElement ? captionElement.textContent.trim() : null; let caption = captionElement ? captionElement.textContent.trim() : null;
const imageElements = post.querySelectorAll("div.m.bg-s13 img"); const imageElements = post.querySelectorAll("div.m.bg-s13 img");
@ -29,8 +143,7 @@ export default function HomeScreen() {
post.dataset.processed = "true"; post.dataset.processed = "true";
const button = createButton(post, caption, imageURLs); const button = createButton(post, caption, imageURLs);
if (caption?.length > 30 || imageURLs.length > 0) {
if (caption && imageURLs.length > 0) {
post.style.position = "relative"; post.style.position = "relative";
post.appendChild(button); post.appendChild(button);
} }
@ -64,33 +177,6 @@ export default function HomeScreen() {
event.stopPropagation(); event.stopPropagation();
const currentTime = new Date().getTime(); const currentTime = new Date().getTime();
if (currentTime - lastClickTime < COOLDOWN_DURATION) {
const remainingTime = Math.ceil((COOLDOWN_DURATION - (currentTime - lastClickTime)) / 1000);
// Fix template literal issue by using escape characters
button.textContent = "Wait " + remainingTime + "s"; // Change here
button.style.background = "linear-gradient(135deg, #a0a0a0, #c0c0c0)";
button.style.cursor = "not-allowed";
button.disabled = true;
const cooldownTimer = setInterval(() => {
const updatedRemainingTime = Math.ceil((COOLDOWN_DURATION - (new Date().getTime() - lastClickTime)) / 1000);
if (updatedRemainingTime <= 0) {
clearInterval(cooldownTimer);
button.textContent = "Check";
button.style.background = "linear-gradient(135deg, #6a11cb, #2575fc)";
button.style.cursor = "pointer";
button.disabled = false;
return;
}
button.textContent = "Wait " + updatedRemainingTime + "s"; // Change here
}, 1000);
return;
}
lastClickTime = currentTime; lastClickTime = currentTime;
button.disabled = true; button.disabled = true;
@ -100,7 +186,13 @@ export default function HomeScreen() {
const seeMoreElement = post.querySelector("span[style*='color:#65676b']"); const seeMoreElement = post.querySelector("span[style*='color:#65676b']");
if (seeMoreElement) { if (seeMoreElement) {
handleSeeMoreClick(seeMoreElement, post, button, initialCaption, initialImageURLs); handleSeeMoreClick(
seeMoreElement,
post,
button,
initialCaption,
initialImageURLs
);
} else { } else {
collectDataImmediately(post, button, initialCaption, initialImageURLs); collectDataImmediately(post, button, initialCaption, initialImageURLs);
} }
@ -109,38 +201,40 @@ export default function HomeScreen() {
return button; return button;
} }
function handleSeeMoreClick(seeMoreElement, post, button, initialCaption, initialImageURLs) { async function handleSeeMoreClick(seeMoreElement, post, button, initialCaption, initialImageURLs) {
const currentTime = new Date().getTime(); return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
seeMoreElement.click(); seeMoreElement.click();
if (currentTime - lastClickTime < COOLDOWN_DURATION) {
buttonClicked = true; buttonClicked = true;
}
setTimeout(() => { setTimeout(async () => {
if (window.ReactNativeWebView) { await checkFacts(fullCaption, button);
window.ReactNativeWebView.postMessage(
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
})
);
}
alert(
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
}));
button.textContent = "Processed"; button.textContent = "Processed";
button.style.opacity = "0.7"; button.style.opacity = "0.7";
button.disabled = true; button.disabled = true;
resolve();
}, 2000); }, 2000);
}, 100); }, 100);
});
} }
function collectDataImmediately(post, button, initialCaption, initialImageURLs) {
const captionElement = post.querySelector("div.m div.m div[data-type='text'] div.native-text"); async function collectDataImmediately(
let caption = captionElement ? captionElement.textContent.trim() : initialCaption; post,
button,
initialCaption,
initialImageURLs
) {
buttonClicked = true;
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"); const imageElements = post.querySelectorAll("div.m img");
let imageURLs = []; let imageURLs = [];
@ -152,12 +246,7 @@ export default function HomeScreen() {
imageURLs = initialImageURLs; imageURLs = initialImageURLs;
} }
alert( await checkFacts(caption, button);
JSON.stringify({
caption: fullCaption,
imageUrls: imageUrls,
}));
button.textContent = "Processed"; button.textContent = "Processed";
button.style.opacity = "0.7"; button.style.opacity = "0.7";
button.disabled = true; button.disabled = true;
@ -189,12 +278,19 @@ export default function HomeScreen() {
} }
}); });
}); });
true; // Successful injection
} catch (error) {
console.error('Injection error:', error);
false;
}
})();
`; `;
return ( return (
<WebView <WebView
ref={webviewRef} ref={webviewRef}
source={{ uri: "https://m.facebook.com/?__n=K" }} source={{ uri: "https://m.facebook.com" }}
injectedJavaScript={injectedJavaScript} injectedJavaScript={injectedJavaScript}
startInLoadingState={true} startInLoadingState={true}
/> />

21
eas.json Normal file
View file

@ -0,0 +1,21 @@
{
"cli": {
"version": ">= 14.2.0",
"appVersionSource": "remote"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal"
},
"preview": {
"distribution": "internal"
},
"production": {
"autoIncrement": true
}
},
"submit": {
"production": {}
}
}