twitter.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. import { getChromeStorage, setChromeStorage } from '@/uilts/chromeExtension.js'
  2. import { throttle, getQueryString, getCookie, nextTick } from '@/uilts/help'
  3. import { discordAuthRedirectUri } from '@/http/configAPI'
  4. import { reportSrcPublishEvent } from '@/http/publishApi'
  5. import Report from "@/log-center/log"
  6. import { fetchAddFinishEvent } from '@/logic/background/fetch/facebook';
  7. let dom = {};
  8. function twitterPinLogin() {
  9. if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
  10. let code = document.querySelector('code')
  11. if (code) {
  12. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_CODE", code: code.innerText }, () => { })
  13. // port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
  14. }
  15. }
  16. }
  17. function getDiscordAuthCode() {
  18. if (window.location.href.indexOf(discordAuthRedirectUri) > -1) {
  19. const urlParams = new URLSearchParams(window.location.search);
  20. const code = urlParams.get('code');
  21. if (code) {
  22. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_DISCORD_AUTH_CODE", code }, () => { })
  23. }
  24. }
  25. };
  26. /**
  27. * 渲染要插入的dom,初始化逻辑
  28. * @param port
  29. */
  30. function renderDom() {
  31. if (window.location.href.indexOf('https://twitter.com') > -1) {
  32. _createBtnDom();
  33. onWindowResize();
  34. checkHasDeBtn();
  35. setTimeout(() => {
  36. _addIframe();
  37. _addDeNetBtn();
  38. _getSliderTwitterBtn();
  39. }, 800)
  40. }
  41. }
  42. /**
  43. * 展示give弹窗
  44. */
  45. export function showGiveDialogHandler(userInfo) {
  46. let iframe = document.getElementById('iframe-content');
  47. if (iframe) {
  48. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  49. } else {
  50. _addIframe();
  51. let iframe = document.getElementById('iframe-content');
  52. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  53. }
  54. }
  55. export function showIframeHandler() {
  56. document.getElementById('iframe-content').style.display = 'block';
  57. }
  58. export function hideIframeHandler() {
  59. document.getElementById('iframe-content').style.display = 'none';
  60. }
  61. /**
  62. * 展示twitter原生发布框
  63. */
  64. let tweetPublishStore = {
  65. showPublishDialog: false
  66. }
  67. export function showTwitterPublishDialogHandler(publishRes) {
  68. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  69. if (bigBtn) {
  70. bigBtn.click();
  71. tweetPublishStore.showPublishDialog = true;
  72. } else {
  73. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  74. smallBtn && smallBtn.click();
  75. tweetPublishStore.showPublishDialog = true;
  76. }
  77. setChromeStorage({ publishData: JSON.stringify(publishRes) })
  78. addPublishTipsIframe({
  79. srcContent: publishRes.copyContent
  80. })
  81. _setPublishContent(publishRes.srcContent);
  82. _publishTweetEvent(publishRes, bindTwitterArtMethod);
  83. }
  84. export function twitterPublishHandler(res) {
  85. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  86. if (bigBtn) {
  87. bigBtn.click();
  88. } else {
  89. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  90. smallBtn && smallBtn.click();
  91. }
  92. nextTick(() => {
  93. document.execCommand('selectAll');
  94. }, 100).then(() => {
  95. document.execCommand('delete');
  96. _setPublishContent(res.srcContent, 200);
  97. })
  98. }
  99. export function showPinTips() {
  100. getChromeStorage('pinData', (res) => {
  101. if (!res || res.show) {
  102. let domPop = document.getElementById('de-pin-pop');
  103. domPop.style.display = 'block';
  104. }
  105. })
  106. }
  107. export function hidePinTips() {
  108. hidePopupPage();
  109. hideNoticeBindTweet();
  110. let pop = document.querySelector('#de-pin-pop');
  111. if(pop) {
  112. pop.style.display = 'none';
  113. }
  114. }
  115. function addPinedPop() {
  116. let domPop = document.getElementById('de-pin-pop');
  117. if (domPop) {
  118. return;
  119. }
  120. let popWrapper = document.createElement('div');
  121. popWrapper.style.cssText = 'position: fixed; height: 400px;width: 300px;top: 12px;right: 20px;border-radius: 12px;border: 0.5px solid #919191;box-sizing: border-box;padding: 20px;background: #fff;display:none';
  122. popWrapper.id = 'de-pin-pop'
  123. let img = document.createElement('img');
  124. img.src = require("@/assets/img/img-pined-guide.png");
  125. img.width = 253;
  126. let contentDom = document.createElement('div');
  127. contentDom.innerHTML = "<div style='font-weight: 500;font-size: 18px;margin-top: 20px;margin-bottom: 20px'>📌 Pin an Extension is more convenient to open😄</div><div style='display: flex; align-items: center; justify-content: space-between;'><div style='display: flex; align-items: center; font-size: 14px; color: #899099; cursor: pointer;' id='de-remind'><input id='de-check' type='checkbox'/> Don't remind</div><div class='de-pin-skip' style='font-weight: 500; font-size: 16px; color: #1D9BF0;cursor: pointer;'>Skip</div></div>"
  128. popWrapper.appendChild(img);
  129. popWrapper.appendChild(contentDom);
  130. document.querySelector('body').appendChild(popWrapper);
  131. let deCheck = document.querySelector('#de-check');
  132. let deRemind = document.querySelector('#de-remind');
  133. deCheck.onclick = function (e) {
  134. e && e.stopPropagation && e.stopPropagation();
  135. setChromeStorage({ pinData: JSON.stringify({ show: !this.checked }) })
  136. }
  137. deRemind.onclick = function () {
  138. deCheck.checked = !deCheck.checked;
  139. setChromeStorage({ pinData: JSON.stringify({ show: !deCheck.checked }) })
  140. }
  141. document.querySelector('.de-pin-skip').onclick = function () {
  142. document.querySelector('#de-pin-pop').style.display = 'none';
  143. }
  144. }
  145. function getUserInfo(cb) {
  146. getChromeStorage('userInfo', (res) => {
  147. cb && cb(res);
  148. })
  149. }
  150. // 绑定推文id所需参数
  151. let bindTwitterArt = {
  152. needBind: false,
  153. postId: '',
  154. isBindIng: false
  155. };
  156. /**
  157. * 监听dialog内点击原生发布按钮事件
  158. * @private
  159. */
  160. function _publishTweetEvent(params, cb) {
  161. setTimeout(() => {
  162. let publishTweetBtn;
  163. let dialog = document.querySelector('div[role="dialog"]');
  164. if (dialog) {
  165. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  166. onClosePublishDialogHandle(dialog, params)
  167. } else {
  168. let domMain = document.querySelector('main[role="main"]');
  169. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  170. }
  171. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  172. bindTwitterArt.needBind = true;
  173. bindTwitterArt.postId = params.postId;
  174. tweetPublishStore.showPublishDialog = false;
  175. // checkIsShowReSend(dialog, params);
  176. cb && cb()
  177. });
  178. }, 800)
  179. }
  180. function onClosePublishDialogHandle(dom, params) {
  181. dom.querySelector('div[role="group"]').addEventListener('click', function () {
  182. setTimeout(() => {
  183. let parent = document.querySelector('div[data-testid="confirmationSheetDialog"]');
  184. if (parent) {
  185. let btnArr = parent.querySelectorAll('div[role=button]')
  186. for (let i = 0; i < btnArr.length; i++) {
  187. let btn = btnArr[i];
  188. btn.addEventListener('click', function () {
  189. tweetPublishStore.showPublishDialog = false;
  190. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  191. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  192. })
  193. }
  194. } else {
  195. setTimeout(() => {
  196. let dialog = document.querySelector('div[role="dialog"]');
  197. if (!dialog) {
  198. tweetPublishStore.showPublishDialog = false;
  199. }
  200. }, 800)
  201. }
  202. }, 1000)
  203. })
  204. }
  205. function checkIsShowReSend(dom, params) {
  206. let str = dom.querySelector('div[data-contents="true"]').innerHTML;
  207. if (str.indexOf(params.postId) < 0) {
  208. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  209. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  210. }
  211. }
  212. /**
  213. * 在输入推文区插入deNet按钮
  214. * @param parent
  215. * @param dom
  216. * @param isClick
  217. * @private
  218. */
  219. function _addDeNetEditBtn(parent, dom, isClick = false) {
  220. setTimeout(() => {
  221. if (parent && parent.parentNode) {
  222. Report.reportLog({
  223. pageSource: Report.pageSource.mainPage,
  224. businessType: Report.businessType.buttonView,
  225. objectType: Report.objectType.buttonSecond
  226. });
  227. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  228. } else {
  229. setTimeout(() => {
  230. parent = _getScheduleDom(isClick);
  231. if (parent && parent.parentNode) {
  232. Report.reportLog({
  233. pageSource: Report.pageSource.mainPage,
  234. businessType: Report.businessType.buttonView,
  235. objectType: Report.objectType.buttonSecond
  236. });
  237. let innerDeIcon = document.getElementById('de-btn1');
  238. if (!innerDeIcon) {
  239. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  240. }
  241. }
  242. }, 1000)
  243. }
  244. })
  245. }
  246. /**
  247. * 在dialog插入deNet按钮
  248. * @private
  249. */
  250. // function _addDeNetBtnToDialog() {
  251. // setTimeout(() => {
  252. // let dialogScheduleBtn = _getScheduleDom(true);
  253. // _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  254. // }, 800)
  255. // }
  256. /**
  257. * 获取左侧twitter按钮
  258. * @private
  259. */
  260. function _getSliderTwitterBtn() {
  261. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  262. dom.tweetBtn.addEventListener('click', function () {
  263. // _addDeNetBtnToDialog();
  264. })
  265. }
  266. /**
  267. * 添加deNet按钮
  268. * @private
  269. */
  270. function _addDeNetBtn() {
  271. setTimeout(() => {
  272. let navWidth = document.querySelector('nav[role="navigation"]').offsetWidth;
  273. addSliderNavDeBtn(navWidth < 245);
  274. let innerDeIcon = document.getElementById('de-btn1');
  275. if (!innerDeIcon) {
  276. let dialogScheduleBtn = _getScheduleDom(false);
  277. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  278. }
  279. }, 800)
  280. }
  281. /**
  282. * 获取推文输入框内dom,用于插入deNet
  283. * @param isDialogInner
  284. * @returns {Element}
  285. * @private
  286. */
  287. function _getScheduleDom(isDialogInner = false) {
  288. let scheduleBtn;
  289. if (isDialogInner) {
  290. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  291. } else {
  292. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  293. if (toolBar) {
  294. scheduleBtn = toolBar.querySelector('div[data-testid="geoButton"]');
  295. }
  296. }
  297. return scheduleBtn;
  298. }
  299. /**
  300. * 插入iframe到页面
  301. * @private
  302. */
  303. function _addIframe() {
  304. // let span = document.createElement('span');
  305. // const shadowRoot = span.attachShadow({mode: 'closed'})
  306. let iframe = document.createElement('iframe');
  307. iframe.src = chrome.runtime.getURL('/iframe/publish.html')
  308. iframe.id = 'iframe-content'
  309. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:10000; border: medium none;display:none';
  310. // shadowRoot.appendChild(iframe);
  311. // document.body.appendChild(span)
  312. dom.iframe = iframe;
  313. let iframeContent = document.getElementById('iframe-content');
  314. if (!iframeContent) {
  315. document.querySelector('body').appendChild(iframe);
  316. }
  317. }
  318. function addPublishTipsIframe(params = {}) {
  319. let { time = 1000 } = params;
  320. setTimeout(() => {
  321. let dialog = document.querySelector('div[role="dialog"]').querySelector('div[role="dialog"]')
  322. if (dialog) {
  323. let right = dialog.offsetLeft - 15 - 266;
  324. let iframe = document.createElement('iframe');
  325. iframe.id = 'de-publish-tips'
  326. iframe.src = chrome.runtime.getURL('/iframe/publish-tips.html');
  327. iframe.style.cssText = `border: medium none; width:270px;height:500px;position: fixed; right: ${right}px; top: 5%;z-index: -1`
  328. let iframeContent = document.getElementById('de-publish-tips');
  329. if (!iframeContent) {
  330. dialog.appendChild(iframe)
  331. }
  332. }
  333. }, time)
  334. }
  335. export function noticeBindTweet(params) {
  336. let iframe = document.createElement('iframe');
  337. iframe.id = 'de-notice-bind-tweet';
  338. iframe.src = chrome.runtime.getURL('/iframe/bind-tweet.html') + `?params=${JSON.stringify(params)}`;
  339. iframe.style.cssText = `border: medium none; width:400px;min-height:313px;position: fixed; right: 16px; top: 16px;`
  340. let iframeContent = document.getElementById('de-notice-bind-tweet');
  341. if (!iframeContent) {
  342. document.querySelector('body').appendChild(iframe)
  343. }
  344. }
  345. export function hideNoticeBindTweet() {
  346. hidePinTips();
  347. hidePopupPage();
  348. let iframeContent = document.getElementById('de-notice-bind-tweet');
  349. if (iframeContent) {
  350. document.querySelector('body').removeChild(iframeContent)
  351. }
  352. }
  353. /**
  354. * 点击deNet按钮处理
  355. * @private
  356. */
  357. function _deNetBtnClick() {
  358. getUserInfo((res) => {
  359. if (res) {
  360. if (window.location.pathname != '/home') {
  361. if (!dom.homeBtn) {
  362. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  363. }
  364. dom.homeBtn.click();
  365. }
  366. showGiveDialogHandler(res);
  367. } else {
  368. let loadIcon = document.getElementById('de-btn-loading');
  369. if (loadIcon) {
  370. return;
  371. }
  372. dom.deBtn.insertBefore(dom.loadingImg, dom.deBtn.querySelector('span'));
  373. setTimeout(() => {
  374. dom.loadingImg.style.transform = 'rotate(1080deg)'
  375. });
  376. setTimeout(() => {
  377. dom.loadingImg.style.transform = 'rotate(0deg)'
  378. dom.deBtn.innerHTML = '<span>DeNet<span>';
  379. }, 2000)
  380. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_LOGIN", data: '1' }, (res) => { console.log(res) })
  381. }
  382. })
  383. }
  384. /**
  385. * 设置发布内容
  386. * @param content
  387. * @private
  388. */
  389. let isSetContent = false;
  390. const _setPublishContent = throttle(function (content, time = 1000) {
  391. if (!isSetContent) {
  392. isSetContent = true;
  393. let inputEle = document.querySelector('div[contenteditable="true"]');
  394. if (inputEle) {
  395. inputEle.focus();
  396. }
  397. setTimeout(() => {
  398. document.execCommand("insertText", false, content);
  399. setTimeout(() => {
  400. isSetContent = false;
  401. }, 2000)
  402. }, time);
  403. }
  404. }, 800);
  405. /**
  406. * 创建deNet按钮 添加到页面
  407. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  408. * @private
  409. */
  410. function _createBtnDom() {
  411. let loadingImg = document.createElement('img');
  412. loadingImg.id = 'de-btn-loading'
  413. loadingImg.src = require("@/assets/img/icon-btn-loading.png");
  414. loadingImg.style.cssText = 'width:20px;height: 20px;margin-right:3px;transition-duration: 3s;';
  415. let style = document.createElement('style');
  416. style.innerHTML = "#de-btn:hover{opacity: .9;};@-webkit-keyframes load{from{ transform: rotate(0deg);} to{transform: rotate(360deg);}}";
  417. document.getElementsByTagName('head').item(0).appendChild(style);
  418. // 左侧大屏按钮
  419. let deBtn = document.createElement('span');
  420. // const shadowDiv = document.createElement('div');
  421. deBtn.innerHTML = '<span>DeNet</span>';
  422. deBtn.id = 'de-btn';
  423. deBtn.style.cssText = 'width:90%;height: 52px;text-align:center;line-height:52px;margin-bottom: 4px;margin-top: 4px;background: linear-gradient(274.8deg, #FF9900 -3.69%, #BD00FF 69.71%, #00F0FF 122.65%);color:#fff;font-size:17px;font-weight:700;border-radius:100px;cursor: pointer;display: flex;align-items: center;justify-content: center;';
  424. // 编辑框内按钮
  425. const deBtn1 = document.createElement('img');
  426. let src = require("@/assets/img/icon-gift-pack.png");
  427. const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;padding: 0px 8px';
  428. deBtn1.id = 'de-btn1';
  429. deBtn1.style.cssText = smallDeBtnStyle;
  430. deBtn1.src = src
  431. const deBtn2 = document.createElement('div');
  432. deBtn2.id = 'de-btn2';
  433. deBtn2.style.cssText = smallDeBtnStyle;
  434. deBtn2.src = "@/assets/img/icon-gift-pack.png"
  435. // 小屏按钮
  436. const deBtn3 = document.createElement('img');
  437. deBtn3.id = 'de-btn3'
  438. deBtn3.src = require("@/assets/logo/128.png");
  439. deBtn3.style.cssText = 'width:52px;height: 52px;margin-top:20px;cursor: pointer;';
  440. deBtn.addEventListener('click', () => {
  441. // chrome.runtime.sendMessage({
  442. // actionType: 'CONTENT_SET_BADGE',
  443. // data: {
  444. // text: '2'
  445. // }
  446. // }, res => {
  447. // console.log(res);
  448. // })
  449. Report.reportLog({
  450. pageSource: Report.pageSource.mainPage,
  451. businessType: Report.businessType.buttonClick,
  452. objectType: Report.objectType.buttonMain
  453. });
  454. _deNetBtnClick();
  455. })
  456. deBtn1.addEventListener('click', () => {
  457. Report.reportLog({
  458. pageSource: Report.pageSource.mainPage,
  459. businessType: Report.businessType.buttonClick,
  460. objectType: Report.objectType.buttonSecond
  461. });
  462. _deNetBtnClick();
  463. })
  464. deBtn2.addEventListener('click', () => {
  465. _deNetBtnClick();
  466. })
  467. deBtn3.addEventListener('click', () => {
  468. Report.reportLog({
  469. pageSource: Report.pageSource.mainPage,
  470. businessType: Report.businessType.buttonClick,
  471. objectType: Report.objectType.buttonMain
  472. });
  473. _deNetBtnClick();
  474. })
  475. dom.deBtn = deBtn;
  476. dom.deBtn1 = deBtn1;
  477. dom.deBtn2 = deBtn2;
  478. dom.deBtn3 = deBtn3;
  479. dom.loadingImg = loadingImg;
  480. }
  481. function addSliderNavDeBtn(isSmall = false) {
  482. if (!isSmall) {
  483. let bigDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  484. let deBtn = document.getElementById('de-btn');
  485. if (bigDom && !deBtn) {
  486. bigDom.appendChild(dom.deBtn);
  487. Report.reportLog({
  488. pageSource: Report.pageSource.mainPage,
  489. businessType: Report.businessType.buttonView,
  490. objectType: Report.objectType.buttonMain
  491. });
  492. }
  493. } else {
  494. let smallDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  495. let deBtn3 = document.getElementById('de-btn3');
  496. if (smallDom && !deBtn3) {
  497. smallDom.appendChild(dom.deBtn3);
  498. Report.reportLog({
  499. pageSource: Report.pageSource.mainPage,
  500. businessType: Report.businessType.buttonView,
  501. objectType: Report.objectType.buttonMain
  502. });
  503. }
  504. }
  505. }
  506. function onWindowResize() {
  507. window.onresize = throttle(function () {
  508. try {
  509. if (tweetPublishStore.showPublishDialog) {
  510. let dialog = document.querySelector('div[role="dialog"]');
  511. let dePublishTips = document.getElementById('de-publish-tips');
  512. if (dialog && !dePublishTips) {
  513. addPublishTipsIframe({ time: 0 });
  514. } else if (dialog && dePublishTips) {
  515. let dialogContent = dialog.querySelector('div[role=dialog]')
  516. if (dialogContent) {
  517. let right = dialogContent.offsetLeft - 15 - 266;
  518. dePublishTips.style.right = right + 'px';
  519. }
  520. }
  521. }
  522. if (window.innerWidth < 1273) {
  523. let bigBtn = document.querySelector('#de-btn');
  524. bigBtn && bigBtn.remove();
  525. setTimeout(() => {
  526. addSliderNavDeBtn(true);
  527. })
  528. } else {
  529. let smallBtn = document.querySelector('#de-btn3');
  530. smallBtn && smallBtn.remove();
  531. setTimeout(() => {
  532. addSliderNavDeBtn()
  533. })
  534. }
  535. } catch (e) {
  536. console.log(e)
  537. }
  538. }, 800)
  539. }
  540. function checkHasDeBtn() {
  541. try {
  542. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  543. let innerDeIcon = document.getElementById('de-btn1');
  544. if (toolBar && !innerDeIcon) {
  545. let dialogScheduleBtn = _getScheduleDom(false);
  546. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  547. }
  548. } catch (e) {
  549. console.log(e)
  550. }
  551. }
  552. /**
  553. * 点击发推,后端绑定推特id
  554. */
  555. function bindTwitterArtMethod() {
  556. if (!bindTwitterArt.postId) {
  557. return
  558. }
  559. if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
  560. bindTwitterArt.isBindIng = true;
  561. reportSrcPublishEvent({
  562. params: {
  563. postId: bindTwitterArt.postId,
  564. }
  565. }).then((res) => {
  566. if (res.code == 0) {
  567. Report.reportLog({
  568. objectType: Report.objectType.tweetPostBinded
  569. });
  570. bindTwitterArt.needBind = false;
  571. bindTwitterArt.postId = '';
  572. bindTwitterArt.isBindIng = false;
  573. }
  574. })
  575. }
  576. }
  577. import parseCard from './ParseCard'
  578. // 检测dom改变
  579. // 获取短链接
  580. // 查看本地是否有postid
  581. // 如果有 修改dom 返回
  582. // 如果没有 网络请求
  583. // 获取postid
  584. // 获取twitterid
  585. // 检测当前所有dom 如果没有
  586. let queue_num = 1
  587. export const changeQueueNum = (num = 0) => {
  588. queue_num = queue_num + num
  589. if (queue_num > 5) {
  590. queue_num = 5
  591. }
  592. }
  593. let main_observer = null
  594. function onChangePageMain(targetNode) {
  595. try {
  596. const config = { attributes: false, childList: true, subtree: true };
  597. const callback = (mutationsList, observer) => {
  598. changeQueueNum(1)
  599. }
  600. main_observer = new MutationObserver(callback);
  601. main_observer.observe(targetNode, config);
  602. } catch (error) {
  603. main_observer = null
  604. }
  605. }
  606. // 1.监听main改变
  607. // 2.监听卡片是否可见
  608. // 3.如果可见了 去找
  609. function setIframeRedPacket(type = 'twitter') {
  610. // 获取所有卡片参数
  611. let card_json_data
  612. switch (type) {
  613. case 'facebook':
  614. card_json_data = parseCard.parseFacebookCardParmas()
  615. for (let i in card_json_data) {
  616. parseCard.replaceFacebookPacket(card_json_data[i])
  617. }
  618. break;
  619. default:
  620. card_json_data = parseCard.parseAllDeNetCardParmas()
  621. // 过滤出可以请求的短链接
  622. parseCard.getCardParmas(card_json_data).then((res) => {
  623. for (let i in res.has_post_Id_card_data) {
  624. let item = res.has_post_Id_card_data[i];
  625. if (item && item.post_Id && item.post_Id.indexOf('nft/') >= 0) {
  626. parseCard.replaceNftDomRedPacket(item)
  627. } else {
  628. parseCard.replaceDOMRedPacket(item)
  629. }
  630. }
  631. if (res.need_net_short_url.length > 0) {
  632. // 请求短链接
  633. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, () => { })
  634. }
  635. })
  636. break;
  637. }
  638. }
  639. // 监听点击发推 按钮绑定事件
  640. // document.addEventListener('click', (e) => {
  641. // try {
  642. // if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
  643. // bindTwitterArtMethod()
  644. // } else if (e.target.closest('div[data-testid=tweetButton]')) {
  645. // bindTwitterArtMethod()
  646. // }
  647. // } catch (error) {
  648. // console.error('error', error)
  649. // }
  650. // })
  651. export function initExecuteScript(changes) {
  652. if (changes.executeScript) {
  653. let item = JSON.parse(changes.executeScript.newValue)
  654. if (item.executeScript) {
  655. init()
  656. }
  657. }
  658. }
  659. const createNFTIframe = ({ url, id }, callback) => {
  660. let iframe = document.createElement('iframe')
  661. iframe.id = id
  662. iframe.src = url
  663. iframe.style.cssText = 'border:medium none; width:100%; height:100%; z-index:100; position: fixed; top:0;left:0; display:none;';
  664. iframe.onload = () => {
  665. callback && callback()
  666. }
  667. document.body.appendChild(iframe);
  668. }
  669. function initParseCard() {
  670. let timer = setInterval(() => {
  671. let inTwitter = window.location.href.includes('twitter.com');
  672. let inTwitterNode = document.querySelector('main');
  673. let inFacebook = window.location.href.includes('facebook.com');
  674. let inFacebookNode = document.querySelector('#facebook');
  675. if (inTwitter && inTwitterNode) {
  676. clearInterval(timer)
  677. setInterval(() => {
  678. if (!main_observer) {
  679. onChangePageMain(inTwitterNode)
  680. changeQueueNum(1)
  681. }
  682. if (queue_num <= 0) {
  683. return
  684. }
  685. setIframeRedPacket()
  686. checkHasDeBtn()
  687. checkHasSliderDeBtn();
  688. changeQueueNum(-1)
  689. showNFTCard()
  690. }, 1000)
  691. } else if (inFacebook && inFacebookNode) {
  692. clearInterval(timer)
  693. setInterval(() => {
  694. if (!main_observer) {
  695. onChangePageMain(inFacebookNode)
  696. changeQueueNum(1)
  697. }
  698. if (queue_num <= 0) {
  699. return
  700. }
  701. setIframeRedPacket('facebook')
  702. changeQueueNum(-1)
  703. }, 1000)
  704. }
  705. }, 1000);
  706. }
  707. let inited = false
  708. // 初始化
  709. export function init() {
  710. if (inited) {
  711. return
  712. }
  713. inited = true
  714. console.log('init')
  715. getDiscordAuthCode();
  716. let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
  717. if (where) {
  718. return
  719. }
  720. twitterPinLogin();
  721. // 渲染dom
  722. initParseCard()
  723. showNFTCard()
  724. renderDom();
  725. checkTwitterTaskState();
  726. onBodyClick();
  727. initBuyNFT()
  728. getChromeStorage("popupShowPublishDialog", (res) => {
  729. console.log("popupShowPublishDialog", res);
  730. if (res && res.show) {
  731. setTimeout(() => {
  732. showTwitterPublishDialogHandler({
  733. srcContent: res.srcContent,
  734. postId: res.postId,
  735. copyContent: res.copyContent || ''
  736. });
  737. }, 1500);
  738. chrome.storage.local.remove("popupShowPublishDialog");
  739. }
  740. });
  741. getChromeStorage("userSettings", (res) => {
  742. setTimeout(() => {
  743. addPinedPop();
  744. if (res && !res.isOnToolbar) {
  745. setTimeout(() => {
  746. showPinTips();
  747. chrome.storage.local.remove("userSettings");
  748. }, 800);
  749. }
  750. }, 800);
  751. });
  752. }
  753. function checkHasSliderDeBtn() {
  754. let deBtn = document.getElementById('de-btn');
  755. let deBtn3 = document.getElementById('de-btn3');
  756. if (!deBtn && !deBtn3) {
  757. addSliderNavDeBtn();
  758. }
  759. }
  760. export function facebookReplyTweet(params) {
  761. if (window.location.origin.indexOf('twitter.com')) {
  762. const urlParams = new URLSearchParams(window.location.search);
  763. const actionType = urlParams.get('actionType');
  764. let deReplyParams = urlParams.get('deReplyParams') || '{}';
  765. deReplyParams = JSON.parse(deReplyParams);
  766. if (actionType == 'denetFacebookToTwitterReply') {
  767. if (params.postId == deReplyParams.postId) {
  768. let iframe = document.getElementById(params.postId);
  769. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_FACEBOOK_REPLY', data: deReplyParams }, '*');
  770. }
  771. }
  772. }
  773. }
  774. export function replyHandle(params) {
  775. let iframe = window.parent.document.getElementById(params.postId);
  776. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  777. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  778. if (replyBtn) {
  779. replyBtn.click();
  780. }
  781. onReplyDialogOpen(params, iframe);
  782. // 详情页推文底部评论处理
  783. let pathNameArr = window.location.pathname.split('/');
  784. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  785. let tweetReply = document.querySelector('div[data-testid="tweetButtonInline"]');
  786. if (tweetReply) {
  787. tweetReply.addEventListener('click', function () {
  788. let eleList = tweetReply.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll('span[data-text="true"]');
  789. reportReplyResult(eleList, params, () => {
  790. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  791. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  792. });
  793. })
  794. }
  795. }
  796. }
  797. export function onTweetReplyClick(params) {
  798. let iframe = window.parent.document.getElementById(params.postId);
  799. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  800. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  801. if (replyBtn) {
  802. replyBtn.addEventListener('click', () => {
  803. onReplyDialogOpen(params, iframe);
  804. })
  805. }
  806. }
  807. function onReplyDialogOpen(params, iframe) {
  808. setTimeout(() => {
  809. let dialog = document.querySelector('div[role="dialog"]');
  810. let replyBtn;
  811. if (dialog) {
  812. let dialogContent = dialog.querySelector('div[role="dialog"]');
  813. replyBtn = dialog.querySelector('div[data-testid="toolBar"]').querySelector('div[data-testid="tweetButton"]');
  814. if (dialogContent) {
  815. let width = dialogContent.offsetWidth;
  816. let ele = document.createElement('div');
  817. ele.innerText = 'Tag 3 friends to complete the task';
  818. ele.style.cssText = `width: ${width}px; height: 38px; color: #fff; font-weight: 600;
  819. font-size: 16px; display: flex; align-items: center; justify-content: center; background: #1D9BF0;
  820. opacity: 0.8; position: absolute; top: 18px; left: 50%; transform: translateX(-50%); z-index: 1000`;
  821. dialogContent.style.top = '80px';
  822. dialogContent.parentNode.appendChild(ele);
  823. }
  824. } else {
  825. dialog = document.querySelector('main[role="main"]');
  826. if (dialog) {
  827. replyBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  828. }
  829. }
  830. if (dialog && replyBtn) {
  831. replyBtn.addEventListener('click', function () {
  832. let eleList = dialog.querySelector('div[contenteditable="true"]').querySelectorAll('span[data-text="true"]');
  833. reportReplyResult(eleList, params, () => {
  834. // 上報完成
  835. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  836. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  837. })
  838. });
  839. }
  840. }, 1000);
  841. }
  842. const reportReplyResult = throttle(function (eleList, params, cb) {
  843. if (eleList && eleList.length) {
  844. let atList = [];
  845. for (let i = 0; i < eleList.length; i++) {
  846. let item = eleList[i];
  847. // 是否有中文
  848. let reg = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
  849. if (item && item.innerText.startsWith('@') && !reg.test(item.innerText)) {
  850. atList.push(item.innerText);
  851. }
  852. }
  853. atList = Array.from(new Set(atList));
  854. if (atList.length >= 3) {
  855. fetchAddFinishEvent({
  856. eventType: params.type,
  857. luckdropId: params.taskLuckdropId
  858. }).then(res => {
  859. if (res.code == 0) {
  860. cb && cb();
  861. }
  862. })
  863. }
  864. }
  865. }, 800);
  866. // 根据推特id找到dom,完成任务
  867. export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_type = 'like') {
  868. // 1.根据推特ID寻找推文,获取卡片
  869. // 获取所有卡片参数
  870. let card_json_data = parseCard.parseAllDeNetCardParmas()
  871. let result = card_json_data.filter((item) => { return item.tweet_Id == tweet_Id }) || []
  872. if (result.length < 1) {
  873. return
  874. }
  875. result = result[0]
  876. switch (task_type) {
  877. case 'like':
  878. // https://twitter.com/intent/retweet?tweet_id=1525900221628223491
  879. if (result.dom_card) {
  880. result.dom_card.querySelector('div[data-testid=like]').click()
  881. result.dom_card.querySelector('iframe').contentWindow.postMessage({ actionType: 'CONTENT_DONE_TASK', task_type, }, '*');
  882. }
  883. break;
  884. case 'retweet':
  885. if (result.dom_card) {
  886. result.dom_card.querySelector('div[data-testid=retweet]').click()
  887. result.dom_card.querySelector('div[data-testid=retweetConfirm]').click()
  888. }
  889. break
  890. case 'follow':
  891. follow_names.forEach((item) => {
  892. window.open(`https://twitter.com/intent/follow?screen_name=${item}&tweet_Id=${tweet_Id}`)
  893. })
  894. break
  895. }
  896. }
  897. function clickByDataTestId(e, id, callback) {
  898. if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
  899. callback()
  900. } else if (e.target.closest('div[data-testid=' + id + ']')) {
  901. callback()
  902. }
  903. }
  904. // 校验关注推特状态
  905. export function checkTwitterTaskState() {
  906. let task_type = ''
  907. let url = window.location.href
  908. let tweet_Id
  909. let task_data = {
  910. follow_name: ''
  911. }
  912. // 校验当前链接
  913. if (url.includes('https://twitter.com/intent/retweet')) {
  914. task_type = 'retweet'
  915. } else if (url.includes('https://twitter.com/intent/follow')) {
  916. task_type = 'follow'
  917. task_data.follow_name = getQueryString('screen_name')
  918. } else if (url.includes('https://twitter.com/intent/like')) {
  919. task_type = 'like'
  920. } else {
  921. return
  922. }
  923. tweet_Id = getQueryString('tweet_id')
  924. // let root_status
  925. document.body.addEventListener('click', (e) => {
  926. // 点击 确认
  927. clickByDataTestId(e, 'confirmationSheetConfirm', () => {
  928. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  929. })
  930. // 点击取消
  931. clickByDataTestId(e, 'confirmationSheetCancel', () => {
  932. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  933. })
  934. // 点击 蒙层
  935. if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
  936. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  937. }
  938. }, true)
  939. let timer = setInterval(() => {
  940. // 喜欢
  941. if (document.querySelector('div[data-testid=unlike]') && task_type == 'like') {
  942. clearInterval(timer)
  943. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  944. }
  945. // 转推
  946. if (document.querySelector('div[data-testid=unretweet]') && task_type == 'retweet') {
  947. clearInterval(timer)
  948. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  949. }
  950. // 关注
  951. if (task_type == 'follow') {
  952. let follow_area = document.querySelector('div[data-testid=placementTracking]')
  953. if (follow_area && follow_area.querySelectorAll('div')) {
  954. follow_area = follow_area.querySelectorAll('div')
  955. clearInterval(timer)
  956. for (let i in follow_area) {
  957. if (follow_area[i] && follow_area[i].dataset && follow_area[i].dataset.testid && follow_area[i].dataset.testid.indexOf('unfollow') > 0) {
  958. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  959. break
  960. }
  961. }
  962. }
  963. }
  964. }, 1000)
  965. }
  966. export function getTweetAuthorByDom(params) {
  967. let iframe = document.getElementById(params.postId);
  968. if (!iframe) {
  969. return;
  970. }
  971. let fullNameDom;
  972. let pathNameArr = window.location.pathname.split('/');
  973. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  974. fullNameDom = iframe.parentNode.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  975. } else {
  976. fullNameDom = iframe.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  977. }
  978. if (fullNameDom) {
  979. let arr = fullNameDom.href.split('/');
  980. if (arr.length) {
  981. let fullName = arr[arr.length - 1];
  982. if (fullName) {
  983. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_GET_TWEET_AUTHOR', data: { fullName } }, '*');
  984. }
  985. }
  986. }
  987. }
  988. export function doTaskTwitterAPI({ task_data, task_type }) {
  989. switch (task_type) {
  990. case 'like':
  991. TwitterLikeAPI(task_data.tweet_Id)
  992. break
  993. case 'retweet':
  994. TwitterRetweetAPI(task_data.tweet_Id)
  995. break
  996. case 'follow':
  997. task_data.follow_data.forEach((item) => {
  998. if (item.name && item.twitterUserId) {
  999. TwitterFollowAPI(item, task_data.tweet_Id)
  1000. }
  1001. })
  1002. break
  1003. }
  1004. }
  1005. const TwitterFollowAPI = (item, tweet_Id) => {
  1006. fetch("https://twitter.com/i/api/1.1/friendships/create.json", {
  1007. "headers": {
  1008. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1009. "content-type": "application/x-www-form-urlencoded",
  1010. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1011. "sec-ch-ua-mobile": "?0",
  1012. "sec-ch-ua-platform": "\"macOS\"",
  1013. "x-csrf-token": getCookie('ct0'),
  1014. "x-twitter-active-user": "yes",
  1015. "x-twitter-auth-type": "OAuth2Session",
  1016. "x-twitter-client-language": "zh-cn"
  1017. },
  1018. "referrer": "https://twitter.com/home",
  1019. "referrerPolicy": "strict-origin-when-cross-origin",
  1020. "body": "include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&include_ext_has_nft_avatar=1&skip_status=1&user_id=" + item.twitterUserId + "",
  1021. "method": "POST",
  1022. "mode": "cors",
  1023. "credentials": "include"
  1024. }).then(() => {
  1025. let task_data = {
  1026. follow_name: item.name
  1027. }
  1028. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: true }, () => { })
  1029. }).catch(() => {
  1030. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: false }, () => { })
  1031. })
  1032. }
  1033. const TwitterRetweetAPI = (tweet_Id) => {
  1034. fetch("https://twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet", {
  1035. "headers": {
  1036. "accept": "*/*",
  1037. "accept-language": "zh,en;q=0.9,zh-CN;q=0.8",
  1038. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1039. "content-type": "application/json",
  1040. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1041. "sec-ch-ua-mobile": "?0",
  1042. "sec-ch-ua-platform": "\"macOS\"",
  1043. "sec-fetch-dest": "empty",
  1044. "sec-fetch-mode": "cors",
  1045. "sec-fetch-site": "same-origin",
  1046. "x-csrf-token": getCookie('ct0'),
  1047. "x-twitter-active-user": "yes",
  1048. "x-twitter-auth-type": "OAuth2Session",
  1049. "x-twitter-client-language": "zh-cn"
  1050. },
  1051. "referrer": "https://twitter.com/home",
  1052. "referrerPolicy": "strict-origin-when-cross-origin",
  1053. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\",\"dark_request\":false},\"queryId\":\"ojPdsZsimiJrUGLR1sjUtA\"}",
  1054. "method": "POST",
  1055. "mode": "cors",
  1056. "credentials": "include"
  1057. }).then(() => {
  1058. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: true }, () => { })
  1059. }).catch(() => {
  1060. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: false }, () => { })
  1061. })
  1062. }
  1063. const TwitterLikeAPI = (tweet_Id) => {
  1064. fetch("https://twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet", {
  1065. "headers": {
  1066. "accept": "*/*",
  1067. "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
  1068. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1069. "content-type": "application/json",
  1070. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
  1071. "sec-ch-ua-mobile": "?0",
  1072. "sec-ch-ua-platform": "\"Windows\"",
  1073. "sec-fetch-dest": "empty",
  1074. "sec-fetch-mode": "cors",
  1075. "sec-fetch-site": "same-origin",
  1076. "x-csrf-token": getCookie('ct0'),
  1077. "x-twitter-active-user": "yes",
  1078. "x-twitter-auth-type": "OAuth2Session",
  1079. "x-twitter-client-language": "en"
  1080. },
  1081. "referrer": "https://twitter.com/home",
  1082. "referrerPolicy": "strict-origin-when-cross-origin",
  1083. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\"},\"queryId\":\"lI07N6Otwv1PhnEgXILM7A\"}",
  1084. "method": "POST",
  1085. "mode": "cors",
  1086. "credentials": "include"
  1087. }).then(() => {
  1088. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: true }, () => { })
  1089. }).catch(() => {
  1090. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: false }, () => { })
  1091. })
  1092. }
  1093. export const hideBuyNFT = () => {
  1094. let iframe = document.querySelector('#nftProjectId')
  1095. iframe.style.display = 'none'
  1096. }
  1097. export const showBuyNFT = ({ nft_project_Id }) => {
  1098. if (!nft_project_Id) {
  1099. return
  1100. }
  1101. let iframe = document.querySelector('#nftProjectId')
  1102. if(!iframe.src.includes(nft_project_Id)){
  1103. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html?nftProjectId=${nft_project_Id}`)
  1104. }
  1105. iframe.style.display = 'block'
  1106. }
  1107. const initBuyNFT = () => {
  1108. let url = chrome.runtime.getURL(`/iframe/buy-nft.html`)
  1109. let id = `nftProjectId`
  1110. createNFTIframe({ url, id })
  1111. }
  1112. export const showNFTCard = () => {
  1113. let urlInfo = new URL(window.location.href)
  1114. let isTwitter = urlInfo.hostname === 'twitter.com'
  1115. let userElem = document.querySelector('div[data-testid="UserName"]');
  1116. let sideElem = document.querySelector('div[data-testid="sidebarColumn"]')
  1117. let tabIndex = sideElem && sideElem.querySelector('div[tabindex="0"]');
  1118. let isAppend = document.querySelector('div[id="de-nft-node"]');
  1119. let where = isTwitter && userElem && tabIndex;
  1120. if (where) {
  1121. let iframe = document.createElement('iframe');
  1122. iframe.src = chrome.runtime.getURL(`/iframe/nft-card.html`)
  1123. iframe.style.cssText = 'border:medium none; width:100%; height:297px;';
  1124. let nftElement = document.createElement('div');
  1125. nftElement.id = 'de-nft-node';
  1126. nftElement.innerHTML = `
  1127. ${iframe.outerHTML}
  1128. <style>
  1129. #de-nft-node {height:297px; margin-bottom:17px; display:none;}
  1130. </style>
  1131. `;
  1132. if (tabIndex && tabIndex.firstChild && tabIndex.firstChild.childNodes && !isAppend) {
  1133. tabIndex.firstChild.insertBefore(nftElement, tabIndex.firstChild.childNodes[2]);
  1134. }
  1135. }
  1136. }
  1137. export const showNFTSale = () => {
  1138. document.querySelector('div[id="de-nft-node"]').style.display = 'block';
  1139. }
  1140. export const appendPopupPage = (params) => {
  1141. let { path = '' } = params;
  1142. let iframe = document.createElement('iframe');
  1143. iframe.id = 'de-popup-page';
  1144. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1145. iframe.style.cssText = `border: medium none; width: 375px;
  1146. height: 600px;position: fixed; right: 16px; top: 16px;background: #FFFFFF;border: 0.5px solid #919191;box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.2);box-sizing: border-box;z-index: 1000;`
  1147. let iframeContent = document.getElementById('de-extension-popup');
  1148. if (!iframeContent) {
  1149. document.querySelector('body').appendChild(iframe);
  1150. }
  1151. }
  1152. export const showPopupPage = (params) => {
  1153. let { path = '' } = params;
  1154. appendPopupPage({path});
  1155. }
  1156. export const hidePopupPage = () => {
  1157. hidePinTips();
  1158. hideNoticeBindTweet();
  1159. let iframe = document.getElementById('de-popup-page');
  1160. if (iframe) {
  1161. document.querySelector('body').removeChild(iframe);
  1162. }
  1163. }
  1164. const onBodyClick = () => {
  1165. document.querySelector('body').addEventListener('click', function() {
  1166. hidePopupPage();
  1167. })
  1168. }