twitter.js 69 KB

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