twitter.js 78 KB

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