jihuaqiang 2 лет назад
Родитель
Сommit
4288197527
2 измененных файлов с 55 добавлено и 145 удалено
  1. 1 2
      src/pages/screens/twitter.tsx
  2. 54 143
      src/utils/contentInTwitterJS/addDom.js

+ 1 - 2
src/pages/screens/twitter.tsx

@@ -1,6 +1,6 @@
 /* eslint-disable react-native/no-inline-styles */
 import React, { Component } from 'react';
-import { View, Text, DeviceEventEmitter } from 'react-native';
+import { View, DeviceEventEmitter } from 'react-native';
 import { DenetWebview } from '@/components/webview';
 import { addDom } from '../../utils/contentInTwitterJS/addDom';
 // import EventEmitter from 'react-native-eventemitter';
@@ -53,7 +53,6 @@ class Twitter extends Component<Props> {
 	render() {
 		return (
 			<View style={{ flex: 1 }}>
-				<Text>{this.packUrl}</Text>
 				<DenetWebview
 					onLoadEndHandle={this.injectJavaScript}
 					refHandle={r => (this.webref = r)}

+ 54 - 143
src/utils/contentInTwitterJS/addDom.js

@@ -1,6 +1,7 @@
 import { parseAllDeNetCardParmas, getCardParmas } from './ParseCard';
 const addDom = () => {
-	window.parseAllDeNetCard = () => {
+	window.denetJS = {};
+	window.denetJS.parseAllDeNetCard = () => {
 		let de_net_card = [];
 		try {
 			let arr_article = document.querySelectorAll('article') || [];
@@ -19,7 +20,7 @@ const addDom = () => {
 		}
 		return de_net_card;
 	};
-	window.parseCardParmas = dom_card => {
+	window.denetJS.parseCardParmas = dom_card => {
 		let tweet_Id = '';
 		let short_url = '';
 		let a_arr = dom_card.querySelectorAll('a') || [];
@@ -47,29 +48,29 @@ const addDom = () => {
 		}
 		return { tweet_Id, short_url, dom_card };
 	};
-	// window.parseAllDeNetCardParmas = () => {
-	// 	let json_data = [];
-	// 	window.parseAllDeNetCard().forEach(item => {
-	// 		let _obj = window.parseCardParmas(item.dom);
-	// 		if (_obj.tweet_Id && _obj.short_url && _obj.dom_card) {
-	// 			_obj.time = item.time;
-	// 			json_data.push(_obj);
-	// 		}
-	// 	});
-	// 	return json_data;
-	// };
-	window.toRedpackPage = ({ post_Id, tweet_Id }) => {
+	window.denetJS.parseAllDeNetCardParmas = () => {
+		let json_data = [];
+		window.denetJS.parseAllDeNetCard().forEach(item => {
+			let _obj = window.denetJS.parseCardParmas(item.dom);
+			if (_obj.tweet_Id && _obj.short_url && _obj.dom_card) {
+				_obj.time = item.time;
+				json_data.push(_obj);
+			}
+		});
+		return json_data;
+	};
+	window.denetJS.toRedpackPage = ({ post_Id, tweet_Id }) => {
 		window.ReactNativeWebView.postMessage(
 			JSON.stringify({
 				post_Id,
 				tweet_Id,
-				ct0: window.getCookie('ct0'),
+				ct0: window.denetJS.getCookie('ct0'),
 			}),
 		);
 		window.event.stopPropagation();
 		// window.location.href = 'denet://redpack';
 	};
-	window.getCookie = name => {
+	window.denetJS.getCookie = name => {
 		var strcookie = document.cookie; //获取cookie字符串
 		var arrcookie = strcookie.split('; '); //分割
 		//遍历匹配
@@ -82,7 +83,7 @@ const addDom = () => {
 		return '';
 	};
 
-	window.TwitterLikeAPI = (id1, id2) => {
+	window.denetJS.TwitterLikeAPI = (id1, id2) => {
 		alert(`${id1}${id2}`);
 		fetch(
 			'https://mobile.twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet',
@@ -98,7 +99,7 @@ const addDom = () => {
 					'sec-fetch-dest': 'document',
 					'sec-fetch-mode': 'cors',
 					'sec-fetch-site': 'same-origin',
-					'x-csrf-token': window.getCookie('ct0'),
+					'x-csrf-token': window.denetJS.getCookie('ct0'),
 					'x-twitter-active-user': 'yes',
 					'x-twitter-auth-type': 'OAuth2Session',
 					'x-twitter-client-language': 'zh-cn',
@@ -121,6 +122,11 @@ const addDom = () => {
 				// alert(e);
 			});
 	};
+	function setStyle(obj, css) {
+		for (var atr in css) {
+			obj.style[atr] = css[atr];
+		}
+	}
 	function replaceDOMRedPacket({
 		dom_card,
 		tweet_Id,
@@ -134,31 +140,37 @@ const addDom = () => {
 		}
 		let type;
 		let dom = dom_card.querySelector('div[aria-labelledby]');
-		if (dom) {
-			type = 'card';
-			for (let i = 0; i < dom.childNodes.length; i++) {
-				if (
-					dom.childNodes[i].dataset &&
-					dom.childNodes[i].dataset.testid &&
-					dom.childNodes[i].dataset.testid == 'card.wrapper'
-				) {
-					dom.children[i].style.display = 'none';
-				}
-			}
-		} else {
-			type = 'txt';
-			dom = dom_card.querySelector('div[lang][dir=auto]').parentElement;
-		}
+		dom.style.position = 'relative';
+		// if (dom) {
+		// 	type = 'card';
+		// 	for (let i = 0; i < dom.childNodes.length; i++) {
+		// 		if (
+		// 			dom.childNodes[i].dataset &&
+		// 			dom.childNodes[i].dataset.testid &&
+		// 			dom.childNodes[i].dataset.testid == 'card.wrapper'
+		// 		) {
+		// 			dom.children[i].style.position = 'relative';
+		// 		}
+		// 	}
+		// } else {
+		// 	type = 'txt';
+		// 	dom = dom_card.querySelector('div[lang][dir=auto]').parentElement;
+		// }
 
 		// dom.style = 'min-height:500px';
 		if (dom) {
 			// debugger mode
 			let div = document.createElement('div');
 			div.addEventListener('click', () => {
-				window.toRedpackPage({ post_Id, tweet_Id });
+				window.denetJS.toRedpackPage({ post_Id, tweet_Id });
 			});
 			div.id = 'denet-card';
-			div.style.color = 'red';
+			setStyle(div, {
+				position: 'absolute',
+				width: '100%',
+				height: '100%',
+				'z-index': 1,
+			});
 			// div.innerHTML = `
 			// 	<h2>这里是自定义插入的内容</h2>
 			//     tweet_Id:${tweet_Id} ,
@@ -174,117 +186,16 @@ const addDom = () => {
 		}
 	}
 
-	let queue_num = 1;
-
-	const changeQueueNum = (num = 0) => {
-		queue_num = queue_num + num;
-		if (queue_num > 5) {
-			queue_num = 5;
-		}
-	};
-
 	try {
 		let timer = setInterval(() => {
-			let card_json_data = parseAllDeNetCardParmas();
-			getCardParmas(card_json_data).then(res => {
-				console.log('res', res);
-				// for (let i in res.has_post_Id_card_data) {
-				// 	let item = res.has_post_Id_card_data[i];
-				// 	if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		item.post_Id.indexOf('nft/') >= 0
-				// 	) {
-				// 		parseCard.replaceNftDomRedPacket(item);
-				// 	} else if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		item.post_Id.indexOf('nft_group/') >= 0
-				// 	) {
-				// 		parseCard.replaceNftGroupDomRedPacket(item);
-				// 	} else if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		item.post_Id.indexOf('luckdraw/') >= 0
-				// 	) {
-				// 		item.post_Id = item.post_Id.split('luckdraw/')[1] || '';
-				// 		item.page_type = '抽奖';
-				// 		parseCard.replaceDOMRedPacket(item);
-				// 	} else if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		item.post_Id.indexOf('toolbox/') >= 0
-				// 	) {
-				// 		item.page_type = 'toolbox';
-				// 		item.post_Id = item.post_Id.split('toolbox/')[1] || '';
-				// 		if (item.post_Id) {
-				// 			parseCard.replaceIframeToolBox(item);
-				// 		}
-				// 	} else if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		!item.post_Id.includes('/')
-				// 	) {
-				// 		item.page_type = '红包';
-				// 		parseCard.replaceDOMRedPacket(item);
-				// 		// 夺宝链接
-				// 	} else if (
-				// 		item &&
-				// 		item.post_Id &&
-				// 		item.post_Id.includes('treasure/')
-				// 	) {
-				// 		// https://testh5.denetme.net/treasure/{postid}
-				// 		// https://testh5.denetme.net/treasure/invite/{inviteCode}
-				// 		// 邀请链接
-				// 		if (item.post_Id.includes('invite/')) {
-				// 			let arr = item.post_Id.split('/');
-				// 			let index;
-				// 			for (let i in arr) {
-				// 				index = Number(i) + 1;
-				// 				if (arr[i] == 'invite' && arr.length >= index) {
-				// 					item.invite_code = arr[index];
-				// 					if (arr.length > index + 1) {
-				// 						item.invite_channel = arr[index + 1];
-				// 					} else {
-				// 						item.invite_channel = '';
-				// 					}
-				// 					break;
-				// 				}
-				// 			}
-
-				// 			item.page_type = '邀请链接';
-				// 			if (item.invite_code) {
-				// 				item.post_Id = '';
-				// 				parseCard.replaceDOMTreasureCard(item);
-				// 			}
-				// 		} else {
-				// 			// 原始链接
-				// 			item.page_type = '原始链接';
-				// 			item.post_Id =
-				// 				item.post_Id.split('treasure/')[1] || '';
-				// 			if (item.post_Id) {
-				// 				parseCard.replaceDOMTreasureCard(item);
-				// 			}
-				// 		}
-				// 	}
-				// }
-				// if (res.need_net_short_urls.length > 0) {
-				// 	parseCard.netShortUrl(res.need_net_short_urls, () => {
-				// 		changeQueueNum(5);
-				// 	});
-				// }
-			});
+			let card_json_data = window.denetJS.parseAllDeNetCardParmas();
+			if (card_json_data.length) {
+				// clearInterval(timer);
+				for (let i = 0; i < card_json_data.length; i++) {
+					replaceDOMRedPacket(card_json_data[i]);
+				}
+			}
 		}, 1000);
-
-		// let contain = document.getElementsByTagName('body')[0];
-		// let iframe = document.createElement('iframe');
-		// iframe.setAttribute('id', 'elonmusk');
-		// iframe.setAttribute('src', 'https://denet.me');
-		// iframe.setAttribute(
-		// 	'style',
-		// 	'width:375px;min-height:50px; position: fixed; left: 0;top:100px;z-index:1000',
-		// );
-		// contain.appendChild(iframe);
 	} catch (error) {
 		console.log(error);
 	}