twitter.js 73 KB

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