addDom.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. const addDataToStorge = data => {
  2. function getStorage(key) {
  3. const item = window.localStorage.getItem(key);
  4. try {
  5. return item ? JSON.parse(item) : [];
  6. } catch (e) {
  7. return item;
  8. }
  9. }
  10. function setStorage(key, value) {
  11. return window.localStorage.setItem(key, JSON.stringify(value) || '');
  12. }
  13. try {
  14. let allData = getStorage('denetCardData');
  15. if (
  16. data.short_url &&
  17. (data.post_Id || data.invite_code) &&
  18. !allData.find(item => item.short_url === data.short_url)
  19. ) {
  20. allData.push(data);
  21. console.log('addDataToStorge', allData);
  22. setStorage('denetCardData', allData);
  23. return true;
  24. }
  25. } catch (error) {
  26. console.log('error', error);
  27. }
  28. };
  29. const addDom = () => {
  30. window.denetJS = {};
  31. window.denetJS.parseAllDeNetCard = () => {
  32. let de_net_card = [];
  33. try {
  34. let arr_article = document.querySelectorAll('article') || [];
  35. let _txt;
  36. for (let i in arr_article) {
  37. _txt = arr_article[i].innerText || '';
  38. if (_txt.includes('#DeNet') || _txt.includes('#DNFT')) {
  39. de_net_card.push({
  40. time: new Date().getTime(),
  41. dom: arr_article[i],
  42. });
  43. }
  44. }
  45. } catch (error) {
  46. console.log(error);
  47. }
  48. return de_net_card;
  49. };
  50. window.denetJS.parseCardParmas = dom_card => {
  51. let tweet_Id = '';
  52. let short_url = '';
  53. let a_arr = dom_card.querySelectorAll('a') || [];
  54. a_arr = Array.from(a_arr).reverse();
  55. for (let i in a_arr) {
  56. // 获取推特id
  57. if (
  58. a_arr[i].href &&
  59. a_arr[i].href.indexOf('/status/') > 0 &&
  60. !tweet_Id
  61. ) {
  62. tweet_Id = a_arr[i].href.split('/status/')[1] || '';
  63. tweet_Id = tweet_Id.split('/')[0];
  64. }
  65. if (
  66. a_arr[i].href &&
  67. a_arr[i].href.includes('https://t.co') &&
  68. !short_url
  69. ) {
  70. short_url = a_arr[i].href;
  71. }
  72. if (tweet_Id && short_url) {
  73. break;
  74. }
  75. }
  76. return { tweet_Id, short_url, dom_card };
  77. };
  78. function parseAllDeNetCardParmas() {
  79. let json_data = [];
  80. window.denetJS.parseAllDeNetCard().forEach(item => {
  81. let _obj = window.denetJS.parseCardParmas(item.dom);
  82. if (_obj.tweet_Id && _obj.short_url && _obj.dom_card) {
  83. _obj.time = item.time;
  84. json_data.push(_obj);
  85. }
  86. });
  87. return json_data;
  88. }
  89. window.denetJS.toRedpackPage = item => {
  90. window.ReactNativeWebView.postMessage(
  91. JSON.stringify({
  92. actionType: 'goDetailPage',
  93. data: {
  94. ...item,
  95. ct0: window.denetJS.getCookie('ct0'),
  96. },
  97. }),
  98. );
  99. window.event.stopPropagation();
  100. // window.location.href = 'denet://redpack';
  101. };
  102. window.denetJS.getCookie = name => {
  103. var strcookie = document.cookie; //获取cookie字符串
  104. var arrcookie = strcookie.split('; '); //分割
  105. //遍历匹配
  106. for (var i = 0; i < arrcookie.length; i++) {
  107. var arr = arrcookie[i].split('=');
  108. if (arr[0] == name) {
  109. return arr[1];
  110. }
  111. }
  112. return '';
  113. };
  114. window.denetJS.TwitterLikeAPI = (id1, id2) => {
  115. fetch(
  116. 'https://mobile.twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet',
  117. {
  118. headers: {
  119. accept: '*/*',
  120. 'accept-language': 'zh-CN,zh;q=0.9',
  121. authorization:
  122. 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
  123. 'cache-control': 'no-cache',
  124. 'content-type': 'application/json',
  125. pragma: 'no-cache',
  126. 'sec-fetch-dest': 'document',
  127. 'sec-fetch-mode': 'cors',
  128. 'sec-fetch-site': 'same-origin',
  129. 'x-csrf-token': window.denetJS.getCookie('ct0'),
  130. 'x-twitter-active-user': 'yes',
  131. 'x-twitter-auth-type': 'OAuth2Session',
  132. 'x-twitter-client-language': 'zh-cn',
  133. },
  134. referrer:
  135. 'https://mobile.twitter.com/laoghaire99/status/1559470845076000769',
  136. referrerPolicy: 'strict-origin-when-cross-origin',
  137. body: `{"variables":{"tweet_id":"${id1}${id2}"},"queryId":"lI07N6Otwv1PhnEgXILM7A"}`,
  138. method: 'POST',
  139. mode: 'cors',
  140. credentials: 'include',
  141. },
  142. )
  143. .then(res => {
  144. console.log(res);
  145. window.ReactNativeWebView.postMessage(
  146. JSON.stringify({
  147. actionType: 'TwitterLikeSuccess',
  148. data: {
  149. tweetId: `${id1}${id2}`,
  150. do_type: 'api',
  151. task_type: 'like',
  152. task_done: true,
  153. task_data: '',
  154. },
  155. }),
  156. );
  157. })
  158. .catch(e => {
  159. // alert(window.getCookie('ct0'))
  160. // alert(e);
  161. });
  162. };
  163. window.denetJS.TwitterRetweetAPI = (id1, id2) => {
  164. let url =
  165. 'https://mobile.twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet';
  166. fetch(url, {
  167. headers: {
  168. accept: '*/*',
  169. 'accept-language': 'zh,en;q=0.9,zh-CN;q=0.8',
  170. authorization:
  171. 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
  172. 'content-type': 'application/json',
  173. 'sec-ch-ua':
  174. '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
  175. 'sec-ch-ua-mobile': '?0',
  176. 'sec-ch-ua-platform': '"macOS"',
  177. 'sec-fetch-dest': 'empty',
  178. 'sec-fetch-mode': 'cors',
  179. 'sec-fetch-site': 'same-origin',
  180. 'x-csrf-token': window.denetJS.getCookie('ct0'),
  181. 'x-twitter-active-user': 'yes',
  182. 'x-twitter-auth-type': 'OAuth2Session',
  183. 'x-twitter-client-language': 'zh-cn',
  184. },
  185. referrer: 'https://twitter.com/home',
  186. referrerPolicy: 'strict-origin-when-cross-origin',
  187. body:
  188. '{"variables":{"tweet_id":"' +
  189. id1 +
  190. id2 +
  191. '","dark_request":false},"queryId":"ojPdsZsimiJrUGLR1sjUtA"}',
  192. method: 'POST',
  193. mode: 'cors',
  194. credentials: 'include',
  195. })
  196. .then(res => {
  197. console.log(res);
  198. window.ReactNativeWebView.postMessage(
  199. JSON.stringify({
  200. actionType: 'RetweetSuccess',
  201. data: {
  202. tweetId: `${id1}${id2}`,
  203. do_type: 'api',
  204. task_type: 'retweet',
  205. task_done: true,
  206. task_data: '',
  207. },
  208. }),
  209. );
  210. })
  211. .catch(() => {
  212. console.log('DO0_TASK4');
  213. });
  214. };
  215. function setStyle(obj, css) {
  216. for (var atr in css) {
  217. obj.style[atr] = css[atr];
  218. }
  219. }
  220. function replaceDOMRedPacket(item) {
  221. const {
  222. dom_card,
  223. tweet_Id,
  224. post_Id,
  225. time,
  226. short_url,
  227. page_type = '',
  228. invite_code,
  229. } = item;
  230. if (!dom_card || !dom_card.parentElement) {
  231. return;
  232. }
  233. let type;
  234. let dom = dom_card.querySelector('div[aria-labelledby]');
  235. dom.style.position = 'relative';
  236. if (dom) {
  237. for (let i = 0; i < dom.childNodes.length; i++) {
  238. if (
  239. dom.childNodes[i].id &&
  240. dom.childNodes[i].id == 'denet-card'
  241. ) {
  242. return;
  243. }
  244. }
  245. }
  246. if (dom) {
  247. // debugger mode
  248. let div = document.createElement('div');
  249. div.addEventListener('click', () => {
  250. window.denetJS.toRedpackPage({
  251. tweet_Id,
  252. post_Id,
  253. time,
  254. short_url,
  255. page_type,
  256. invite_code,
  257. });
  258. });
  259. div.id = 'denet-card';
  260. setStyle(div, {
  261. position: 'absolute',
  262. width: '100%',
  263. height: '100%',
  264. background: 'rgba(0,0,0,.3)',
  265. 'z-index': 1,
  266. 'text-align': 'center',
  267. color: '#fff',
  268. 'font-size': '30px',
  269. });
  270. div.innerText = 'OPEN IT';
  271. dom.appendChild(div);
  272. }
  273. }
  274. function setStorage(key, value) {
  275. return window.localStorage.setItem(key, JSON.stringify(value) || '');
  276. }
  277. function getStorage(key) {
  278. const item = window.localStorage.getItem(key);
  279. try {
  280. return item ? JSON.parse(item) : '';
  281. } catch (e) {
  282. return item;
  283. }
  284. }
  285. function getCardFromStorge(query) {
  286. return new Promise((res, rej) => {
  287. const denetCardData = getStorage('denetCardData') || [];
  288. const thisCardData = denetCardData.find(
  289. item => item.short_url === query.short_url,
  290. );
  291. if (thisCardData) {
  292. res(thisCardData);
  293. } else {
  294. window.ReactNativeWebView.postMessage(
  295. JSON.stringify({
  296. actionType: 'fetchPostId',
  297. data: {
  298. // dom_card: query.dom_card,
  299. short_url: query.short_url,
  300. time: query.time,
  301. tweet_Id: query.tweet_Id,
  302. },
  303. }),
  304. );
  305. res(null);
  306. }
  307. });
  308. }
  309. try {
  310. let timer = setInterval(() => {
  311. let card_json_data = parseAllDeNetCardParmas();
  312. if (card_json_data.length) {
  313. for (let i = 0; i < card_json_data.length; i++) {
  314. getCardFromStorge(card_json_data[i]).then(thisCardData => {
  315. if (
  316. thisCardData &&
  317. (thisCardData.post_Id || thisCardData.invite_code)
  318. ) {
  319. replaceDOMRedPacket({
  320. ...card_json_data[i],
  321. ...thisCardData,
  322. });
  323. }
  324. });
  325. }
  326. }
  327. }, 3000);
  328. } catch (error) {
  329. console.log(error);
  330. }
  331. };
  332. export { addDom, addDataToStorge };