123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- const addDataToStorge = data => {
- function getStorage(key) {
- const item = window.localStorage.getItem(key);
- try {
- return item ? JSON.parse(item) : [];
- } catch (e) {
- return item;
- }
- }
- function setStorage(key, value) {
- return window.localStorage.setItem(key, JSON.stringify(value) || '');
- }
- try {
- let allData = getStorage('denetCardData');
- if (
- data.short_url &&
- (data.post_Id || data.invite_code) &&
- !allData.find(item => item.short_url === data.short_url)
- ) {
- allData.push(data);
- console.log('addDataToStorge', allData);
- setStorage('denetCardData', allData);
- return true;
- }
- } catch (error) {
- console.log('error', error);
- }
- };
- const addDom = () => {
- window.denetJS = {};
- window.denetJS.parseAllDeNetCard = () => {
- let de_net_card = [];
- try {
- let arr_article = document.querySelectorAll('article') || [];
- let _txt;
- for (let i in arr_article) {
- _txt = arr_article[i].innerText || '';
- if (_txt.includes('#DeNet') || _txt.includes('#DNFT')) {
- de_net_card.push({
- time: new Date().getTime(),
- dom: arr_article[i],
- });
- }
- }
- } catch (error) {
- console.log(error);
- }
- return de_net_card;
- };
- window.denetJS.parseCardParmas = dom_card => {
- let tweet_Id = '';
- let short_url = '';
- let a_arr = dom_card.querySelectorAll('a') || [];
- a_arr = Array.from(a_arr).reverse();
- for (let i in a_arr) {
- // 获取推特id
- if (
- a_arr[i].href &&
- a_arr[i].href.indexOf('/status/') > 0 &&
- !tweet_Id
- ) {
- tweet_Id = a_arr[i].href.split('/status/')[1] || '';
- tweet_Id = tweet_Id.split('/')[0];
- }
- if (
- a_arr[i].href &&
- a_arr[i].href.includes('https://t.co') &&
- !short_url
- ) {
- short_url = a_arr[i].href;
- }
- if (tweet_Id && short_url) {
- break;
- }
- }
- return { tweet_Id, short_url, dom_card };
- };
- function 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 = item => {
- window.ReactNativeWebView.postMessage(
- JSON.stringify({
- actionType: 'goDetailPage',
- data: {
- ...item,
- ct0: window.denetJS.getCookie('ct0'),
- },
- }),
- );
- window.event.stopPropagation();
- // window.location.href = 'denet://redpack';
- };
- window.denetJS.getCookie = name => {
- var strcookie = document.cookie; //获取cookie字符串
- var arrcookie = strcookie.split('; '); //分割
- //遍历匹配
- for (var i = 0; i < arrcookie.length; i++) {
- var arr = arrcookie[i].split('=');
- if (arr[0] == name) {
- return arr[1];
- }
- }
- return '';
- };
- window.denetJS.TwitterLikeAPI = (id1, id2) => {
- fetch(
- 'https://mobile.twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet',
- {
- headers: {
- accept: '*/*',
- 'accept-language': 'zh-CN,zh;q=0.9',
- authorization:
- 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
- 'cache-control': 'no-cache',
- 'content-type': 'application/json',
- pragma: 'no-cache',
- 'sec-fetch-dest': 'document',
- 'sec-fetch-mode': 'cors',
- 'sec-fetch-site': 'same-origin',
- 'x-csrf-token': window.denetJS.getCookie('ct0'),
- 'x-twitter-active-user': 'yes',
- 'x-twitter-auth-type': 'OAuth2Session',
- 'x-twitter-client-language': 'zh-cn',
- },
- referrer:
- 'https://mobile.twitter.com/laoghaire99/status/1559470845076000769',
- referrerPolicy: 'strict-origin-when-cross-origin',
- body: `{"variables":{"tweet_id":"${id1}${id2}"},"queryId":"lI07N6Otwv1PhnEgXILM7A"}`,
- method: 'POST',
- mode: 'cors',
- credentials: 'include',
- },
- )
- .then(res => {
- console.log(res);
- window.ReactNativeWebView.postMessage(
- JSON.stringify({
- actionType: 'TwitterLikeSuccess',
- data: {
- tweetId: `${id1}${id2}`,
- do_type: 'api',
- task_type: 'like',
- task_done: true,
- task_data: '',
- },
- }),
- );
- })
- .catch(e => {
- // alert(window.getCookie('ct0'))
- // alert(e);
- });
- };
- window.denetJS.TwitterRetweetAPI = (id1, id2) => {
- let url =
- 'https://mobile.twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet';
- fetch(url, {
- headers: {
- accept: '*/*',
- 'accept-language': 'zh,en;q=0.9,zh-CN;q=0.8',
- authorization:
- 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
- 'content-type': 'application/json',
- 'sec-ch-ua':
- '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
- 'sec-ch-ua-mobile': '?0',
- 'sec-ch-ua-platform': '"macOS"',
- 'sec-fetch-dest': 'empty',
- 'sec-fetch-mode': 'cors',
- 'sec-fetch-site': 'same-origin',
- 'x-csrf-token': window.denetJS.getCookie('ct0'),
- 'x-twitter-active-user': 'yes',
- 'x-twitter-auth-type': 'OAuth2Session',
- 'x-twitter-client-language': 'zh-cn',
- },
- referrer: 'https://twitter.com/home',
- referrerPolicy: 'strict-origin-when-cross-origin',
- body:
- '{"variables":{"tweet_id":"' +
- id1 +
- id2 +
- '","dark_request":false},"queryId":"ojPdsZsimiJrUGLR1sjUtA"}',
- method: 'POST',
- mode: 'cors',
- credentials: 'include',
- })
- .then(res => {
- console.log(res);
- window.ReactNativeWebView.postMessage(
- JSON.stringify({
- actionType: 'RetweetSuccess',
- data: {
- tweetId: `${id1}${id2}`,
- do_type: 'api',
- task_type: 'retweet',
- task_done: true,
- task_data: '',
- },
- }),
- );
- })
- .catch(() => {
- console.log('DO0_TASK4');
- });
- };
- function setStyle(obj, css) {
- for (var atr in css) {
- obj.style[atr] = css[atr];
- }
- }
- function replaceDOMRedPacket(item) {
- const {
- dom_card,
- tweet_Id,
- post_Id,
- time,
- short_url,
- page_type = '',
- invite_code,
- } = item;
- if (!dom_card || !dom_card.parentElement) {
- return;
- }
- let type;
- let dom = dom_card.querySelector('div[aria-labelledby]');
- dom.style.position = 'relative';
- if (dom) {
- for (let i = 0; i < dom.childNodes.length; i++) {
- if (
- dom.childNodes[i].id &&
- dom.childNodes[i].id == 'denet-card'
- ) {
- return;
- }
- }
- }
- if (dom) {
- // debugger mode
- let div = document.createElement('div');
- div.addEventListener('click', () => {
- window.denetJS.toRedpackPage({
- tweet_Id,
- post_Id,
- time,
- short_url,
- page_type,
- invite_code,
- });
- });
- div.id = 'denet-card';
- setStyle(div, {
- position: 'absolute',
- width: '100%',
- height: '100%',
- background: 'rgba(0,0,0,.3)',
- 'z-index': 1,
- 'text-align': 'center',
- color: '#fff',
- 'font-size': '30px',
- });
- div.innerText = 'OPEN IT';
- dom.appendChild(div);
- }
- }
- function setStorage(key, value) {
- return window.localStorage.setItem(key, JSON.stringify(value) || '');
- }
- function getStorage(key) {
- const item = window.localStorage.getItem(key);
- try {
- return item ? JSON.parse(item) : '';
- } catch (e) {
- return item;
- }
- }
- function getCardFromStorge(query) {
- return new Promise((res, rej) => {
- const denetCardData = getStorage('denetCardData') || [];
- const thisCardData = denetCardData.find(
- item => item.short_url === query.short_url,
- );
- if (thisCardData) {
- res(thisCardData);
- } else {
- window.ReactNativeWebView.postMessage(
- JSON.stringify({
- actionType: 'fetchPostId',
- data: {
- // dom_card: query.dom_card,
- short_url: query.short_url,
- time: query.time,
- tweet_Id: query.tweet_Id,
- },
- }),
- );
- res(null);
- }
- });
- }
- try {
- let timer = setInterval(() => {
- let card_json_data = parseAllDeNetCardParmas();
- if (card_json_data.length) {
- for (let i = 0; i < card_json_data.length; i++) {
- getCardFromStorge(card_json_data[i]).then(thisCardData => {
- if (
- thisCardData &&
- (thisCardData.post_Id || thisCardData.invite_code)
- ) {
- replaceDOMRedPacket({
- ...card_json_data[i],
- ...thisCardData,
- });
- }
- });
- }
- }
- }, 3000);
- } catch (error) {
- console.log(error);
- }
- };
- export { addDom, addDataToStorge };
|