twitter.js 75 KB

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