twitter.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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. import { showNFTGroupIcon, hideNFTGroupList, checkUserJoinGroup, elemAddEventListener, addJoinedGroupList } from '@/logic/content/nft';
  8. import { getTwitterNftGroupInfo } from '@/http/nft'
  9. let dom = {};
  10. let tweetAccountBindGroupInfo = {
  11. isBind: false,
  12. groupInfo: null,
  13. isInit: false
  14. }
  15. function twitterPinLogin() {
  16. if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
  17. let code = document.querySelector('code')
  18. if (code) {
  19. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_CODE", code: code.innerText }, () => { })
  20. // port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
  21. }
  22. }
  23. }
  24. function getDiscordAuthCode() {
  25. if (window.location.href.indexOf(discordAuthRedirectUri) > -1) {
  26. const urlParams = new URLSearchParams(window.location.search);
  27. const code = urlParams.get('code');
  28. if (code) {
  29. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_DISCORD_AUTH_CODE", code }, () => { })
  30. }
  31. }
  32. };
  33. /**
  34. * 渲染要插入的dom,初始化逻辑
  35. * @param port
  36. */
  37. function renderDom() {
  38. if (window.location.href.indexOf('https://twitter.com') > -1) {
  39. _createBtnDom();
  40. onWindowResize();
  41. checkHasDeBtn();
  42. setTimeout(() => {
  43. _addIframe();
  44. _addDeNetBtn();
  45. _getSliderTwitterBtn();
  46. }, 800)
  47. }
  48. }
  49. /**
  50. * 展示give弹窗
  51. */
  52. export function showGiveDialogHandler(userInfo) {
  53. let iframe = document.getElementById('iframe-content');
  54. if (iframe) {
  55. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  56. } else {
  57. _addIframe();
  58. let iframe = document.getElementById('iframe-content');
  59. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  60. }
  61. }
  62. export function showIframeHandler() {
  63. document.getElementById('iframe-content').style.display = 'block';
  64. }
  65. export function hideIframeHandler() {
  66. document.getElementById('iframe-content').style.display = 'none';
  67. }
  68. /**
  69. * 展示twitter原生发布框
  70. */
  71. let tweetPublishStore = {
  72. showPublishDialog: false
  73. }
  74. export function showTwitterPublishDialogHandler(publishRes) {
  75. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  76. if (bigBtn) {
  77. bigBtn.click();
  78. tweetPublishStore.showPublishDialog = true;
  79. } else {
  80. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  81. smallBtn && smallBtn.click();
  82. tweetPublishStore.showPublishDialog = true;
  83. }
  84. setChromeStorage({ publishData: JSON.stringify(publishRes) })
  85. addPublishTipsIframe({
  86. srcContent: publishRes.copyContent
  87. })
  88. _setPublishContent(publishRes.srcContent);
  89. _publishTweetEvent(publishRes, bindTwitterArtMethod);
  90. }
  91. export function twitterPublishHandler(res) {
  92. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  93. if (bigBtn) {
  94. bigBtn.click();
  95. } else {
  96. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  97. smallBtn && smallBtn.click();
  98. }
  99. nextTick(() => {
  100. document.execCommand('selectAll');
  101. }, 100).then(() => {
  102. _setPublishContent(res.srcContent, 500);
  103. })
  104. }
  105. export function showPinTips() {
  106. hidePopupPage();
  107. hideNoticeBindTweet();
  108. getChromeStorage('pinData', (res) => {
  109. if (!res || res.show) {
  110. let domPop = document.getElementById('de-pin-pop');
  111. domPop.style.display = 'block';
  112. }
  113. })
  114. }
  115. export function hidePinTips() {
  116. let pop = document.querySelector('#de-pin-pop');
  117. if (pop) {
  118. pop.style.display = 'none';
  119. }
  120. }
  121. function addPinedPop() {
  122. let domPop = document.getElementById('de-pin-pop');
  123. if (domPop) {
  124. return;
  125. }
  126. let popWrapper = document.createElement('div');
  127. 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';
  128. popWrapper.id = 'de-pin-pop'
  129. let img = document.createElement('img');
  130. img.src = require("@/assets/img/img-pined-guide.png");
  131. img.width = 253;
  132. let contentDom = document.createElement('div');
  133. 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>"
  134. popWrapper.appendChild(img);
  135. popWrapper.appendChild(contentDom);
  136. document.querySelector('body').appendChild(popWrapper);
  137. let deCheck = document.querySelector('#de-check');
  138. let deRemind = document.querySelector('#de-remind');
  139. deCheck.onclick = function (e) {
  140. e && e.stopPropagation && e.stopPropagation();
  141. setChromeStorage({ pinData: JSON.stringify({ show: !this.checked }) })
  142. }
  143. deRemind.onclick = function () {
  144. deCheck.checked = !deCheck.checked;
  145. setChromeStorage({ pinData: JSON.stringify({ show: !deCheck.checked }) })
  146. }
  147. document.querySelector('.de-pin-skip').onclick = function () {
  148. document.querySelector('#de-pin-pop').style.display = 'none';
  149. }
  150. }
  151. function getUserInfo(cb) {
  152. getChromeStorage('userInfo', (res) => {
  153. cb && cb(res);
  154. })
  155. }
  156. // 绑定推文id所需参数
  157. let bindTwitterArt = {
  158. needBind: false,
  159. postId: '',
  160. isBindIng: false
  161. };
  162. /**
  163. * 监听dialog内点击原生发布按钮事件
  164. * @private
  165. */
  166. function _publishTweetEvent(params, cb) {
  167. setTimeout(() => {
  168. let publishTweetBtn;
  169. let dialog = document.querySelector('div[role="dialog"]');
  170. if (dialog) {
  171. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  172. onClosePublishDialogHandle(dialog, params)
  173. } else {
  174. let domMain = document.querySelector('main[role="main"]');
  175. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  176. }
  177. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  178. bindTwitterArt.needBind = true;
  179. bindTwitterArt.postId = params.postId;
  180. tweetPublishStore.showPublishDialog = false;
  181. // checkIsShowReSend(dialog, params);
  182. cb && cb()
  183. });
  184. }, 800)
  185. }
  186. function onClosePublishDialogHandle(dom, params) {
  187. dom.querySelector('div[role="group"]').addEventListener('click', function () {
  188. setTimeout(() => {
  189. let parent = document.querySelector('div[data-testid="confirmationSheetDialog"]');
  190. if (parent) {
  191. let btnArr = parent.querySelectorAll('div[role=button]')
  192. for (let i = 0; i < btnArr.length; i++) {
  193. let btn = btnArr[i];
  194. btn.addEventListener('click', function () {
  195. tweetPublishStore.showPublishDialog = false;
  196. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  197. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  198. })
  199. }
  200. } else {
  201. setTimeout(() => {
  202. let dialog = document.querySelector('div[role="dialog"]');
  203. if (!dialog) {
  204. tweetPublishStore.showPublishDialog = false;
  205. }
  206. }, 800)
  207. }
  208. }, 1000)
  209. })
  210. }
  211. function checkIsShowReSend(dom, params) {
  212. let str = dom.querySelector('div[data-contents="true"]').innerHTML;
  213. if (str.indexOf(params.postId) < 0) {
  214. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  215. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  216. }
  217. }
  218. /**
  219. * 在输入推文区插入deNet按钮
  220. * @param parent
  221. * @param dom
  222. * @param isClick
  223. * @private
  224. */
  225. function _addDeNetEditBtn(parent, dom, isClick = false) {
  226. setTimeout(() => {
  227. if (parent && parent.parentNode) {
  228. Report.reportLog({
  229. pageSource: Report.pageSource.mainPage,
  230. businessType: Report.businessType.buttonView,
  231. objectType: Report.objectType.buttonSecond
  232. });
  233. let innerDeIcon = document.getElementById('de-btn1');
  234. if (!innerDeIcon) {
  235. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  236. }
  237. } else {
  238. setTimeout(() => {
  239. parent = _getScheduleDom(isClick);
  240. if (parent && parent.parentNode) {
  241. Report.reportLog({
  242. pageSource: Report.pageSource.mainPage,
  243. businessType: Report.businessType.buttonView,
  244. objectType: Report.objectType.buttonSecond
  245. });
  246. let innerDeIcon = document.getElementById('de-btn1');
  247. if (!innerDeIcon) {
  248. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  249. }
  250. }
  251. }, 1000)
  252. }
  253. })
  254. }
  255. /**
  256. * 在dialog插入deNet按钮
  257. * @private
  258. */
  259. // function _addDeNetBtnToDialog() {
  260. // setTimeout(() => {
  261. // let dialogScheduleBtn = _getScheduleDom(true);
  262. // _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  263. // }, 800)
  264. // }
  265. /**
  266. * 获取左侧twitter按钮
  267. * @private
  268. */
  269. function _getSliderTwitterBtn() {
  270. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  271. dom.tweetBtn.addEventListener('click', function () {
  272. // _addDeNetBtnToDialog();
  273. })
  274. }
  275. /**
  276. * 添加deNet按钮
  277. * @private
  278. */
  279. function _addDeNetBtn() {
  280. setTimeout(() => {
  281. let navWidth = document.querySelector('nav[role="navigation"]').offsetWidth;
  282. addSliderNavDeBtn(navWidth < 245);
  283. let innerDeIcon = document.getElementById('de-btn1');
  284. if (!innerDeIcon) {
  285. let dialogScheduleBtn = _getScheduleDom(false);
  286. dom && dom.deBtn1 && _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  287. }
  288. }, 800)
  289. }
  290. /**
  291. * 获取推文输入框内dom,用于插入deNet
  292. * @param isDialogInner
  293. * @returns {Element}
  294. * @private
  295. */
  296. function _getScheduleDom(isDialogInner = false) {
  297. let scheduleBtn;
  298. if (isDialogInner) {
  299. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  300. } else {
  301. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  302. if (toolBar) {
  303. scheduleBtn = toolBar.querySelector('div[data-testid="geoButton"]');
  304. }
  305. }
  306. return scheduleBtn;
  307. }
  308. /**
  309. * 插入iframe到页面
  310. * @private
  311. */
  312. function _addIframe() {
  313. // let span = document.createElement('span');
  314. // const shadowRoot = span.attachShadow({mode: 'closed'})
  315. let iframe = document.createElement('iframe');
  316. iframe.src = chrome.runtime.getURL('/iframe/publish.html')
  317. iframe.id = 'iframe-content'
  318. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:10000; border: medium none;display:none';
  319. // shadowRoot.appendChild(iframe);
  320. // document.body.appendChild(span)
  321. dom.iframe = iframe;
  322. let iframeContent = document.getElementById('iframe-content');
  323. if (!iframeContent) {
  324. document.querySelector('body').appendChild(iframe);
  325. }
  326. }
  327. function addPublishTipsIframe(params = {}) {
  328. let { time = 1000 } = params;
  329. setTimeout(() => {
  330. let dialog = document.querySelector('div[role="dialog"]').querySelector('div[role="dialog"]')
  331. if (dialog) {
  332. let right = dialog.offsetLeft - 15 - 266;
  333. let iframe = document.createElement('iframe');
  334. iframe.id = 'de-publish-tips'
  335. if (params.type == 'nft') {
  336. iframe.src = chrome.runtime.getURL('/iframe/publish-tips.html?type="nft"');
  337. } else {
  338. iframe.src = chrome.runtime.getURL('/iframe/publish-tips.html');
  339. }
  340. iframe.style.cssText = `border: medium none; width:270px;height:500px;position: fixed; right: ${right}px; top: 5%;z-index: -1`
  341. let iframeContent = document.getElementById('de-publish-tips');
  342. if (!iframeContent) {
  343. dialog.appendChild(iframe)
  344. }
  345. }
  346. }, time)
  347. }
  348. export function noticeBindTweet(params) {
  349. hidePinTips();
  350. hidePopupPage();
  351. let iframe = document.createElement('iframe');
  352. iframe.id = 'de-notice-bind-tweet';
  353. iframe.src = chrome.runtime.getURL('/iframe/bind-tweet.html') + `?params=${JSON.stringify(params)}`;
  354. iframe.style.cssText = `border: medium none; width:400px;min-height:313px;position: fixed; right: 16px; top: 16px;`
  355. let iframeContent = document.getElementById('de-notice-bind-tweet');
  356. if (!iframeContent) {
  357. document.querySelector('body').appendChild(iframe)
  358. }
  359. }
  360. export function hideNoticeBindTweet() {
  361. let iframeContent = document.getElementById('de-notice-bind-tweet');
  362. if (iframeContent) {
  363. document.querySelector('body').removeChild(iframeContent)
  364. }
  365. }
  366. /**
  367. * 点击deNet按钮处理
  368. * @private
  369. */
  370. function _deNetBtnClick() {
  371. getUserInfo((res) => {
  372. if (res) {
  373. if (window.location.pathname != '/home') {
  374. if (!dom.homeBtn) {
  375. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  376. }
  377. dom.homeBtn.click();
  378. }
  379. showGiveDialogHandler(res);
  380. } else {
  381. let loadIcon = document.getElementById('de-btn-loading');
  382. if (loadIcon) {
  383. return;
  384. }
  385. dom && dom.deBtn && dom.deBtn.insertBefore(dom.loadingImg, dom.deBtn.querySelector('span'));
  386. setTimeout(() => {
  387. dom.loadingImg.style.transform = 'rotate(1080deg)'
  388. });
  389. setTimeout(() => {
  390. dom.loadingImg.style.transform = 'rotate(0deg)'
  391. dom.deBtn.innerHTML = '<span>DeNet<span>';
  392. }, 2000)
  393. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_LOGIN", data: '1' }, (res) => { console.log(res) })
  394. }
  395. })
  396. }
  397. /**
  398. * 设置发布内容
  399. * @param content
  400. * @private
  401. */
  402. let isSetContent = false;
  403. export const _setPublishContent = throttle(function (content, time = 1000) {
  404. if (!isSetContent) {
  405. isSetContent = true;
  406. let inputEle = document.querySelector('div[contenteditable="true"]');
  407. if (inputEle) {
  408. inputEle.focus();
  409. }
  410. setTimeout(() => {
  411. document.execCommand("insertText", false, content);
  412. setTimeout(() => {
  413. isSetContent = false;
  414. }, 2000)
  415. }, time);
  416. }
  417. }, 800);
  418. /**
  419. * 创建deNet按钮 添加到页面
  420. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  421. * @private
  422. */
  423. function _createBtnDom() {
  424. let loadingImg = document.createElement('img');
  425. loadingImg.id = 'de-btn-loading'
  426. loadingImg.src = require("@/assets/img/icon-btn-loading.png");
  427. loadingImg.style.cssText = 'width:20px;height: 20px;margin-right:3px;transition-duration: 3s;';
  428. let style = document.createElement('style');
  429. style.innerHTML = "#de-btn:hover{opacity: .9;};@-webkit-keyframes load{from{ transform: rotate(0deg);} to{transform: rotate(360deg);}}";
  430. document.getElementsByTagName('head').item(0).appendChild(style);
  431. // 左侧大屏按钮
  432. let deBtn = document.createElement('span');
  433. // const shadowDiv = document.createElement('div');
  434. deBtn.innerHTML = '<span>DeNet</span>';
  435. deBtn.id = 'de-btn';
  436. 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;';
  437. // 编辑框内按钮
  438. const deBtn1 = document.createElement('img');
  439. let src = require("@/assets/img/icon-gift-pack.png");
  440. const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;padding: 0px 8px';
  441. deBtn1.id = 'de-btn1';
  442. deBtn1.style.cssText = smallDeBtnStyle;
  443. deBtn1.src = src
  444. const deBtn2 = document.createElement('div');
  445. deBtn2.id = 'de-btn2';
  446. deBtn2.style.cssText = smallDeBtnStyle;
  447. deBtn2.src = "@/assets/img/icon-gift-pack.png"
  448. // 小屏按钮
  449. const deBtn3 = document.createElement('img');
  450. deBtn3.id = 'de-btn3'
  451. deBtn3.src = require("@/assets/logo/128.png");
  452. deBtn3.style.cssText = 'width:52px;height: 52px;margin-top:20px;cursor: pointer;';
  453. deBtn.addEventListener('click', () => {
  454. // chrome.runtime.sendMessage({
  455. // actionType: 'CONTENT_SET_BADGE',
  456. // data: {
  457. // text: '2'
  458. // }
  459. // }, res => {
  460. // console.log(res);
  461. // })
  462. Report.reportLog({
  463. pageSource: Report.pageSource.mainPage,
  464. businessType: Report.businessType.buttonClick,
  465. objectType: Report.objectType.buttonMain
  466. });
  467. _deNetBtnClick();
  468. })
  469. deBtn1.addEventListener('click', () => {
  470. Report.reportLog({
  471. pageSource: Report.pageSource.mainPage,
  472. businessType: Report.businessType.buttonClick,
  473. objectType: Report.objectType.buttonSecond
  474. });
  475. _deNetBtnClick();
  476. })
  477. deBtn2.addEventListener('click', () => {
  478. _deNetBtnClick();
  479. })
  480. deBtn3.addEventListener('click', () => {
  481. Report.reportLog({
  482. pageSource: Report.pageSource.mainPage,
  483. businessType: Report.businessType.buttonClick,
  484. objectType: Report.objectType.buttonMain
  485. });
  486. _deNetBtnClick();
  487. })
  488. dom.deBtn = deBtn;
  489. dom.deBtn1 = deBtn1;
  490. dom.deBtn2 = deBtn2;
  491. dom.deBtn3 = deBtn3;
  492. dom.loadingImg = loadingImg;
  493. }
  494. function addSliderNavDeBtn(isSmall = false) {
  495. if (!isSmall) {
  496. let bigDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  497. let deBtn = document.getElementById('de-btn');
  498. if (bigDom && !deBtn) {
  499. dom && dom.deBtn && bigDom.appendChild(dom.deBtn);
  500. Report.reportLog({
  501. pageSource: Report.pageSource.mainPage,
  502. businessType: Report.businessType.buttonView,
  503. objectType: Report.objectType.buttonMain
  504. });
  505. }
  506. } else {
  507. let smallDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  508. let deBtn3 = document.getElementById('de-btn3');
  509. if (smallDom && !deBtn3) {
  510. dom && dom.deBtn3 && smallDom.appendChild(dom.deBtn3);
  511. Report.reportLog({
  512. pageSource: Report.pageSource.mainPage,
  513. businessType: Report.businessType.buttonView,
  514. objectType: Report.objectType.buttonMain
  515. });
  516. }
  517. }
  518. }
  519. function onWindowResize() {
  520. window.onresize = throttle(function () {
  521. setTabGroupIframeStyle();
  522. try {
  523. if (tweetPublishStore.showPublishDialog) {
  524. let dialog = document.querySelector('div[role="dialog"]');
  525. let dePublishTips = document.getElementById('de-publish-tips');
  526. if (dialog && !dePublishTips) {
  527. addPublishTipsIframe({ time: 0 });
  528. } else if (dialog && dePublishTips) {
  529. let dialogContent = dialog.querySelector('div[role=dialog]')
  530. if (dialogContent) {
  531. let right = dialogContent.offsetLeft - 15 - 266;
  532. dePublishTips.style.right = right + 'px';
  533. }
  534. }
  535. }
  536. if (window.innerWidth < 1273) {
  537. let bigBtn = document.querySelector('#de-btn');
  538. bigBtn && bigBtn.remove();
  539. setTimeout(() => {
  540. addSliderNavDeBtn(true);
  541. })
  542. } else {
  543. let smallBtn = document.querySelector('#de-btn3');
  544. smallBtn && smallBtn.remove();
  545. setTimeout(() => {
  546. addSliderNavDeBtn()
  547. })
  548. }
  549. } catch (e) {
  550. console.log(e)
  551. }
  552. }, 800)
  553. }
  554. function checkHasDeBtn() {
  555. try {
  556. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  557. let innerDeIcon = document.getElementById('de-btn1');
  558. if (toolBar && !innerDeIcon) {
  559. let dialogScheduleBtn = _getScheduleDom(false);
  560. dom && dom.deBtn1 && _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  561. }
  562. } catch (e) {
  563. console.log(e)
  564. }
  565. }
  566. /**
  567. * 点击发推,后端绑定推特id
  568. */
  569. function bindTwitterArtMethod() {
  570. if (!bindTwitterArt.postId) {
  571. return
  572. }
  573. if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
  574. bindTwitterArt.isBindIng = true;
  575. reportSrcPublishEvent({
  576. params: {
  577. postId: bindTwitterArt.postId,
  578. }
  579. }).then((res) => {
  580. if (res.code == 0) {
  581. Report.reportLog({
  582. objectType: Report.objectType.tweetPostBinded
  583. });
  584. bindTwitterArt.needBind = false;
  585. bindTwitterArt.postId = '';
  586. bindTwitterArt.isBindIng = false;
  587. }
  588. })
  589. }
  590. }
  591. import parseCard from './ParseCard'
  592. import { renderThumbStyle } from 'element-plus'
  593. // 检测dom改变
  594. // 获取短链接
  595. // 查看本地是否有postid
  596. // 如果有 修改dom 返回
  597. // 如果没有 网络请求
  598. // 获取postid
  599. // 获取twitterid
  600. // 检测当前所有dom 如果没有
  601. let queue_num = 1
  602. export const changeQueueNum = (num = 0) => {
  603. queue_num = queue_num + num
  604. if (queue_num > 5) {
  605. queue_num = 5
  606. }
  607. }
  608. let main_observer = null
  609. function onChangePageMain(targetNode) {
  610. try {
  611. const config = { attributes: false, childList: true, subtree: true };
  612. const callback = (mutationsList, observer) => {
  613. changeQueueNum(1)
  614. }
  615. main_observer = new MutationObserver(callback);
  616. main_observer.observe(targetNode, config);
  617. } catch (error) {
  618. main_observer = null
  619. }
  620. }
  621. // 1.监听main改变
  622. // 2.监听卡片是否可见
  623. // 3.如果可见了 去找
  624. function setIframeRedPacket(type = 'twitter') {
  625. // 获取所有卡片参数
  626. let card_json_data
  627. switch (type) {
  628. case 'facebook':
  629. card_json_data = parseCard.parseFacebookCardParmas()
  630. for (let i in card_json_data) {
  631. parseCard.replaceFacebookPacket(card_json_data[i])
  632. }
  633. break;
  634. default:
  635. card_json_data = parseCard.parseAllDeNetCardParmas()
  636. // 过滤出可以请求的短链接
  637. parseCard.getCardParmas(card_json_data).then((res) => {
  638. for (let i in res.has_post_Id_card_data) {
  639. let item = res.has_post_Id_card_data[i];
  640. if (item && item.post_Id && item.post_Id.indexOf('nft/') >= 0) {
  641. parseCard.replaceNftDomRedPacket(item)
  642. } else if (item && item.post_Id && item.post_Id.indexOf('nft_group/') >= 0) {
  643. parseCard.replaceNftGroupDomRedPacket(item)
  644. } else {
  645. parseCard.replaceDOMRedPacket(item)
  646. }
  647. }
  648. if (res.need_net_short_url.length > 0) {
  649. // 请求短链接
  650. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, () => { })
  651. }
  652. })
  653. break;
  654. }
  655. }
  656. // 监听点击发推 上报文案
  657. // document.addEventListener('click', (e) => {
  658. // try {
  659. // let inputEle = document.querySelector('div[contenteditable="true"]');
  660. // if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
  661. // // 获取文案上报
  662. // console.log(inputEle.innerText)
  663. // } else if (e.target.closest('div[data-testid=tweetButton]')) {
  664. // console.log(inputEle.innerText)
  665. // }
  666. // } catch (error) {
  667. // console.error('error', error)
  668. // }
  669. // })
  670. export function initExecuteScript(changes) {
  671. if (changes.executeScript) {
  672. let item = JSON.parse(changes.executeScript.newValue)
  673. if (item.executeScript) {
  674. init()
  675. }
  676. }
  677. }
  678. const createNFTIframe = ({ url, id }, callback) => {
  679. let iframe = document.createElement('iframe')
  680. iframe.id = id
  681. iframe.src = url
  682. iframe.style.cssText = 'border:medium none; width:100%; height:100%; z-index:100; position: fixed; top:0;left:0; display:none;';
  683. iframe.onload = () => {
  684. callback && callback()
  685. }
  686. document.body.appendChild(iframe);
  687. }
  688. function initParseCard() {
  689. let timer = setInterval(() => {
  690. let inTwitter = window.location.href.includes('twitter.com');
  691. let inTwitterNode = document.querySelector('main');
  692. let inFacebook = window.location.href.includes('facebook.com');
  693. let inFacebookNode = document.querySelector('#facebook');
  694. if (inTwitter && inTwitterNode) {
  695. clearInterval(timer)
  696. setInterval(() => {
  697. if (!main_observer) {
  698. onChangePageMain(inTwitterNode)
  699. changeQueueNum(1)
  700. }
  701. if (queue_num <= 0) {
  702. return
  703. }
  704. showNFTGroupIcon()
  705. setIframeRedPacket()
  706. checkHasDeBtn()
  707. checkHasSliderDeBtn();
  708. changeQueueNum(-1)
  709. showNFTCard()
  710. initGroupTip()
  711. addGroupTab();
  712. addJoinedGroupList();
  713. }, 1000)
  714. } else if (inFacebook && inFacebookNode) {
  715. clearInterval(timer)
  716. setInterval(() => {
  717. if (!main_observer) {
  718. onChangePageMain(inFacebookNode)
  719. changeQueueNum(1)
  720. }
  721. if (queue_num <= 0) {
  722. return
  723. }
  724. setIframeRedPacket('facebook')
  725. changeQueueNum(-1)
  726. }, 1000)
  727. }
  728. }, 1000);
  729. }
  730. let inited = false
  731. // 初始化
  732. export function init() {
  733. if (inited) {
  734. return
  735. }
  736. inited = true
  737. console.log('init')
  738. getDiscordAuthCode();
  739. appendPopupPage();
  740. chrome.runtime.sendMessage({
  741. actionType: "CONTENT_SET_POPUP_CONFIG",
  742. data: {
  743. popup: 'popup.html'
  744. }
  745. }, () => { });
  746. let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
  747. if (where) {
  748. return
  749. }
  750. twitterPinLogin();
  751. // 渲染dom
  752. initParseCard()
  753. showNFTCard()
  754. showNFTGroupIcon()
  755. addEventAction();
  756. checkUserJoinGroup();
  757. renderDom();
  758. checkTwitterTaskState();
  759. initBuyNFT();
  760. addJoinedGroupList();
  761. // getTweetAccountGroupInfo( () => {
  762. addGroupTab()
  763. // })
  764. getChromeStorage("popupShowPublishDialog", (res) => {
  765. console.log("popupShowPublishDialog", res);
  766. if (res && res.show) {
  767. setTimeout(() => {
  768. showTwitterPublishDialogHandler({
  769. srcContent: res.srcContent,
  770. postId: res.postId,
  771. copyContent: res.copyContent || ''
  772. });
  773. }, 1500);
  774. chrome.storage.local.remove("popupShowPublishDialog");
  775. }
  776. });
  777. getChromeStorage("userSettings", (res) => {
  778. setTimeout(() => {
  779. addPinedPop();
  780. if (res && !res.isOnToolbar) {
  781. setTimeout(() => {
  782. showPinTips();
  783. chrome.storage.local.remove("userSettings");
  784. }, 800);
  785. }
  786. }, 800);
  787. });
  788. }
  789. function checkHasSliderDeBtn() {
  790. let deBtn = document.getElementById('de-btn');
  791. let deBtn3 = document.getElementById('de-btn3');
  792. if (!deBtn && !deBtn3) {
  793. addSliderNavDeBtn();
  794. }
  795. }
  796. export function facebookReplyTweet(params) {
  797. if (window.location.origin.indexOf('twitter.com')) {
  798. const urlParams = new URLSearchParams(window.location.search);
  799. const actionType = urlParams.get('actionType');
  800. let deReplyParams = urlParams.get('deReplyParams') || '{}';
  801. deReplyParams = JSON.parse(deReplyParams);
  802. if (actionType == 'denetFacebookToTwitterReply') {
  803. if (params.postId == deReplyParams.postId) {
  804. let iframe = document.getElementById(params.postId);
  805. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_FACEBOOK_REPLY', data: deReplyParams }, '*');
  806. }
  807. }
  808. }
  809. }
  810. export function replyHandle(params) {
  811. let iframe = window.parent.document.getElementById(params.postId);
  812. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  813. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  814. if (replyBtn) {
  815. replyBtn.click();
  816. }
  817. onReplyDialogOpen(params, iframe);
  818. // 详情页推文底部评论处理
  819. let pathNameArr = window.location.pathname.split('/');
  820. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  821. let tweetReply = document.querySelector('div[data-testid="tweetButtonInline"]');
  822. if (tweetReply) {
  823. tweetReply.addEventListener('click', function () {
  824. // 详情页回复按钮点击
  825. Report.reportLog({
  826. pageSource: Report.pageSource.mainPage,
  827. businessType: Report.businessType.buttonClick,
  828. objectType: Report.objectType.replyClickByDetailPage
  829. });
  830. let eleList = tweetReply.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll('span[data-text="true"]');
  831. reportReplyResult(eleList, params, () => {
  832. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  833. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  834. });
  835. })
  836. }
  837. }
  838. }
  839. export function onTweetReplyClick(params) {
  840. let iframe = window.parent.document.getElementById(params.postId);
  841. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  842. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  843. if (replyBtn) {
  844. replyBtn.addEventListener('click', () => {
  845. onReplyDialogOpen(params, iframe);
  846. })
  847. }
  848. }
  849. function onReplyDialogOpen(params, iframe) {
  850. setTimeout(() => {
  851. let dialog = document.querySelector('div[role="dialog"]');
  852. let replyBtn;
  853. if (dialog) {
  854. let dialogContent = dialog.querySelector('div[role="dialog"]');
  855. replyBtn = dialog.querySelector('div[data-testid="toolBar"]').querySelector('div[data-testid="tweetButton"]');
  856. if (dialogContent) {
  857. let width = dialogContent.offsetWidth;
  858. let ele = document.createElement('div');
  859. ele.innerText = 'Tag 3 friends to complete the task';
  860. ele.style.cssText = `width: ${width}px; height: 38px; color: #fff; font-weight: 600;
  861. font-size: 16px; display: flex; align-items: center; justify-content: center; background: #1D9BF0;
  862. opacity: 0.8; position: absolute; top: 18px; left: 50%; transform: translateX(-50%); z-index: 1000`;
  863. dialogContent.style.top = '80px';
  864. dialogContent.parentNode.appendChild(ele);
  865. }
  866. } else {
  867. dialog = document.querySelector('main[role="main"]');
  868. if (dialog) {
  869. replyBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  870. }
  871. }
  872. if (dialog && replyBtn) {
  873. replyBtn.addEventListener('click', function () {
  874. // 推文页回复按钮点击
  875. Report.reportLog({
  876. pageSource: Report.pageSource.mainPage,
  877. businessType: Report.businessType.buttonClick,
  878. objectType: Report.objectType.replyClickByTwitterList
  879. });
  880. let eleList = dialog.querySelector('div[contenteditable="true"]').querySelectorAll('span[data-text="true"]');
  881. reportReplyResult(eleList, params, () => {
  882. // 上報完成
  883. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  884. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  885. })
  886. });
  887. }
  888. }, 1000);
  889. }
  890. const reportReplyResult = throttle(function (eleList, params, cb) {
  891. //未过滤的回复文本
  892. Report.reportLog({
  893. pageSource: Report.pageSource.mainPage,
  894. businessType: Report.businessType.buttonClick,
  895. objectType: Report.objectType.replyInputTextGet
  896. }, {
  897. replyStr: eleList
  898. });
  899. if (eleList && eleList.length) {
  900. let atList = [];
  901. for (let i = 0; i < eleList.length; i++) {
  902. let item = eleList[i];
  903. // 是否有中文
  904. let reg = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
  905. if (item && item.innerText.startsWith('@') && !reg.test(item.innerText)) {
  906. atList.push(item.innerText);
  907. }
  908. }
  909. // 去重过滤文本
  910. atList = Array.from(new Set(atList));
  911. Report.reportLog({
  912. pageSource: Report.pageSource.mainPage,
  913. businessType: Report.businessType.buttonClick,
  914. objectType: Report.objectType.replyFilterTextGet
  915. }, {
  916. replyStr: atList
  917. });
  918. if (atList.length >= 3) {
  919. //真实上报
  920. Report.reportLog({
  921. pageSource: Report.pageSource.mainPage,
  922. businessType: Report.businessType.buttonClick,
  923. objectType: Report.objectType.replyReport
  924. }, {
  925. replyStr: atList
  926. });
  927. fetchAddFinishEvent({
  928. eventType: params.type,
  929. luckdropId: params.taskLuckdropId
  930. }).then(res => {
  931. if (res.code == 0) {
  932. cb && cb();
  933. }
  934. })
  935. }
  936. }
  937. }, 800);
  938. // 根据推特id找到dom,完成任务
  939. export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_type = 'like') {
  940. // 1.根据推特ID寻找推文,获取卡片
  941. // 获取所有卡片参数
  942. let card_json_data = parseCard.parseAllDeNetCardParmas()
  943. let result = card_json_data.filter((item) => { return item.tweet_Id == tweet_Id }) || []
  944. if (result.length < 1) {
  945. return
  946. }
  947. result = result[0]
  948. switch (task_type) {
  949. case 'like':
  950. // https://twitter.com/intent/retweet?tweet_id=1525900221628223491
  951. if (result.dom_card) {
  952. result.dom_card.querySelector('div[data-testid=like]').click()
  953. result.dom_card.querySelector('iframe').contentWindow.postMessage({ actionType: 'CONTENT_DONE_TASK', task_type, }, '*');
  954. }
  955. break;
  956. case 'retweet':
  957. if (result.dom_card) {
  958. result.dom_card.querySelector('div[data-testid=retweet]').click()
  959. result.dom_card.querySelector('div[data-testid=retweetConfirm]').click()
  960. }
  961. break
  962. case 'follow':
  963. follow_names.forEach((item) => {
  964. window.open(`https://twitter.com/intent/follow?screen_name=${item}&tweet_Id=${tweet_Id}`)
  965. })
  966. break
  967. }
  968. }
  969. function clickByDataTestId(e, id, callback) {
  970. if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
  971. callback()
  972. } else if (e.target.closest('div[data-testid=' + id + ']')) {
  973. callback()
  974. }
  975. }
  976. // 校验关注推特状态
  977. export function checkTwitterTaskState() {
  978. let task_type = ''
  979. let url = window.location.href
  980. let tweet_Id
  981. let task_data = {
  982. follow_name: ''
  983. }
  984. // 校验当前链接
  985. if (url.includes('https://twitter.com/intent/retweet')) {
  986. task_type = 'retweet'
  987. } else if (url.includes('https://twitter.com/intent/follow')) {
  988. task_type = 'follow'
  989. task_data.follow_name = getQueryString('screen_name')
  990. } else if (url.includes('https://twitter.com/intent/like')) {
  991. task_type = 'like'
  992. } else {
  993. return
  994. }
  995. tweet_Id = getQueryString('tweet_id')
  996. // let root_status
  997. document.body.addEventListener('click', (e) => {
  998. // 点击 确认
  999. clickByDataTestId(e, 'confirmationSheetConfirm', () => {
  1000. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1001. })
  1002. // 点击取消
  1003. clickByDataTestId(e, 'confirmationSheetCancel', () => {
  1004. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  1005. })
  1006. // 点击 蒙层
  1007. if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
  1008. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  1009. }
  1010. }, true)
  1011. let timer = setInterval(() => {
  1012. // 喜欢
  1013. if (document.querySelector('div[data-testid=unlike]') && task_type == 'like') {
  1014. clearInterval(timer)
  1015. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1016. }
  1017. // 转推
  1018. if (document.querySelector('div[data-testid=unretweet]') && task_type == 'retweet') {
  1019. clearInterval(timer)
  1020. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1021. }
  1022. // 关注
  1023. if (task_type == 'follow') {
  1024. let follow_area = document.querySelector('div[data-testid=placementTracking]')
  1025. if (follow_area && follow_area.querySelectorAll('div')) {
  1026. follow_area = follow_area.querySelectorAll('div')
  1027. clearInterval(timer)
  1028. for (let i in follow_area) {
  1029. if (follow_area[i] && follow_area[i].dataset && follow_area[i].dataset.testid && follow_area[i].dataset.testid.indexOf('unfollow') > 0) {
  1030. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1031. break
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }, 1000)
  1037. }
  1038. export function getTweetAuthorByDom(params) {
  1039. let iframe = document.getElementById(params.postId);
  1040. if (!iframe) {
  1041. return;
  1042. }
  1043. let fullNameDom;
  1044. let pathNameArr = window.location.pathname.split('/');
  1045. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  1046. fullNameDom = iframe.parentNode.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1047. } else {
  1048. fullNameDom = iframe.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1049. }
  1050. if (fullNameDom) {
  1051. let arr = fullNameDom.href.split('/');
  1052. if (arr.length) {
  1053. let fullName = arr[arr.length - 1];
  1054. if (fullName) {
  1055. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_GET_TWEET_AUTHOR', data: { fullName } }, '*');
  1056. }
  1057. }
  1058. }
  1059. }
  1060. export function doTaskTwitterAPI({ task_data, task_type }) {
  1061. switch (task_type) {
  1062. case 'like':
  1063. TwitterLikeAPI(task_data.tweet_Id)
  1064. break
  1065. case 'retweet':
  1066. TwitterRetweetAPI(task_data.tweet_Id)
  1067. break
  1068. case 'follow':
  1069. task_data.follow_data.forEach((item) => {
  1070. if (item.name && item.twitterUserId) {
  1071. TwitterFollowAPI(item, task_data.tweet_Id)
  1072. }
  1073. })
  1074. break
  1075. }
  1076. }
  1077. export function showJoinDialog(data) {
  1078. let iframe = document.querySelector('#nftProjectId')
  1079. iframe.style.display = 'block'
  1080. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html#/group?type=${data.type}&buy_nft_status=${data.buy_nft_status}&nft_group_Id=${data.nft_group_Id}&nftProjectId=${data.buyNftProjectId}`)
  1081. }
  1082. const TwitterFollowAPI = (item, tweet_Id) => {
  1083. fetch("https://twitter.com/i/api/1.1/friendships/create.json", {
  1084. "headers": {
  1085. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1086. "content-type": "application/x-www-form-urlencoded",
  1087. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1088. "sec-ch-ua-mobile": "?0",
  1089. "sec-ch-ua-platform": "\"macOS\"",
  1090. "x-csrf-token": getCookie('ct0'),
  1091. "x-twitter-active-user": "yes",
  1092. "x-twitter-auth-type": "OAuth2Session",
  1093. "x-twitter-client-language": "zh-cn"
  1094. },
  1095. "referrer": "https://twitter.com/home",
  1096. "referrerPolicy": "strict-origin-when-cross-origin",
  1097. "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 + "",
  1098. "method": "POST",
  1099. "mode": "cors",
  1100. "credentials": "include"
  1101. }).then(() => {
  1102. let task_data = {
  1103. follow_name: item.name
  1104. }
  1105. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: true }, () => { })
  1106. }).catch(() => {
  1107. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: false }, () => { })
  1108. })
  1109. }
  1110. const TwitterRetweetAPI = (tweet_Id) => {
  1111. fetch("https://twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet", {
  1112. "headers": {
  1113. "accept": "*/*",
  1114. "accept-language": "zh,en;q=0.9,zh-CN;q=0.8",
  1115. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1116. "content-type": "application/json",
  1117. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1118. "sec-ch-ua-mobile": "?0",
  1119. "sec-ch-ua-platform": "\"macOS\"",
  1120. "sec-fetch-dest": "empty",
  1121. "sec-fetch-mode": "cors",
  1122. "sec-fetch-site": "same-origin",
  1123. "x-csrf-token": getCookie('ct0'),
  1124. "x-twitter-active-user": "yes",
  1125. "x-twitter-auth-type": "OAuth2Session",
  1126. "x-twitter-client-language": "zh-cn"
  1127. },
  1128. "referrer": "https://twitter.com/home",
  1129. "referrerPolicy": "strict-origin-when-cross-origin",
  1130. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\",\"dark_request\":false},\"queryId\":\"ojPdsZsimiJrUGLR1sjUtA\"}",
  1131. "method": "POST",
  1132. "mode": "cors",
  1133. "credentials": "include"
  1134. }).then(() => {
  1135. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: true }, () => { })
  1136. }).catch(() => {
  1137. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: false }, () => { })
  1138. })
  1139. }
  1140. const TwitterLikeAPI = (tweet_Id) => {
  1141. fetch("https://twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet", {
  1142. "headers": {
  1143. "accept": "*/*",
  1144. "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
  1145. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1146. "content-type": "application/json",
  1147. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
  1148. "sec-ch-ua-mobile": "?0",
  1149. "sec-ch-ua-platform": "\"Windows\"",
  1150. "sec-fetch-dest": "empty",
  1151. "sec-fetch-mode": "cors",
  1152. "sec-fetch-site": "same-origin",
  1153. "x-csrf-token": getCookie('ct0'),
  1154. "x-twitter-active-user": "yes",
  1155. "x-twitter-auth-type": "OAuth2Session",
  1156. "x-twitter-client-language": "en"
  1157. },
  1158. "referrer": "https://twitter.com/home",
  1159. "referrerPolicy": "strict-origin-when-cross-origin",
  1160. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\"},\"queryId\":\"lI07N6Otwv1PhnEgXILM7A\"}",
  1161. "method": "POST",
  1162. "mode": "cors",
  1163. "credentials": "include"
  1164. }).then(() => {
  1165. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: true }, () => { })
  1166. }).catch(() => {
  1167. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: false }, () => { })
  1168. })
  1169. }
  1170. export const showTwitterPost = (data) => {
  1171. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  1172. if (bigBtn) {
  1173. bigBtn.click();
  1174. } else {
  1175. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  1176. smallBtn && smallBtn.click();
  1177. }
  1178. content_get_nft_post_pre({
  1179. groupId: data.groupId
  1180. })
  1181. // addPublishTipsIframe({ type: 'nft' })
  1182. }
  1183. function publishNFTTweetEvent({ groupId, postId, srcContent }) {
  1184. setTimeout(() => {
  1185. let publishTweetBtn;
  1186. let dialog = document.querySelector('div[role="dialog"]');
  1187. if (dialog) {
  1188. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  1189. } else {
  1190. let domMain = document.querySelector('main[role="main"]');
  1191. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  1192. }
  1193. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  1194. // 获取文案上报
  1195. let inputEle = document.querySelector('div[contenteditable="true"]');
  1196. let textContent = inputEle.innerText
  1197. let arr = srcContent.split(' ') || []
  1198. arr.forEach((item) => {
  1199. if (textContent.includes(item)) {
  1200. textContent = textContent.replaceAll(item, '')
  1201. }
  1202. })
  1203. let formData = {
  1204. groupId,
  1205. textContent
  1206. }
  1207. let params = {
  1208. postBizData: JSON.stringify(formData),
  1209. postSrc: 1, //1 twitter
  1210. postType: 2, //2 nft
  1211. postId
  1212. }
  1213. chrome.runtime.sendMessage({
  1214. actionType: "CONTENT_NFT_TXT_PUBLISH",
  1215. data: params
  1216. }, () => { });
  1217. setTimeout(() => {
  1218. document.querySelector('a[data-testid="AppTabBar_Profile_Link"]').click()
  1219. }, 2000);
  1220. });
  1221. }, 800)
  1222. }
  1223. const content_get_nft_post_pre = (data) => {
  1224. chrome.runtime.sendMessage({
  1225. actionType: "CONTENT_GET_TWITTER_NFT_POST_PRE",
  1226. data
  1227. }, () => { });
  1228. }
  1229. let tweet_nft_content = {}
  1230. export const setTwitterTextarea = (params) => {
  1231. tweet_nft_content = params
  1232. let inputEle = document.querySelector('div[contenteditable="true"]');
  1233. if (inputEle) {
  1234. inputEle.focus();
  1235. document.execCommand("insertText", false, '');
  1236. }
  1237. setTimeout(() => {
  1238. document.execCommand("insertText", false, params.srcContent);
  1239. }, 1000);
  1240. publishNFTTweetEvent(params)
  1241. }
  1242. const initGroupTip = () => {
  1243. let arr = window.location.pathname.split('/') || []
  1244. if (arr.length == 2) {
  1245. let twitterAccount = arr[1]
  1246. addGroupTip(twitterAccount)
  1247. }
  1248. }
  1249. export const showGroupTip = () => {
  1250. let dom_denet_group_tip = document.querySelector('#denet_group_tip')
  1251. dom_denet_group_tip.style.display = 'block'
  1252. }
  1253. const addGroupTip = (twitterAccount) => {
  1254. let dom_denet_group_tip = document.querySelector('#denet_group_tip')
  1255. if (dom_denet_group_tip) {
  1256. return
  1257. }
  1258. try {
  1259. let nav_list = document.querySelectorAll('nav[role=navigation]') || []
  1260. let dom
  1261. for (let i in nav_list) {
  1262. if (nav_list[i] && nav_list[i].querySelector('div[data-testid="ScrollSnap-SwipeableList"]')) {
  1263. dom = nav_list[i]
  1264. break
  1265. }
  1266. }
  1267. let iframe = document.createElement('iframe')
  1268. iframe.id = 'denet_group_tip'
  1269. iframe.style.cssText = 'border: medium none; display:none; width:100%; height:100px;'
  1270. iframe.src = chrome.runtime.getURL(`/iframe/group-card.html?twitterAccount=${twitterAccount}`)
  1271. if (dom) {
  1272. dom.parentNode.children[0].appendChild(iframe)
  1273. }
  1274. } catch (error) {
  1275. }
  1276. }
  1277. export const hideBuyNFT = () => {
  1278. let iframe = document.querySelector('#nftProjectId')
  1279. iframe.style.display = 'none'
  1280. iframe.src = ''
  1281. }
  1282. export const showBuyNFT = ({ nft_project_Id }) => {
  1283. if (!nft_project_Id) {
  1284. return
  1285. }
  1286. let iframe = document.querySelector('#nftProjectId')
  1287. iframe.style.display = 'block'
  1288. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html?nftProjectId=${nft_project_Id}`)
  1289. }
  1290. const initBuyNFT = () => {
  1291. let url = chrome.runtime.getURL(`/iframe/buy-nft.html`)
  1292. let id = `nftProjectId`
  1293. createNFTIframe({ url, id })
  1294. }
  1295. export const showNFTCard = () => {
  1296. let urlInfo = new URL(window.location.href)
  1297. let isTwitter = urlInfo.hostname === 'twitter.com'
  1298. let userElem = document.querySelector('div[data-testid="UserName"]');
  1299. let sideElem = document.querySelector('div[data-testid="sidebarColumn"]')
  1300. let tabIndex = sideElem && sideElem.querySelector('div[tabindex="0"]');
  1301. let isAppend = document.querySelector('div[id="de-nft-node"]');
  1302. let where = isTwitter && userElem && tabIndex;
  1303. if (where) {
  1304. let iframe = document.createElement('iframe');
  1305. iframe.src = chrome.runtime.getURL(`/iframe/nft-card.html`)
  1306. iframe.style.cssText = 'border:medium none; width:100%; height:290px;';
  1307. let nftElement = document.createElement('div');
  1308. nftElement.id = 'de-nft-node';
  1309. nftElement.innerHTML = `
  1310. ${iframe.outerHTML}
  1311. <style>
  1312. #de-nft-node {height:290px; margin-bottom:17px; display:none;}
  1313. </style>
  1314. `;
  1315. if (tabIndex && tabIndex.firstChild && tabIndex.firstChild.childNodes && !isAppend) {
  1316. tabIndex.firstChild.insertBefore(nftElement, tabIndex.firstChild.childNodes[2]);
  1317. }
  1318. }
  1319. }
  1320. export const showNFTSale = () => {
  1321. document.querySelector('div[id="de-nft-node"]').style.display = 'block';
  1322. }
  1323. export const addEventAction = () => {
  1324. let urlInfo = new URL(window.location.href)
  1325. let isTwitter = urlInfo.hostname === 'twitter.com'
  1326. // 监听发推按钮
  1327. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  1328. let smallBtn = document.querySelector('a[href="/compose/tweet"]');
  1329. let addBtn = document.querySelector('div[data-testid="addButton"]');
  1330. elemAddEventListener(bigBtn, 'click', () => { queue_num = 3 })
  1331. elemAddEventListener(smallBtn, 'click', () => { queue_num = 3 })
  1332. elemAddEventListener(addBtn, 'click', () => { queue_num = 3 })
  1333. // 页面滚动
  1334. if (isTwitter) {
  1335. // 首页
  1336. if (urlInfo.pathname === '/home') {
  1337. window.addEventListener('scroll', () => {
  1338. hideNFTGroupList()
  1339. })
  1340. }
  1341. }
  1342. }
  1343. export const appendPopupPage = (params = {}) => {
  1344. let { path = '' } = params;
  1345. let iframe = document.createElement('iframe');
  1346. iframe.id = 'de-popup-page';
  1347. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1348. iframe.style.cssText = `border: medium none; width: 375px;
  1349. height: 650px;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: 90000;
  1350. animation-duration: 0.5s !important;
  1351. animation-timing-function: ease-in-out !important;
  1352. animation-fill-mode: forwards !important;
  1353. transition: all 1s ease 0s !important;right: -385px;transform: translateX(385px);`
  1354. let iframeContent = document.getElementById('de-popup-page');
  1355. let overlayDom = document.createElement('div');
  1356. overlayDom.id = 'de-popup-overlay';
  1357. overlayDom.style.cssText = `position: fixed;z-index: 88888;top: 0;
  1358. left: 0;width: 100%;height: 100%;opacity: 0;display: none`;
  1359. let overlay = document.getElementById('de-popup-overlay');
  1360. let body = document.querySelector('body');
  1361. if (!iframeContent && body) {
  1362. document.querySelector('body').appendChild(iframe);
  1363. if (!overlay) {
  1364. document.querySelector('body').appendChild(overlayDom);
  1365. overlayDom.addEventListener('click', function () {
  1366. hidePopupPage();
  1367. })
  1368. } else {
  1369. overlay.addEventListener('click', function () {
  1370. hidePopupPage();
  1371. })
  1372. }
  1373. }
  1374. }
  1375. let showPopupPageFrom = '';
  1376. export const showPopupPage = (params = {}) => {
  1377. let { path = '', from, showJoinGroupFinish = false } = params;
  1378. showPopupPageFrom = from;
  1379. hidePinTips();
  1380. hideNoticeBindTweet();
  1381. let iframe = document.getElementById('de-popup-page');
  1382. if (!iframe) {
  1383. appendPopupPage();
  1384. iframe = document.getElementById('de-popup-page');
  1385. }
  1386. if (iframe) {
  1387. if (path) {
  1388. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1389. }
  1390. iframe.style.transform = 'translateX(-' + 395 + 'px)';
  1391. chrome.runtime.sendMessage({
  1392. actionType: "CONTENT_POPUP_PAGE_SHOW",
  1393. data: {
  1394. path,
  1395. showJoinGroupFinish,
  1396. }
  1397. }, () => { });
  1398. chrome.runtime.sendMessage({
  1399. actionType: "CONTENT_SET_POPUP_CONFIG",
  1400. data: {
  1401. popup: ''
  1402. }
  1403. }, () => { });
  1404. let overlay = document.getElementById('de-popup-overlay');
  1405. overlay.style.display = 'block';
  1406. let htmlDom = document.querySelector('html');
  1407. if (htmlDom) {
  1408. htmlDom.style.overflowY = 'hidden';
  1409. }
  1410. }
  1411. }
  1412. export const hidePopupPage = () => {
  1413. let iframe = document.getElementById('de-popup-page');
  1414. if (iframe) {
  1415. iframe.style.transform = 'translateX(' + 385 + 'px)';
  1416. let overlay = document.getElementById('de-popup-overlay');
  1417. overlay.style.display = 'none';
  1418. let htmlDom = document.querySelector('html');
  1419. if (htmlDom) {
  1420. htmlDom.style.overflowY = 'auto';
  1421. }
  1422. chrome.runtime.sendMessage({
  1423. actionType: "CONTENT_SET_POPUP_CONFIG",
  1424. data: {
  1425. popup: 'popup.html'
  1426. }
  1427. }, () => { });
  1428. if (showPopupPageFrom == 'BUY_NFT_FINISH') {
  1429. chrome.runtime.sendMessage({
  1430. actionType: "CONTENT_GET_PINED",
  1431. data: {}
  1432. }, () => { });
  1433. showPopupPageFrom = '';
  1434. }
  1435. }
  1436. }
  1437. export const tiggerInjectPopupPage = () => {
  1438. let iframeContent = document.getElementById('de-popup-page');
  1439. if (iframeContent) {
  1440. hidePinTips();
  1441. hideNoticeBindTweet();
  1442. let { transform = '' } = iframeContent.style;
  1443. if (transform == 'translateX(385px)' || !transform) {
  1444. showPopupPage();
  1445. } else {
  1446. hidePopupPage();
  1447. }
  1448. } else {
  1449. appendPopupPage();
  1450. setTimeout(() => {
  1451. let iframe = document.getElementById('de-popup-page');
  1452. let { transform = '' } = iframe.style;
  1453. if (transform == 'translateX(385px)' || !transform) {
  1454. showPopupPage();
  1455. } else {
  1456. hidePopupPage();
  1457. }
  1458. }, 300)
  1459. }
  1460. }
  1461. const onBodyClick = () => {
  1462. if (window.location.href.indexOf('api.twitter.com') < 0) {
  1463. document.querySelector('body').addEventListener('click', function () {
  1464. console.log('click')
  1465. // hidePopupPage();
  1466. })
  1467. }
  1468. }
  1469. export const setPopupConfByPopupPage = () => {
  1470. let iframe = document.getElementById('de-popup-page');
  1471. if (iframe) {
  1472. let { transform = '' } = iframe.style;
  1473. if (transform && transform == 'translateX(-395px)') {
  1474. chrome.runtime.sendMessage({
  1475. actionType: "CONTENT_SET_POPUP_CONFIG",
  1476. data: {
  1477. popup: ''
  1478. }
  1479. }, () => { });
  1480. } else {
  1481. chrome.runtime.sendMessage({
  1482. actionType: "CONTENT_SET_POPUP_CONFIG",
  1483. data: {
  1484. popup: 'popup.html'
  1485. }
  1486. }, () => { });
  1487. }
  1488. } else {
  1489. chrome.runtime.sendMessage({
  1490. actionType: "CONTENT_SET_POPUP_CONFIG",
  1491. data: {
  1492. popup: 'popup.html'
  1493. }
  1494. }, () => { });
  1495. }
  1496. }
  1497. /**
  1498. *
  1499. * Group Tab List Start
  1500. */
  1501. const addGroupTab = () => {
  1502. // if(!tweetAccountBindGroupInfo.isInit) {
  1503. // getTweetAccountGroupInfo(() => {
  1504. // main();
  1505. // })
  1506. // } else {
  1507. main();
  1508. // }
  1509. function main() {
  1510. // if(!tweetAccountBindGroupInfo.isBind) {
  1511. // return;
  1512. // }
  1513. let tabListDom = document.querySelector('div[role="tablist"]');
  1514. let groupItemTab = document.querySelector('#de-nav-tab-group');
  1515. let groupIcon = document.createElement('img');
  1516. groupIcon.id = 'de-group-tab-icon'
  1517. groupIcon.src = require("@/assets/img/icon-group-tab-item.png");
  1518. groupIcon.style.cssText = 'width:20px;height: 20px;margin-right:4px;';
  1519. let divNode = document.createElement('div');
  1520. divNode.style.cssText = 'display: flex; align-items: center';
  1521. divNode.appendChild(groupIcon);
  1522. divNode.appendChild(document.createTextNode('Group'));
  1523. if (tabListDom && !groupItemTab) {
  1524. let lineDom = document.createElement('div');
  1525. lineDom.id = 'de-tab-line';
  1526. lineDom.style.cssText = `border-radius: 9999px;
  1527. position: absolute;
  1528. bottom: 0px;
  1529. min-width: 56px;
  1530. align-self: center;
  1531. height: 4px;
  1532. background-color: rgb(29, 155, 240);
  1533. display: none`;
  1534. let groupTab = document.createElement('div');
  1535. groupTab.id = 'de-nav-tab-group';
  1536. groupTab.style.cssText = `z-index: 1;
  1537. position: relative;
  1538. display: flex;
  1539. min-width: 56px;
  1540. -webkit-box-pack: center;
  1541. justify-content: center;
  1542. -webkit-box-align: center;
  1543. align-items: center;
  1544. text-align: center;
  1545. padding: 0px 16px;
  1546. color: rgb(83, 100, 113);
  1547. font-weight: 700;
  1548. height: 53px;
  1549. cursor: pointer;
  1550. font: 500 15px / 20px TwitterChirp, -apple-system, "system-ui", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;`;
  1551. groupTab.appendChild(divNode);
  1552. groupTab.appendChild(lineDom);
  1553. tabListDom.appendChild(groupTab);
  1554. groupTab.addEventListener('mouseenter', function () {
  1555. groupTab.style.background = 'rgba(15, 20, 25, 0.1)'
  1556. });
  1557. groupTab.addEventListener('mouseleave', function () {
  1558. groupTab.style.background = 'none'
  1559. });
  1560. addGroupTabEventListener({ groupTab });
  1561. }
  1562. addTweetTabEventListener({
  1563. tabListDom
  1564. });
  1565. addTabGroupContent();
  1566. checkNeedSelectGroupTab();
  1567. }
  1568. }
  1569. /**
  1570. * 跳转到个人主页 检查是否需要选中 Group tab
  1571. */
  1572. const checkNeedSelectGroupTab = () => {
  1573. if (window.location.pathname != '/home') {
  1574. setTimeout(() => {
  1575. const urlParams = new URLSearchParams(window.location.search);
  1576. const deTabVal = urlParams.get('deTabVal');
  1577. if (deTabVal == 'deGroupTab') {
  1578. selectGroupTab();
  1579. }
  1580. }, 500)
  1581. }
  1582. }
  1583. /** 选中 Group tab */
  1584. export const selectGroupTab = () => {
  1585. let groupTab = document.querySelector('#de-nav-tab-group');
  1586. if (groupTab) {
  1587. groupTab.click();
  1588. }
  1589. };
  1590. /**
  1591. *
  1592. * Group tab点击事件监听
  1593. */
  1594. const addGroupTabEventListener = (params) => {
  1595. let { groupTab } = params;
  1596. groupTab.addEventListener('click', function () {
  1597. setGroupTabSelfStyle({
  1598. groupColor: 'rgb(15, 20, 25)',
  1599. groupFontWeight: '700',
  1600. lineDisplay: 'block'
  1601. });
  1602. setTweetActiveTabStyle({
  1603. color: 'rgb(83, 100, 113)',
  1604. display: 'none'
  1605. });
  1606. setTabContentStyle({
  1607. tweetTabContentDisply: 'none',
  1608. iframeContentDisplay: 'block'
  1609. });
  1610. refreshTabGroup();
  1611. window.addEventListener('scroll', addPageScrollEvent)
  1612. chrome.runtime.sendMessage({
  1613. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1614. data: { type: 'btn' }
  1615. }, () => { });
  1616. })
  1617. }
  1618. const addPageScrollEvent = () => {
  1619. let wrapperDom = document.querySelector('html');
  1620. let contentDom = document.querySelector('main[role="main"]');
  1621. let data = {
  1622. wrapperHeight: wrapperDom.offsetHeight,
  1623. wrapperScrollTop: wrapperDom.scrollTop,
  1624. contentHeight: contentDom.offsetHeight
  1625. }
  1626. chrome.runtime.sendMessage({
  1627. actionType: "CONTENT_GROUP_LIST_SCROLL",
  1628. data: data
  1629. }, () => { });
  1630. };
  1631. /**
  1632. *
  1633. * twitter tab点击事件监听
  1634. */
  1635. const addTweetTabEventListener = (params) => {
  1636. let { tabListDom } = params;
  1637. let groupItemTab = document.querySelector('#de-nav-tab-group');
  1638. if (tabListDom && groupItemTab) {
  1639. // 监听twitter tab点击事件
  1640. let tweetTabItem = tabListDom.querySelectorAll('div[role="presentation"]');
  1641. if (tweetTabItem.length) {
  1642. for (let i = 0; i < tweetTabItem.length; i++) {
  1643. let item = tweetTabItem[i];
  1644. item.addEventListener('click', function () {
  1645. window.removeEventListener('scroll', addPageScrollEvent);
  1646. setGroupTabSelfStyle({
  1647. groupColor: 'rgb(83, 100, 113)',
  1648. groupFontWeight: '500',
  1649. lineDisplay: 'none'
  1650. });
  1651. setTabContentStyle({
  1652. tweetTabContentDisply: 'block',
  1653. iframeContentDisplay: 'none'
  1654. });
  1655. setTweetActiveTabStyle({
  1656. color: 'rgb(15, 20, 25)',
  1657. display: 'block'
  1658. });
  1659. chrome.runtime.sendMessage({
  1660. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1661. data: { type: 'arrow' }
  1662. }, () => { });
  1663. })
  1664. }
  1665. }
  1666. }
  1667. }
  1668. /**
  1669. * 设置 Group Tab 样式
  1670. * */
  1671. const setGroupTabSelfStyle = (params = {}) => {
  1672. let { groupColor, groupFontWeight, lineDisplay } = params;
  1673. let groupTab = document.querySelector('#de-nav-tab-group');
  1674. groupTab.style.color = groupColor;
  1675. groupTab.style.fontWeight = groupFontWeight;
  1676. let lineDom = groupTab.querySelector('#de-tab-line');
  1677. if (lineDom) {
  1678. lineDom.style.display = lineDisplay;
  1679. }
  1680. };
  1681. /**
  1682. * 切换到 Group tab时 刷新列表
  1683. */
  1684. const refreshTabGroup = () => {
  1685. chrome.runtime.sendMessage({
  1686. actionType: "CONTENT_REFRESH_TAB_GROUP_LIST",
  1687. data: {}
  1688. }, () => { });
  1689. }
  1690. /**
  1691. *
  1692. * tab选中时设置 激活 的字体样式和选中条
  1693. */
  1694. const setTweetActiveTabStyle = (params) => {
  1695. let { color, display } = params || {};
  1696. let tweetActiveTab = document.querySelector('a[aria-selected="true"]').querySelector('div');
  1697. tweetActiveTab.style.color = color;
  1698. let tweetTabLine = tweetActiveTab.querySelector('div');
  1699. if (tweetTabLine) {
  1700. tweetTabLine.style.display = display;
  1701. }
  1702. }
  1703. /**
  1704. *
  1705. * 设置 tab 切换时 tab内容的样式(显示隐藏)
  1706. */
  1707. const setTabContentStyle = (params) => {
  1708. let { tweetTabContentDisply, iframeContentDisplay } = params;
  1709. let tweetTabContent = getTweetTabContent();
  1710. if (tweetTabContent) {
  1711. if (tweetTabContentDisply == 'block') {
  1712. let { display } = tweetTabContent.style;
  1713. if (display == 'none') {
  1714. tweetTabContent.style.display = 'block';
  1715. }
  1716. } else {
  1717. tweetTabContent.style.display = tweetTabContentDisply;
  1718. }
  1719. }
  1720. let iframeContent = document.getElementById('de-tab-group-content');
  1721. if (!iframeContent) {
  1722. addTabGroupContent();
  1723. }
  1724. setTimeout(() => {
  1725. iframeContent = document.getElementById('de-tab-group-content');
  1726. if (iframeContent) {
  1727. iframeContent.style.display = iframeContentDisplay;
  1728. }
  1729. })
  1730. };
  1731. /**
  1732. *
  1733. * 获取 twitter tab 下的内容
  1734. */
  1735. const getTweetTabContent = () => {
  1736. let tweetTabContent = document.querySelector('[data-testid="primaryColumn"] [role="navigation"] + * > div[aria-label]:not([role="progressbar"])') || document.querySelector('div[data-testid="emptyState"]');
  1737. return tweetTabContent;
  1738. }
  1739. /**
  1740. * 注入 Group List 内容
  1741. */
  1742. const addTabGroupContent = () => {
  1743. let params = {
  1744. windowLocation: window.location
  1745. }
  1746. let iframe = document.createElement('iframe');
  1747. iframe.id = 'de-tab-group-content';
  1748. iframe.src = chrome.runtime.getURL('/iframe/tab-group.html') + `?params=${JSON.stringify(params)}`;
  1749. iframe.style.cssText = `border: medium none; height: 500px;display: none`
  1750. let iframeContent = document.getElementById('de-tab-group-content');
  1751. let tweetTabContent = getTweetTabContent();
  1752. if (!iframeContent) {
  1753. if (tweetTabContent && tweetTabContent.parentElement) {
  1754. tweetTabContent.parentElement.appendChild(iframe);
  1755. }
  1756. }
  1757. };
  1758. /**
  1759. *
  1760. * 设置Tab Group Iframe 样式
  1761. */
  1762. export const setTabGroupIframeStyle = (params) => {
  1763. let iframeContent = document.getElementById('de-tab-group-content');
  1764. if(iframeContent) {
  1765. iframeContent.style.height = document.querySelector('html').offsetHeight + 'px';
  1766. }
  1767. }
  1768. export const pageJumpHandler = (params) => {
  1769. let { url, name = '_self' } = params
  1770. window.open(url, name)
  1771. }
  1772. export const getTweetProfileNavTop = (params) => {
  1773. let top = document.querySelector('div[role="tablist"]').closest('nav').getBoundingClientRect().top;
  1774. chrome.runtime.sendMessage({
  1775. actionType: "CONTENT_SEND_GROUP_NAV_TOP", data: {
  1776. top,
  1777. scrollTop: params.scrollTop
  1778. }
  1779. }, () => { })
  1780. }
  1781. /**
  1782. *
  1783. * Group Tab List End
  1784. *
  1785. */
  1786. export const loginSuccessHandle = () => {
  1787. // 检查是否漏出group图标
  1788. checkUserJoinGroup(() => {
  1789. showNFTGroupIcon()
  1790. addEventAction()
  1791. addJoinedGroupList();
  1792. })
  1793. }
  1794. export const setGroupInfo = (params = {}) => {
  1795. console.log('params', params)
  1796. tweetAccountBindGroupInfo.groupInfo = params;
  1797. if(!params.nftGroupId) {
  1798. let groupTab = document.querySelector('#de-nav-tab-group');
  1799. if (groupTab) {
  1800. groupTab.style.display = 'none';
  1801. } else {
  1802. setTimeout(() => {
  1803. groupTab = document.querySelector('#de-nav-tab-group');
  1804. if (groupTab) {
  1805. groupTab.style.display = 'none';
  1806. }
  1807. }, 500)
  1808. }
  1809. }
  1810. }
  1811. const getTweetAccountGroupInfo = (cb) => {
  1812. let arr = window.location.pathname.split('/') || [];
  1813. let tabListDom = document.querySelector('div[role="tablist"]');
  1814. if (arr.length == 2 && tabListDom && !tweetAccountBindGroupInfo.isInit) {
  1815. let twitterAccount = arr[1];
  1816. tweetAccountBindGroupInfo.isInit = true;
  1817. getTwitterNftGroupInfo({
  1818. params: {
  1819. twitterAccount
  1820. }
  1821. }).then(res => {
  1822. if (res.code == 0) {
  1823. if (res.data) {
  1824. tweetAccountBindGroupInfo.groupInfo = res.data;
  1825. tweetAccountBindGroupInfo.isBind = true;
  1826. } else {
  1827. tweetAccountBindGroupInfo = {
  1828. isBind: false,
  1829. groupInfo: null
  1830. }
  1831. }
  1832. } else {
  1833. tweetAccountBindGroupInfo = {
  1834. isBind: false,
  1835. groupInfo: null
  1836. }
  1837. }
  1838. cb && cb()
  1839. })
  1840. }
  1841. }