twitter.js 74 KB

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