twitter.js 47 KB

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