twitter.js 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  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.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. 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. }
  552. function onWindowResize() {
  553. window.onresize = throttle(function () {
  554. setTabGroupIframeStyle();
  555. try {
  556. if (tweetPublishStore.showPublishDialog) {
  557. let dialog = document.querySelector('div[role="dialog"]');
  558. let dePublishTips = document.getElementById('de-publish-tips');
  559. if (dialog && !dePublishTips) {
  560. addPublishTipsIframe({ time: 0 });
  561. } else if (dialog && dePublishTips) {
  562. let dialogContent = dialog.querySelector('div[role=dialog]')
  563. if (dialogContent) {
  564. let right = dialogContent.offsetLeft - 15 - 266;
  565. dePublishTips.style.right = right + 'px';
  566. }
  567. }
  568. }
  569. if (window.innerWidth < 1273) {
  570. let bigBtn = document.querySelector('#de-btn');
  571. bigBtn && bigBtn.remove();
  572. setTimeout(() => {
  573. addSliderNavDeBtn(true);
  574. })
  575. } else {
  576. let smallBtn = document.querySelector('#de-btn3');
  577. smallBtn && smallBtn.remove();
  578. setTimeout(() => {
  579. addSliderNavDeBtn()
  580. })
  581. }
  582. } catch (e) {
  583. console.log(e)
  584. }
  585. }, 800)
  586. }
  587. function checkHasDeBtn() {
  588. try {
  589. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  590. let innerDeIcon = document.getElementById('de-btn1');
  591. if (toolBar && !innerDeIcon) {
  592. let dialogScheduleBtn = _getScheduleDom(false);
  593. dom && dom.deBtn1 && _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  594. }
  595. } catch (e) {
  596. console.log(e)
  597. }
  598. }
  599. /**
  600. * 点击发推,后端绑定推特id
  601. */
  602. export function bindTwitterArtMethod() {
  603. if (!bindTwitterArt.postId) {
  604. return
  605. }
  606. if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
  607. bindTwitterArt.isBindIng = true;
  608. reportSrcPublishEvent({
  609. params: {
  610. postId: bindTwitterArt.postId,
  611. }
  612. }).then((res) => {
  613. if (res.code == 0) {
  614. Report.reportLog({
  615. objectType: Report.objectType.tweetPostBinded
  616. });
  617. bindTwitterArt.needBind = false;
  618. bindTwitterArt.postId = '';
  619. bindTwitterArt.isBindIng = false;
  620. }
  621. })
  622. }
  623. }
  624. import parseCard from './ParseCard'
  625. // 检测dom改变
  626. // 获取短链接
  627. // 查看本地是否有postid
  628. // 如果有 修改dom 返回
  629. // 如果没有 网络请求
  630. // 获取postid
  631. // 获取twitterid
  632. // 检测当前所有dom 如果没有
  633. let queue_num = 1
  634. export const changeQueueNum = (num = 0) => {
  635. queue_num = queue_num + num
  636. if (queue_num > 5) {
  637. queue_num = 5
  638. }
  639. }
  640. let main_observer = null
  641. function onChangePageMain(targetNode) {
  642. try {
  643. const config = { attributes: false, childList: true, subtree: true };
  644. const callback = (mutationsList, observer) => {
  645. changeQueueNum(1)
  646. }
  647. main_observer = new MutationObserver(callback);
  648. main_observer.observe(targetNode, config);
  649. } catch (error) {
  650. main_observer = null
  651. }
  652. }
  653. // 1.监听main改变
  654. // 2.监听卡片是否可见
  655. // 3.如果可见了 去找
  656. function setIframeRedPacket(type = 'twitter') {
  657. // 获取所有卡片参数
  658. let card_json_data
  659. switch (type) {
  660. case 'facebook':
  661. card_json_data = parseCard.parseFacebookCardParmas()
  662. for (let i in card_json_data) {
  663. parseCard.replaceFacebookPacket(card_json_data[i])
  664. }
  665. break;
  666. default:
  667. card_json_data = parseCard.parseAllDeNetCardParmas()
  668. // 过滤出可以请求的短链接
  669. parseCard.getCardParmas(card_json_data).then((res) => {
  670. for (let i in res.has_post_Id_card_data) {
  671. let item = res.has_post_Id_card_data[i];
  672. if (item && item.post_Id && item.post_Id.indexOf('nft/') >= 0) {
  673. parseCard.replaceNftDomRedPacket(item)
  674. } else if (item && item.post_Id && item.post_Id.indexOf('nft_group/') >= 0) {
  675. parseCard.replaceNftGroupDomRedPacket(item)
  676. } else if (item && item.post_Id && item.post_Id.indexOf('luckdraw/') >= 0) {
  677. item.post_Id = item.post_Id.split('luckdraw/')[1] || ''
  678. item.page_type = '抽奖'
  679. parseCard.replaceDOMRedPacket(item)
  680. } else {
  681. item.page_type = '红包'
  682. parseCard.replaceDOMRedPacket(item)
  683. }
  684. }
  685. if (res.need_net_short_url.length > 0) {
  686. // 请求短链接
  687. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, (res) => {
  688. if (!res) {
  689. Report.reportLog({
  690. objectType: Report.objectType.parse_card_error
  691. });
  692. }
  693. })
  694. }
  695. })
  696. break;
  697. }
  698. }
  699. // 监听点击发推 上报文案
  700. // document.addEventListener('click', (e) => {
  701. // try {
  702. // let inputEle = document.querySelector('div[contenteditable="true"]');
  703. // if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
  704. // // 获取文案上报
  705. // console.log(inputEle.innerText)
  706. // } else if (e.target.closest('div[data-testid=tweetButton]')) {
  707. // console.log(inputEle.innerText)
  708. // }
  709. // } catch (error) {
  710. // console.error('error', error)
  711. // }
  712. // })
  713. export function initExecuteScript(changes) {
  714. if (changes.executeScript) {
  715. let item = JSON.parse(changes.executeScript.newValue)
  716. if (item.executeScript) {
  717. init()
  718. }
  719. }
  720. if (changes.userInfo) {
  721. let item = JSON.parse(changes.userInfo.newValue)
  722. if (item) {
  723. checkUserJoinGroup(() => {
  724. showNFTGroupIcon()
  725. })
  726. }
  727. }
  728. }
  729. const createNFTIframe = ({ url, id }, callback) => {
  730. let iframe = document.createElement('iframe')
  731. iframe.id = id
  732. iframe.src = url
  733. iframe.style.cssText = 'border:medium none; width:100%; height:100%; z-index:100; position: fixed; top:0;left:0; display:none;';
  734. iframe.onload = () => {
  735. callback && callback()
  736. }
  737. document.body.appendChild(iframe);
  738. }
  739. function initParseCard() {
  740. let timer = setInterval(() => {
  741. let inTwitter = window.location.host.includes('twitter.com');
  742. let inTwitterNode = document.querySelector('main');
  743. let inFacebook = window.location.host.includes('facebook.com');
  744. let inFacebookNode = document.querySelector('#facebook');
  745. if (inTwitter && inTwitterNode) {
  746. clearInterval(timer)
  747. setInterval(() => {
  748. if (!main_observer) {
  749. onChangePageMain(inTwitterNode)
  750. changeQueueNum(1)
  751. }
  752. twitterPinLogin()
  753. showNFTGroupIcon()
  754. if (queue_num <= 0) {
  755. return
  756. }
  757. initGroupTip()
  758. setIframeRedPacket()
  759. checkHasDeBtn()
  760. checkHasSliderDeBtn();
  761. changeQueueNum(-1)
  762. showNFTCard()
  763. addGroupTab();
  764. addJoinedGroupList();
  765. }, 1000)
  766. } else if (inFacebook && inFacebookNode) {
  767. clearInterval(timer)
  768. setInterval(() => {
  769. if (!main_observer) {
  770. onChangePageMain(inFacebookNode)
  771. changeQueueNum(1)
  772. }
  773. if (queue_num <= 0) {
  774. return
  775. }
  776. setIframeRedPacket('facebook')
  777. changeQueueNum(-1)
  778. }, 1000)
  779. }
  780. }, 1000);
  781. }
  782. let inited = false
  783. // 初始化
  784. export function init() {
  785. if (inited) {
  786. return
  787. }
  788. inited = true
  789. console.log('init')
  790. twitterPinLogin()
  791. getDiscordAuthCode();
  792. appendPopupPage();
  793. chrome.runtime.sendMessage({
  794. actionType: "CONTENT_SET_POPUP_CONFIG",
  795. data: {
  796. popup: 'popup.html'
  797. }
  798. }, () => { });
  799. let where = window.location.host.indexOf('twitter.com') < 0 && window.location.host.indexOf('facebook.com') < 0;
  800. if (where) {
  801. return
  802. }
  803. if (window.location.host.includes('twitter.com')) {
  804. showNFTCard()
  805. showNFTGroupIcon()
  806. addEventAction();
  807. checkUserJoinGroup();
  808. renderDom();
  809. checkTwitterTaskState();
  810. initBuyNFT();
  811. addJoinedGroupList();
  812. getSysTheme();
  813. addGroupTab();
  814. }
  815. // 渲染dom
  816. initParseCard()
  817. getChromeStorage("popupShowPublishDialog", (res) => {
  818. console.log("popupShowPublishDialog", res);
  819. if (res && res.show) {
  820. setTimeout(() => {
  821. showTwitterPublishDialogHandler({
  822. srcContent: res.srcContent,
  823. postId: res.postId,
  824. copyContent: res.copyContent || ''
  825. });
  826. }, 1500);
  827. chrome.storage.local.remove("popupShowPublishDialog");
  828. }
  829. });
  830. getChromeStorage("userSettings", (res) => {
  831. setTimeout(() => {
  832. addPinedPop();
  833. if (res && !res.isOnToolbar) {
  834. setTimeout(() => {
  835. showPinTips();
  836. chrome.storage.local.remove("userSettings");
  837. }, 800);
  838. }
  839. }, 800);
  840. });
  841. }
  842. function checkHasSliderDeBtn() {
  843. let deBtn = document.getElementById('de-btn');
  844. let deBtn3 = document.getElementById('de-btn3');
  845. if (!deBtn && !deBtn3) {
  846. addSliderNavDeBtn();
  847. }
  848. }
  849. export function facebookReplyTweet(params) {
  850. if (window.location.origin.indexOf('twitter.com')) {
  851. const urlParams = new URLSearchParams(window.location.search);
  852. const actionType = urlParams.get('actionType');
  853. let deReplyParams = urlParams.get('deReplyParams') || '{}';
  854. deReplyParams = JSON.parse(deReplyParams);
  855. if (actionType == 'denetFacebookToTwitterReply') {
  856. if (params.postId == deReplyParams.postId) {
  857. let iframe = document.getElementById(params.postId);
  858. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_FACEBOOK_REPLY', data: deReplyParams }, '*');
  859. }
  860. }
  861. }
  862. }
  863. export function replyHandle(params) {
  864. let iframe = window.parent.document.getElementById(params.postId);
  865. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  866. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  867. if (replyBtn) {
  868. replyBtn.click();
  869. }
  870. onReplyDialogOpen(params, iframe);
  871. // 详情页推文底部评论处理
  872. let pathNameArr = window.location.pathname.split('/');
  873. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  874. let tweetReply = document.querySelector('div[data-testid="tweetButtonInline"]');
  875. if (tweetReply) {
  876. tweetReply.addEventListener('click', function () {
  877. // 详情页回复按钮点击
  878. Report.reportLog({
  879. pageSource: Report.pageSource.mainPage,
  880. businessType: Report.businessType.buttonClick,
  881. objectType: Report.objectType.replyClickByDetailPage
  882. });
  883. let eleList = tweetReply.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll('span[data-text="true"]');
  884. reportReplyResult(eleList, params, () => {
  885. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  886. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  887. });
  888. })
  889. }
  890. }
  891. }
  892. export function onTweetReplyClick(params) {
  893. let iframe = window.parent.document.getElementById(params.postId);
  894. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  895. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  896. if (replyBtn) {
  897. replyBtn.addEventListener('click', () => {
  898. onReplyDialogOpen(params, iframe);
  899. })
  900. }
  901. }
  902. function onReplyDialogOpen(params, iframe) {
  903. setTimeout(() => {
  904. let dialog = document.querySelector('div[role="dialog"]');
  905. let replyBtn;
  906. if (dialog) {
  907. let dialogContent = dialog.querySelector('div[role="dialog"]');
  908. replyBtn = dialog.querySelector('div[data-testid="toolBar"]').querySelector('div[data-testid="tweetButton"]');
  909. if (dialogContent) {
  910. let width = dialogContent.offsetWidth;
  911. let ele = document.createElement('div');
  912. ele.innerText = 'Tag 3 friends to complete the task';
  913. ele.style.cssText = `width: ${width}px; height: 38px; color: #fff; font-weight: 600;
  914. font-size: 16px; display: flex; align-items: center; justify-content: center; background: #1D9BF0;
  915. opacity: 0.8; position: absolute; top: 18px; left: 50%; transform: translateX(-50%); z-index: 1000`;
  916. dialogContent.style.top = '80px';
  917. dialogContent.parentNode.appendChild(ele);
  918. }
  919. } else {
  920. dialog = document.querySelector('main[role="main"]');
  921. if (dialog) {
  922. replyBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  923. }
  924. }
  925. if (dialog && replyBtn) {
  926. replyBtn.addEventListener('click', function () {
  927. // 推文页回复按钮点击
  928. Report.reportLog({
  929. pageSource: Report.pageSource.mainPage,
  930. businessType: Report.businessType.buttonClick,
  931. objectType: Report.objectType.replyClickByTwitterList
  932. });
  933. let eleList = dialog.querySelector('div[contenteditable="true"]').querySelectorAll('span[data-text="true"]');
  934. reportReplyResult(eleList, params, () => {
  935. // 上報完成
  936. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  937. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  938. })
  939. });
  940. }
  941. }, 1000);
  942. }
  943. const reportReplyResult = throttle(function (eleList, params, cb) {
  944. //未过滤的回复文本
  945. Report.reportLog({
  946. pageSource: Report.pageSource.mainPage,
  947. businessType: Report.businessType.buttonClick,
  948. objectType: Report.objectType.replyInputTextGet
  949. }, {
  950. replyStr: eleList
  951. });
  952. if (eleList && eleList.length) {
  953. let atList = [];
  954. for (let i = 0; i < eleList.length; i++) {
  955. let item = eleList[i];
  956. // 是否有中文
  957. let reg = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
  958. if (item && item.innerText.startsWith('@') && !reg.test(item.innerText)) {
  959. atList.push(item.innerText);
  960. }
  961. }
  962. // 去重过滤文本
  963. atList = Array.from(new Set(atList));
  964. Report.reportLog({
  965. pageSource: Report.pageSource.mainPage,
  966. businessType: Report.businessType.buttonClick,
  967. objectType: Report.objectType.replyFilterTextGet
  968. }, {
  969. replyStr: atList
  970. });
  971. if (atList.length >= 3) {
  972. //真实上报
  973. Report.reportLog({
  974. pageSource: Report.pageSource.mainPage,
  975. businessType: Report.businessType.buttonClick,
  976. objectType: Report.objectType.replyReport
  977. }, {
  978. replyStr: atList
  979. });
  980. fetchAddFinishEvent({
  981. eventType: params.type,
  982. luckdropId: params.taskLuckdropId
  983. }).then(res => {
  984. if (res.code == 0) {
  985. cb && cb();
  986. }
  987. })
  988. }
  989. }
  990. }, 800);
  991. // 根据推特id找到dom,完成任务
  992. export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_type = 'like') {
  993. // 1.根据推特ID寻找推文,获取卡片
  994. // 获取所有卡片参数
  995. let card_json_data = parseCard.parseAllDeNetCardParmas()
  996. let result = card_json_data.filter((item) => { return item.tweet_Id == tweet_Id }) || []
  997. if (result.length < 1) {
  998. return
  999. }
  1000. result = result[0]
  1001. switch (task_type) {
  1002. case 'like':
  1003. // https://twitter.com/intent/retweet?tweet_id=1525900221628223491
  1004. if (result.dom_card) {
  1005. result.dom_card.querySelector('div[data-testid=like]').click()
  1006. result.dom_card.querySelector('iframe').contentWindow.postMessage({ actionType: 'CONTENT_DONE_TASK', task_type, }, '*');
  1007. }
  1008. break;
  1009. case 'retweet':
  1010. if (result.dom_card) {
  1011. result.dom_card.querySelector('div[data-testid=retweet]').click()
  1012. result.dom_card.querySelector('div[data-testid=retweetConfirm]').click()
  1013. }
  1014. break
  1015. case 'follow':
  1016. follow_names.forEach((item) => {
  1017. window.open(`https://twitter.com/intent/follow?screen_name=${item}&tweet_Id=${tweet_Id}`)
  1018. })
  1019. break
  1020. }
  1021. }
  1022. function clickByDataTestId(e, id, callback) {
  1023. if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
  1024. callback()
  1025. } else if (e.target.closest('div[data-testid=' + id + ']')) {
  1026. callback()
  1027. }
  1028. }
  1029. // 校验关注推特状态
  1030. export function checkTwitterTaskState() {
  1031. let task_type = ''
  1032. let url = window.location.href
  1033. let tweet_Id
  1034. let task_data = {
  1035. follow_name: ''
  1036. }
  1037. // 校验当前链接
  1038. if (url.includes('https://twitter.com/intent/retweet')) {
  1039. task_type = 'retweet'
  1040. } else if (url.includes('https://twitter.com/intent/follow')) {
  1041. task_type = 'follow'
  1042. task_data.follow_name = getQueryString('screen_name')
  1043. } else if (url.includes('https://twitter.com/intent/like')) {
  1044. task_type = 'like'
  1045. } else {
  1046. return
  1047. }
  1048. tweet_Id = getQueryString('tweet_id')
  1049. // let root_status
  1050. document.body.addEventListener('click', (e) => {
  1051. // 点击 确认
  1052. clickByDataTestId(e, 'confirmationSheetConfirm', () => {
  1053. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1054. })
  1055. // 点击取消
  1056. clickByDataTestId(e, 'confirmationSheetCancel', () => {
  1057. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  1058. })
  1059. // 点击 蒙层
  1060. if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
  1061. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  1062. }
  1063. }, true)
  1064. let timer = setInterval(() => {
  1065. // 喜欢
  1066. if (document.querySelector('div[data-testid=unlike]') && task_type == 'like') {
  1067. clearInterval(timer)
  1068. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1069. }
  1070. // 转推
  1071. if (document.querySelector('div[data-testid=unretweet]') && task_type == 'retweet') {
  1072. clearInterval(timer)
  1073. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1074. }
  1075. // 关注
  1076. if (task_type == 'follow') {
  1077. let follow_area = document.querySelector('div[data-testid=placementTracking]')
  1078. if (follow_area && follow_area.querySelectorAll('div')) {
  1079. follow_area = follow_area.querySelectorAll('div')
  1080. clearInterval(timer)
  1081. for (let i in follow_area) {
  1082. if (follow_area[i] && follow_area[i].dataset && follow_area[i].dataset.testid && follow_area[i].dataset.testid.indexOf('unfollow') > 0) {
  1083. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1084. break
  1085. }
  1086. }
  1087. }
  1088. }
  1089. }, 1000)
  1090. }
  1091. export function getTweetAuthorByDom(params) {
  1092. let iframe = document.getElementById(params.postId);
  1093. if (!iframe) {
  1094. return;
  1095. }
  1096. let fullNameDom;
  1097. let pathNameArr = window.location.pathname.split('/');
  1098. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  1099. fullNameDom = iframe.parentNode.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1100. } else {
  1101. fullNameDom = iframe.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1102. }
  1103. if (fullNameDom) {
  1104. let arr = fullNameDom.href.split('/');
  1105. if (arr.length) {
  1106. let fullName = arr[arr.length - 1];
  1107. if (fullName) {
  1108. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_GET_TWEET_AUTHOR', data: { fullName } }, '*');
  1109. }
  1110. }
  1111. }
  1112. }
  1113. export function doTaskTwitterAPI({ task_data, task_type }) {
  1114. switch (task_type) {
  1115. case 'like':
  1116. TwitterLikeAPI(task_data.tweet_Id)
  1117. break
  1118. case 'retweet':
  1119. TwitterRetweetAPI(task_data.tweet_Id)
  1120. break
  1121. case 'follow':
  1122. task_data.follow_data.forEach((item) => {
  1123. if (item.name && item.twitterUserId) {
  1124. TwitterFollowAPI(item, task_data.tweet_Id)
  1125. }
  1126. })
  1127. break
  1128. }
  1129. }
  1130. export function showJoinDialog(data) {
  1131. let iframe = document.querySelector('#nftProjectId')
  1132. iframe.style.display = 'block'
  1133. iframe.contentWindow.postMessage({ actionType: 'SHOW_JOIN_DATA', data }, '*');
  1134. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html#/group?params=${JSON.stringify(data)}&time=${new Date().getTime()}`)
  1135. }
  1136. const TwitterFollowAPI = (item, tweet_Id) => {
  1137. fetch("https://twitter.com/i/api/1.1/friendships/create.json", {
  1138. "headers": {
  1139. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1140. "content-type": "application/x-www-form-urlencoded",
  1141. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1142. "sec-ch-ua-mobile": "?0",
  1143. "sec-ch-ua-platform": "\"macOS\"",
  1144. "x-csrf-token": getCookie('ct0'),
  1145. "x-twitter-active-user": "yes",
  1146. "x-twitter-auth-type": "OAuth2Session",
  1147. "x-twitter-client-language": "zh-cn"
  1148. },
  1149. "referrer": "https://twitter.com/home",
  1150. "referrerPolicy": "strict-origin-when-cross-origin",
  1151. "body": "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 + "",
  1152. "method": "POST",
  1153. "mode": "cors",
  1154. "credentials": "include"
  1155. }).then(() => {
  1156. let task_data = {
  1157. follow_name: item.name
  1158. }
  1159. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: true }, () => { })
  1160. }).catch(() => {
  1161. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: false }, () => { })
  1162. })
  1163. }
  1164. const TwitterRetweetAPI = (tweet_Id) => {
  1165. fetch("https://twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet", {
  1166. "headers": {
  1167. "accept": "*/*",
  1168. "accept-language": "zh,en;q=0.9,zh-CN;q=0.8",
  1169. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1170. "content-type": "application/json",
  1171. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1172. "sec-ch-ua-mobile": "?0",
  1173. "sec-ch-ua-platform": "\"macOS\"",
  1174. "sec-fetch-dest": "empty",
  1175. "sec-fetch-mode": "cors",
  1176. "sec-fetch-site": "same-origin",
  1177. "x-csrf-token": getCookie('ct0'),
  1178. "x-twitter-active-user": "yes",
  1179. "x-twitter-auth-type": "OAuth2Session",
  1180. "x-twitter-client-language": "zh-cn"
  1181. },
  1182. "referrer": "https://twitter.com/home",
  1183. "referrerPolicy": "strict-origin-when-cross-origin",
  1184. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\",\"dark_request\":false},\"queryId\":\"ojPdsZsimiJrUGLR1sjUtA\"}",
  1185. "method": "POST",
  1186. "mode": "cors",
  1187. "credentials": "include"
  1188. }).then(() => {
  1189. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: true }, () => { })
  1190. }).catch(() => {
  1191. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: false }, () => { })
  1192. })
  1193. }
  1194. const TwitterLikeAPI = (tweet_Id) => {
  1195. fetch("https://twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet", {
  1196. "headers": {
  1197. "accept": "*/*",
  1198. "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
  1199. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1200. "content-type": "application/json",
  1201. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
  1202. "sec-ch-ua-mobile": "?0",
  1203. "sec-ch-ua-platform": "\"Windows\"",
  1204. "sec-fetch-dest": "empty",
  1205. "sec-fetch-mode": "cors",
  1206. "sec-fetch-site": "same-origin",
  1207. "x-csrf-token": getCookie('ct0'),
  1208. "x-twitter-active-user": "yes",
  1209. "x-twitter-auth-type": "OAuth2Session",
  1210. "x-twitter-client-language": "en"
  1211. },
  1212. "referrer": "https://twitter.com/home",
  1213. "referrerPolicy": "strict-origin-when-cross-origin",
  1214. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\"},\"queryId\":\"lI07N6Otwv1PhnEgXILM7A\"}",
  1215. "method": "POST",
  1216. "mode": "cors",
  1217. "credentials": "include"
  1218. }).then(() => {
  1219. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: true }, () => { })
  1220. }).catch(() => {
  1221. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: false }, () => { })
  1222. })
  1223. }
  1224. export const TwitterApiUserByScreenName = (params) => {
  1225. let { screen_name } = params;
  1226. 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`, {
  1227. headers: {
  1228. "accept": "*/*",
  1229. "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
  1230. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1231. "content-type": "application/json",
  1232. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
  1233. "sec-ch-ua-mobile": "?0",
  1234. "sec-ch-ua-platform": "\"Windows\"",
  1235. "sec-fetch-dest": "empty",
  1236. "sec-fetch-mode": "cors",
  1237. "sec-fetch-site": "same-origin",
  1238. "x-csrf-token": getCookie('ct0'),
  1239. "x-twitter-active-user": "yes",
  1240. "x-twitter-auth-type": "OAuth2Session",
  1241. "x-twitter-client-language": "en"
  1242. },
  1243. }).then(function (response) {
  1244. chrome.runtime.sendMessage({ actionType: "CONTENT_API_GET_TWEET_USER_INFO_RES", data: response.data.data || {} }, () => { })
  1245. }).catch(function (response) {
  1246. });
  1247. }
  1248. let click_old_time = new Date().getTime()
  1249. export const showTwitterPost = (data) => {
  1250. let click_new_time = new Date().getTime()
  1251. if ((click_new_time - click_old_time) < 3000) {
  1252. return
  1253. }
  1254. click_old_time = click_new_time
  1255. showEditTweet(() => {
  1256. clearPostContent(() => {
  1257. contentGetNftPostPre({
  1258. groupId: data.groupId
  1259. })
  1260. })
  1261. })
  1262. // addPublishTipsIframe({ type: 'nft' })
  1263. setGroupTabStatus();
  1264. }
  1265. export function publishNFTTweetEvent({ groupId, postId, srcContent }, callback) {
  1266. setTimeout(() => {
  1267. let publishTweetBtn;
  1268. let dialog = document.querySelector('div[role="dialog"]');
  1269. if (dialog) {
  1270. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  1271. } else {
  1272. let domMain = document.querySelector('main[role="main"]');
  1273. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  1274. }
  1275. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  1276. // 获取文案上报
  1277. publishNFTTweetPost({ postId, srcContent, groupId })
  1278. // 绑定推文id
  1279. bindTwitterArt.needBind = true;
  1280. bindTwitterArt.postId = postId;
  1281. callback && callback()
  1282. jumpTwitterDetailByAlert()
  1283. setTimeout(() => {
  1284. setGroupTabSelfStyle({
  1285. groupColor: 'rgb(83, 100, 113)',
  1286. groupFontWeight: '500',
  1287. lineDisplay: 'none'
  1288. });
  1289. chrome.runtime.sendMessage({
  1290. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1291. data: { type: 'arrow' }
  1292. }, () => { });
  1293. }, 2000);
  1294. });
  1295. }, 800)
  1296. }
  1297. export const publishNFTTweetPost = ({ postId, srcContent, groupId }) => {
  1298. let inputEle = document.querySelector('div[contenteditable="true"]');
  1299. let textContent = inputEle.innerText
  1300. srcContent = srcContent.replaceAll('#DNFT', '')
  1301. srcContent = srcContent.replaceAll('⬇️', '')
  1302. srcContent = srcContent.replaceAll('Join', '')
  1303. srcContent = srcContent.replaceAll('Now:', '')
  1304. let arr = srcContent.split(' ') || []
  1305. arr.forEach((item) => {
  1306. if (textContent.includes(item)) {
  1307. textContent = textContent.replaceAll(item, '')
  1308. }
  1309. })
  1310. textContent = textContent.replaceAll('#DNFT', '')
  1311. textContent = textContent.replaceAll('⬇️', '')
  1312. textContent = textContent.replaceAll('Join', '')
  1313. textContent = textContent.replaceAll('Now:', '')
  1314. textContent = textContent.trim()
  1315. let formData = {
  1316. groupId,
  1317. textContent
  1318. }
  1319. let params = {
  1320. postBizData: JSON.stringify(formData),
  1321. postSrc: 1, //1 twitter
  1322. postType: 2, //2 nft
  1323. postId
  1324. }
  1325. chrome.runtime.sendMessage({
  1326. actionType: "CONTENT_NFT_TXT_PUBLISH",
  1327. data: params
  1328. }, () => { });
  1329. }
  1330. const contentGetNftPostPre = (data) => {
  1331. chrome.runtime.sendMessage({
  1332. actionType: "CONTENT_GET_TWITTER_NFT_POST_PRE",
  1333. data
  1334. }, () => { });
  1335. }
  1336. let tweet_nft_content = {}
  1337. export const setTwitterTextarea = (params, num = 5) => {
  1338. if (num <= 0) {
  1339. return
  1340. }
  1341. tweet_nft_content = params
  1342. let inputEle = document.querySelector('div[contenteditable="true"]');
  1343. if (inputEle) {
  1344. inputEle.focus();
  1345. setGroupIconStatus();
  1346. document.execCommand("insertText", false, '');
  1347. setTimeout(() => {
  1348. document.execCommand("insertText", false, params.srcContent + ' ');
  1349. // 光标定位到起始位置
  1350. setTimeout(() => {
  1351. inputEle.focus();
  1352. let sel = window.getSelection()
  1353. let range = document.createRange()
  1354. range.setStart(inputEle, 0)
  1355. range.setEnd(inputEle, 0)
  1356. sel.removeAllRanges()
  1357. sel.addRange(range);
  1358. }, 0)
  1359. publishNFTTweetEvent(params, bindTwitterArtMethod)
  1360. }, 1000)
  1361. } else {
  1362. setTimeout(() => {
  1363. num--
  1364. setTwitterTextarea(params, num)
  1365. }, 500);
  1366. }
  1367. }
  1368. const initGroupTip = () => {
  1369. let arr = window.location.pathname.split('/') || []
  1370. if (location.pathname == '/compose/tweet') {
  1371. return
  1372. }
  1373. if (!document.querySelector('div[data-testid=UserName]')) {
  1374. return
  1375. }
  1376. if (arr.length >= 2) {
  1377. let twitterAccount = arr[1]
  1378. let iframe_banner = document.querySelector('#denet_group_banner')
  1379. if (iframe_banner) {
  1380. if (twitterAccount != getQueryStringByUrl(iframe_banner.src, 'twitterAccount')) {
  1381. iframe_banner.style.display = 'none'
  1382. // iframe_banner.src = chrome.runtime.getURL(`/iframe/group-card.html?twitterAccount=${twitterAccount}`)
  1383. iframe_banner.contentWindow.postMessage({ actionType: 'SHOW_BANNER', twitterAccount }, '*');
  1384. }
  1385. return
  1386. }
  1387. try {
  1388. let dom = document.querySelector('div[data-testid="ScrollSnap-SwipeableList"]').closest('nav')
  1389. let iframe = document.createElement('iframe')
  1390. iframe.id = 'denet_group_banner'
  1391. iframe.style.cssText = 'border: medium none; display:none; width:100%; height:100px;'
  1392. iframe.src = chrome.runtime.getURL(`/iframe/group-card.html?twitterAccount=${twitterAccount}`)
  1393. // iframe.contentWindow.postMessage({ actionType: 'SHOW_BANNER', twitterAccount }, '*');
  1394. if (dom && !dom.parentNode.children[0].querySelector('iframe')) {
  1395. // dom.parentNode.insertBefore(iframe, dom)
  1396. dom.parentNode.children[0].appendChild(iframe)
  1397. }
  1398. } catch (error) {
  1399. }
  1400. }
  1401. }
  1402. export const showGroupTip = () => {
  1403. let dom_denet_group_banner = document.querySelector('#denet_group_banner')
  1404. dom_denet_group_banner.style.display = 'block';
  1405. groupBtnStyleChange();
  1406. }
  1407. export const hideBuyNFT = () => {
  1408. let iframe = document.querySelector('#nftProjectId')
  1409. iframe.style.display = 'none'
  1410. iframe.src = ''
  1411. }
  1412. export const showBuyNFT = ({ nft_project_Id }) => {
  1413. if (!nft_project_Id) {
  1414. return
  1415. }
  1416. let iframe = document.querySelector('#nftProjectId')
  1417. iframe.style.display = 'block'
  1418. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html#/?nftProjectId=${nft_project_Id}`)
  1419. }
  1420. const initBuyNFT = () => {
  1421. let url = ''
  1422. let id = `nftProjectId`
  1423. createNFTIframe({ url, id })
  1424. }
  1425. export const showNFTCard = () => {
  1426. let urlInfo = new URL(window.location.href)
  1427. let isTwitter = urlInfo.hostname === 'twitter.com'
  1428. let userElem = document.querySelector('div[data-testid="UserName"]');
  1429. let sideElem = document.querySelector('div[data-testid="sidebarColumn"]')
  1430. let tabIndex = sideElem && sideElem.querySelector('div[tabindex="0"]');
  1431. let isAppend = document.querySelector('div[id="de-nft-node"]');
  1432. let where = isTwitter && userElem && tabIndex;
  1433. if (where) {
  1434. let iframe = document.createElement('iframe');
  1435. iframe.src = chrome.runtime.getURL(`/iframe/nft-card.html`)
  1436. iframe.style.cssText = 'border:medium none; width:100%; height:290px;';
  1437. let nftElement = document.createElement('div');
  1438. nftElement.id = 'de-nft-node';
  1439. nftElement.innerHTML = `
  1440. ${iframe.outerHTML}
  1441. <style>
  1442. #de-nft-node {height:290px; margin-bottom:17px; display:none;}
  1443. </style>
  1444. `;
  1445. if (tabIndex && tabIndex.firstChild && tabIndex.firstChild.childNodes && !isAppend) {
  1446. tabIndex.firstChild.insertBefore(nftElement, tabIndex.firstChild.childNodes[2]);
  1447. }
  1448. }
  1449. }
  1450. export const showNFTSale = () => {
  1451. document.querySelector('div[id="de-nft-node"]').style.display = 'block';
  1452. }
  1453. export const addEventAction = () => {
  1454. let urlInfo = new URL(window.location.href)
  1455. let isTwitter = urlInfo.hostname === 'twitter.com'
  1456. // 页面滚动
  1457. if (isTwitter) {
  1458. // 首页
  1459. if (urlInfo.pathname === '/home') {
  1460. window.addEventListener('scroll', () => {
  1461. hideNFTGroupList()
  1462. })
  1463. }
  1464. }
  1465. }
  1466. export const appendPopupPage = (params = {}) => {
  1467. let { path = '' } = params;
  1468. let iframe = document.createElement('iframe');
  1469. iframe.id = 'de-popup-page';
  1470. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1471. iframe.style.cssText = `border: medium none; width: 375px !important;
  1472. 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;
  1473. animation-duration: 0.5s !important;
  1474. animation-timing-function: ease-in-out !important;
  1475. animation-fill-mode: forwards !important;
  1476. transition: all 1s ease 0s !important;right: -385px;transform: translateX(385px);`
  1477. let iframeContent = document.getElementById('de-popup-page');
  1478. let overlayDom = document.createElement('div');
  1479. overlayDom.id = 'de-popup-overlay';
  1480. overlayDom.style.cssText = `position: fixed;z-index: 88888;top: 0;
  1481. left: 0;width: 100%;height: 100%;opacity: 0;display: none`;
  1482. let overlay = document.getElementById('de-popup-overlay');
  1483. let body = document.querySelector('body');
  1484. if (!iframeContent && body) {
  1485. document.querySelector('body').appendChild(iframe);
  1486. if (!overlay) {
  1487. document.querySelector('body').appendChild(overlayDom);
  1488. overlayDom.addEventListener('click', function () {
  1489. hidePopupPage();
  1490. })
  1491. } else {
  1492. overlay.addEventListener('click', function () {
  1493. hidePopupPage();
  1494. })
  1495. }
  1496. }
  1497. }
  1498. let showPopupPageFrom = '';
  1499. export const showPopupPage = (params = {}) => {
  1500. let { path = '', from, showJoinGroupFinish = false } = params;
  1501. showPopupPageFrom = from;
  1502. hidePinTips();
  1503. hideNoticeBindTweet();
  1504. let iframe = document.getElementById('de-popup-page');
  1505. if (!iframe) {
  1506. appendPopupPage();
  1507. iframe = document.getElementById('de-popup-page');
  1508. }
  1509. if (iframe) {
  1510. if (path) {
  1511. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1512. }
  1513. iframe.style.transform = 'translateX(-' + 395 + 'px)';
  1514. chrome.runtime.sendMessage({
  1515. actionType: "CONTENT_POPUP_PAGE_SHOW",
  1516. data: {
  1517. path,
  1518. showJoinGroupFinish,
  1519. }
  1520. }, () => { });
  1521. chrome.runtime.sendMessage({
  1522. actionType: "CONTENT_SET_POPUP_CONFIG",
  1523. data: {
  1524. popup: ''
  1525. }
  1526. }, () => { });
  1527. let overlay = document.getElementById('de-popup-overlay');
  1528. overlay.style.display = 'block';
  1529. let htmlDom = document.querySelector('html');
  1530. if (htmlDom) {
  1531. htmlDom.style.overflowY = 'hidden';
  1532. }
  1533. }
  1534. }
  1535. export const hidePopupPage = () => {
  1536. let iframe = document.getElementById('de-popup-page');
  1537. if (iframe) {
  1538. iframe.style.transform = 'translateX(' + 385 + 'px)';
  1539. let overlay = document.getElementById('de-popup-overlay');
  1540. overlay.style.display = 'none';
  1541. let htmlDom = document.querySelector('html');
  1542. if (htmlDom) {
  1543. htmlDom.style.overflowY = 'auto';
  1544. }
  1545. chrome.runtime.sendMessage({
  1546. actionType: "CONTENT_SET_POPUP_CONFIG",
  1547. data: {
  1548. popup: 'popup.html'
  1549. }
  1550. }, () => { });
  1551. if (showPopupPageFrom == 'BUY_NFT_FINISH') {
  1552. chrome.runtime.sendMessage({
  1553. actionType: "CONTENT_GET_PINED",
  1554. data: {}
  1555. }, () => { });
  1556. showPopupPageFrom = '';
  1557. }
  1558. }
  1559. }
  1560. export const tiggerInjectPopupPage = () => {
  1561. let iframeContent = document.getElementById('de-popup-page');
  1562. if (iframeContent) {
  1563. hidePinTips();
  1564. hideNoticeBindTweet();
  1565. let { transform = '' } = iframeContent.style;
  1566. if (transform == 'translateX(385px)' || !transform) {
  1567. showPopupPage();
  1568. } else {
  1569. hidePopupPage();
  1570. }
  1571. } else {
  1572. appendPopupPage();
  1573. setTimeout(() => {
  1574. let iframe = document.getElementById('de-popup-page');
  1575. let { transform = '' } = iframe.style;
  1576. if (transform == 'translateX(385px)' || !transform) {
  1577. showPopupPage();
  1578. } else {
  1579. hidePopupPage();
  1580. }
  1581. }, 300)
  1582. }
  1583. }
  1584. const onBodyClick = () => {
  1585. if (window.location.href.indexOf('api.twitter.com') < 0) {
  1586. document.querySelector('body').addEventListener('click', function () {
  1587. console.log('click')
  1588. // hidePopupPage();
  1589. })
  1590. }
  1591. }
  1592. export const setPopupConfByPopupPage = () => {
  1593. let iframe = document.getElementById('de-popup-page');
  1594. if (iframe) {
  1595. let { transform = '' } = iframe.style;
  1596. if (transform && transform == 'translateX(-395px)') {
  1597. chrome.runtime.sendMessage({
  1598. actionType: "CONTENT_SET_POPUP_CONFIG",
  1599. data: {
  1600. popup: ''
  1601. }
  1602. }, () => { });
  1603. } else {
  1604. chrome.runtime.sendMessage({
  1605. actionType: "CONTENT_SET_POPUP_CONFIG",
  1606. data: {
  1607. popup: 'popup.html'
  1608. }
  1609. }, () => { });
  1610. }
  1611. } else {
  1612. chrome.runtime.sendMessage({
  1613. actionType: "CONTENT_SET_POPUP_CONFIG",
  1614. data: {
  1615. popup: 'popup.html'
  1616. }
  1617. }, () => { });
  1618. }
  1619. }
  1620. /**
  1621. *
  1622. * Group Tab List Start
  1623. */
  1624. /**
  1625. *
  1626. * 创建 Group Tab
  1627. */
  1628. const createGroupTabNode = () => {
  1629. let groupIcon = document.createElement('img');
  1630. groupIcon.id = 'de-group-tab-icon'
  1631. groupIcon.src = require("@/assets/img/icon-group-tab-item.png");
  1632. groupIcon.style.cssText = 'width:20px;height: 20px;margin-right:4px;';
  1633. let divNode = document.createElement('div');
  1634. divNode.style.cssText = 'display: flex; align-items: center;height: 100%';
  1635. divNode.appendChild(groupIcon);
  1636. divNode.appendChild(document.createTextNode('Group'));
  1637. let lineDom = document.createElement('div');
  1638. lineDom.id = 'de-tab-line';
  1639. lineDom.style.cssText = `border-radius: 9999px;
  1640. position: absolute;
  1641. bottom: 0px;
  1642. min-width: 56px;
  1643. align-self: center;
  1644. height: 4px;
  1645. background-color: rgb(29, 155, 240);
  1646. display: none`;
  1647. let groupTab = document.createElement('div');
  1648. groupTab.id = 'de-nav-tab-group';
  1649. groupTab.style.cssText = `z-index: 1;
  1650. position: relative;
  1651. display: flex;
  1652. min-width: 56px;
  1653. -webkit-box-pack: center;
  1654. justify-content: center;
  1655. -webkit-box-align: center;
  1656. align-items: center;
  1657. text-align: center;
  1658. padding: 0px 16px;
  1659. color: rgb(83, 100, 113);
  1660. font-weight: 700;
  1661. height: 53px;
  1662. cursor: pointer;
  1663. font: 500 15px / 20px TwitterChirp, -apple-system, "system-ui", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;`;
  1664. groupTab.appendChild(divNode);
  1665. groupTab.appendChild(lineDom);
  1666. return groupTab;
  1667. }
  1668. const groupBtnStyleChange = () => {
  1669. let tab = getGroupTabNode();
  1670. if (tab) {
  1671. let line = tab.querySelector('#de-tab-line');
  1672. if (line) {
  1673. let { display } = line.style;
  1674. if (display != 'none') {
  1675. chrome.runtime.sendMessage({
  1676. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1677. data: { type: 'btn' }
  1678. }, () => { });
  1679. }
  1680. }
  1681. }
  1682. }
  1683. const getGroupTabNode = () => {
  1684. let tab = document.querySelector('#de-nav-tab-group');
  1685. return tab;
  1686. }
  1687. const addGroupTab = () => {
  1688. if (!document.querySelector('div[data-testid=UserName]')) {
  1689. return
  1690. }
  1691. let tabListDom = document.querySelector('div[role="tablist"]');
  1692. let groupItemTab = getGroupTabNode();
  1693. if (tabListDom && !groupItemTab) {
  1694. let groupTab = createGroupTabNode();
  1695. tabListDom.appendChild(groupTab);
  1696. groupTab.addEventListener('mouseenter', function () {
  1697. groupTab.style.background = 'rgba(15, 20, 25, 0.1)'
  1698. });
  1699. groupTab.addEventListener('mouseleave', function () {
  1700. groupTab.style.background = 'none'
  1701. });
  1702. setTimeout(() => {
  1703. let count = 0;
  1704. hiddenMaskWeb3Tab(count);
  1705. }, 1300)
  1706. addGroupTabEventListener();
  1707. }
  1708. removeTweetTabEvent({
  1709. tabListDom
  1710. });
  1711. addTweetTabEventListener({
  1712. tabListDom
  1713. });
  1714. addTabGroupContent(() => {
  1715. checkNeedSelectGroupTab();
  1716. });
  1717. }
  1718. const hiddenMaskWeb3Tab = (count) => {
  1719. setTimeout(() => {
  1720. count++;
  1721. if (count < 6) {
  1722. let tab = getMaskWeb3Tab();
  1723. if (tab) {
  1724. tab.style.display = 'none'
  1725. } else {
  1726. hiddenMaskWeb3Tab(count);
  1727. }
  1728. }
  1729. }, 1000);
  1730. }
  1731. /**
  1732. * 跳转到个人主页 检查是否需要选中 Group tab
  1733. */
  1734. const checkNeedSelectGroupTab = () => {
  1735. if (window.location.pathname != '/home') {
  1736. setTimeout(() => {
  1737. getChromeStorage('groupTabData', (res) => {
  1738. console.log('groupTabData', res);
  1739. if (res && res.deTabVal == 'deGroupTab') {
  1740. chrome.storage.local.remove("groupTabData");
  1741. setTimeout(() => {
  1742. selectGroupTab();
  1743. }, 300)
  1744. }
  1745. })
  1746. }, 1300)
  1747. }
  1748. }
  1749. /** 选中 Group tab */
  1750. export const selectGroupTab = () => {
  1751. let groupTab = getGroupTabNode();
  1752. if (groupTab) {
  1753. groupTab.click();
  1754. }
  1755. };
  1756. export const groupTipsSelectGroupTab = (params = {}) => {
  1757. if (params.type == 'btn') {
  1758. let groupTab = getGroupTabNode();
  1759. if (groupTab) {
  1760. let line = groupTab.querySelector('#de-tab-line');
  1761. if (line) {
  1762. let { display } = line.style;
  1763. if (display == 'none') {
  1764. groupTab.click();
  1765. }
  1766. }
  1767. }
  1768. } else {
  1769. selectGroupTab();
  1770. }
  1771. }
  1772. /**
  1773. *
  1774. * Group tab点击事件监听
  1775. */
  1776. const addGroupTabEventListener = () => {
  1777. let groupTab = getGroupTabNode();
  1778. groupTab.addEventListener('click', function () {
  1779. let groupColor = systemInfo.theme == 'light' ? 'rgb(15, 20, 25)' : '#fff';
  1780. setGroupTabSelfStyle({
  1781. groupColor: groupColor,
  1782. groupFontWeight: '700',
  1783. lineDisplay: 'block'
  1784. });
  1785. setTweetActiveTabStyle({
  1786. color: 'rgb(83, 100, 113)',
  1787. display: 'none'
  1788. });
  1789. setTabContentStyle({
  1790. tweetTabContentDisply: 'none',
  1791. iframeContentDisplay: 'block'
  1792. });
  1793. refreshTabGroup();
  1794. window.addEventListener('scroll', addPageScrollEvent);
  1795. let tipsDom = document.querySelector('#denet_group_banner');
  1796. if (tipsDom) {
  1797. chrome.runtime.sendMessage({
  1798. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1799. data: { type: 'btn' }
  1800. }, () => { });
  1801. } else {
  1802. onShowGroupBanner();
  1803. }
  1804. })
  1805. }
  1806. const onShowGroupBanner = () => {
  1807. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  1808. switch (req.actionType) {
  1809. case 'IFRAME_SHOW_GROUP_TIP':
  1810. chrome.runtime.sendMessage({
  1811. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1812. data: { type: 'btn' }
  1813. }, () => { });
  1814. if (!getGroupTabNode()) {
  1815. addGroupTab();
  1816. }
  1817. break
  1818. }
  1819. })
  1820. }
  1821. const addPageScrollEvent = () => {
  1822. let wrapperDom = document.querySelector('html');
  1823. let contentDom = document.querySelector('main[role="main"]');
  1824. let data = {
  1825. wrapperHeight: wrapperDom.offsetHeight,
  1826. wrapperScrollTop: wrapperDom.scrollTop,
  1827. contentHeight: contentDom.offsetHeight
  1828. }
  1829. chrome.runtime.sendMessage({
  1830. actionType: "CONTENT_GROUP_LIST_SCROLL",
  1831. data: data
  1832. }, () => { });
  1833. };
  1834. const removeTweetTabEvent = (params) => {
  1835. let { tabListDom } = params;
  1836. if (tabListDom) {
  1837. let tweetTabItem = tabListDom.querySelectorAll('div[role="presentation"]');
  1838. if (tweetTabItem.length) {
  1839. for (let i = 0; i < tweetTabItem.length; i++) {
  1840. let item = tweetTabItem[i];
  1841. item.removeEventListener('click', TweetTabEventHandler)
  1842. }
  1843. }
  1844. }
  1845. }
  1846. /**
  1847. *
  1848. * twitter tab点击事件监听
  1849. */
  1850. const addTweetTabEventListener = (params) => {
  1851. let { tabListDom } = params;
  1852. let groupItemTab = getGroupTabNode();
  1853. if (tabListDom && groupItemTab) {
  1854. // 监听twitter tab点击事件
  1855. let tweetTabItem = tabListDom.querySelectorAll('div[role="presentation"]');
  1856. if (tweetTabItem.length) {
  1857. for (let i = 0; i < tweetTabItem.length; i++) {
  1858. let item = tweetTabItem[i];
  1859. item.addEventListener('click', TweetTabEventHandler)
  1860. }
  1861. }
  1862. }
  1863. }
  1864. const TweetTabEventHandler = () => {
  1865. window.removeEventListener('scroll', addPageScrollEvent);
  1866. setGroupTabSelfStyle({
  1867. groupColor: 'rgb(83, 100, 113)',
  1868. groupFontWeight: '500',
  1869. lineDisplay: 'none'
  1870. });
  1871. setTabContentStyle({
  1872. tweetTabContentDisply: 'block',
  1873. iframeContentDisplay: 'none'
  1874. });
  1875. setTweetActiveTabStyle({
  1876. color: 'rgb(15, 20, 25)',
  1877. display: 'block'
  1878. });
  1879. chrome.runtime.sendMessage({
  1880. actionType: "SWITCH_GROUP_BANNER_STATUS",
  1881. data: { type: 'arrow' }
  1882. }, () => { });
  1883. };
  1884. /**
  1885. * 设置 Group Tab 样式
  1886. * */
  1887. const setGroupTabSelfStyle = (params = {}) => {
  1888. let { groupColor, groupFontWeight, lineDisplay } = params;
  1889. let groupTab = getGroupTabNode();
  1890. if (groupTab) {
  1891. groupTab.style.color = groupColor;
  1892. groupTab.style.fontWeight = groupFontWeight;
  1893. let lineDom = groupTab.querySelector('#de-tab-line');
  1894. if (lineDom) {
  1895. lineDom.style.display = lineDisplay;
  1896. }
  1897. }
  1898. };
  1899. /**
  1900. * 切换到 Group tab时 刷新列表
  1901. */
  1902. export const refreshTabGroup = () => {
  1903. chrome.runtime.sendMessage({
  1904. actionType: "CONTENT_REFRESH_TAB_GROUP_LIST",
  1905. data: {}
  1906. }, () => { });
  1907. }
  1908. /**
  1909. *
  1910. * tab选中时设置 激活 的字体样式和选中条
  1911. */
  1912. const setTweetActiveTabStyle = (params) => {
  1913. let { color, display } = params || {};
  1914. let tab = document.querySelector('a[aria-selected="true"]');
  1915. if (tab) {
  1916. let tweetActiveTab = tab.querySelector('div');
  1917. if (tweetActiveTab) {
  1918. tweetActiveTab.style.color = color;
  1919. let tweetTabLine = tweetActiveTab.querySelector('div');
  1920. if (tweetTabLine) {
  1921. tweetTabLine.style.display = display;
  1922. }
  1923. }
  1924. }
  1925. }
  1926. /**
  1927. *
  1928. * 设置 tab 切换时 tab内容的样式(显示隐藏)
  1929. */
  1930. const setTabContentStyle = (params) => {
  1931. let { tweetTabContentDisply, iframeContentDisplay } = params;
  1932. let tweetTabContent = getTweetTabContent();
  1933. if (tweetTabContent) {
  1934. if (tweetTabContentDisply == 'block') {
  1935. let { visibility } = tweetTabContent.style;
  1936. if (visibility == 'hidden') {
  1937. tweetTabContent.style.visibility = 'visible';
  1938. tweetTabContent.style.height = 'auto';
  1939. tweetTabContent.style.overflow = 'auto';
  1940. }
  1941. } else {
  1942. tweetTabContent.style.visibility = 'hidden';
  1943. tweetTabContent.style.height = '0px';
  1944. tweetTabContent.style.overflow = 'hidden';
  1945. tweetTabContent.style.margin = '0';
  1946. }
  1947. }
  1948. let iframeContent = getGroupTabContentNode();
  1949. if (!iframeContent) {
  1950. addTabGroupContent();
  1951. }
  1952. setTimeout(() => {
  1953. iframeContent = getGroupTabContentNode();
  1954. if (iframeContent) {
  1955. iframeContent.style.display = iframeContentDisplay;
  1956. }
  1957. })
  1958. };
  1959. /**
  1960. *
  1961. * 获取 twitter tab 下的内容
  1962. */
  1963. const getTweetTabContent = () => {
  1964. let tweetTabContent = document.querySelector('[data-testid="primaryColumn"] [role="navigation"] + * > div[aria-label]:not([role="progressbar"])') || document.querySelector('div[data-testid="emptyState"]');
  1965. return tweetTabContent;
  1966. }
  1967. /**
  1968. * 注入 Group List 内容
  1969. */
  1970. const addTabGroupContent = (cb) => {
  1971. let params = {
  1972. windowLocation: window.location
  1973. }
  1974. let iframe = document.createElement('iframe');
  1975. iframe.id = 'de-tab-group-content';
  1976. iframe.src = chrome.runtime.getURL('/iframe/tab-group.html') + `?params=${JSON.stringify(params)}`;
  1977. iframe.style.cssText = `border: medium none; height: 500px;display: none`
  1978. let iframeContent = getGroupTabContentNode();
  1979. let tweetTabContent = getTweetTabContent();
  1980. if (!iframeContent) {
  1981. if (tweetTabContent && tweetTabContent.parentElement) {
  1982. tweetTabContent.parentElement.appendChild(iframe);
  1983. cb && cb();
  1984. }
  1985. }
  1986. };
  1987. const getGroupTabContentNode = () => {
  1988. let content = document.getElementById('de-tab-group-content');
  1989. return content;
  1990. }
  1991. const setGroupTabStatus = () => {
  1992. let groupTab = getGroupTabNode();
  1993. if (groupTab) {
  1994. let line = groupTab.querySelector('#de-tab-line');
  1995. if (line) {
  1996. let { display } = line.style;
  1997. if (display != 'none') {
  1998. groupTab.click();
  1999. }
  2000. }
  2001. } else {
  2002. setTimeout(() => {
  2003. addGroupTab();
  2004. }, 2000)
  2005. }
  2006. clearInterval(fixProfileTabAutoTimer);
  2007. fixProfileTabAutoSwitch();
  2008. };
  2009. const fixProfileTabAutoSwitch = () => {
  2010. fixProfileTabAutoTimer = setInterval(() => {
  2011. let groupTab = getGroupTabNode();
  2012. let tweetTab = document.querySelector('a[aria-selected="true"]');
  2013. if (groupTab) {
  2014. let line = groupTab.querySelector('#de-tab-line');
  2015. if (line) {
  2016. let { display } = line.style;
  2017. if (display != 'none' && tweetTab) {
  2018. let groupContent = getGroupTabContentNode();
  2019. if (groupContent) {
  2020. let tweetTabContent = getTweetTabContent();
  2021. let { visibility } = tweetTabContent.style;
  2022. let { display } = groupContent.style;
  2023. if (display == 'block' && visibility != 'hidden') {
  2024. groupTab.click();
  2025. }
  2026. }
  2027. }
  2028. }
  2029. }
  2030. }, 1000)
  2031. }
  2032. /**
  2033. *
  2034. * 设置Tab Group Iframe 样式
  2035. */
  2036. export const setTabGroupIframeStyle = (params) => {
  2037. let iframeContent = getGroupTabContentNode();
  2038. if (iframeContent) {
  2039. let htmlHeight = document.querySelector('html').offsetHeight;
  2040. let primaryColumnHeightn = document.querySelector('div[data-testid="primaryColumn"]').offsetHeight;
  2041. let height = primaryColumnHeightn > htmlHeight ? primaryColumnHeightn : htmlHeight;
  2042. iframeContent.style.height = height + 'px';
  2043. }
  2044. }
  2045. /**
  2046. * mask web3 Tab
  2047. *
  2048. */
  2049. const getMaskWeb3Tab = () => {
  2050. let tab = document.querySelector('div[data-testid="ScrollSnap-nextButtonWrapper"] + span');
  2051. return tab;
  2052. }
  2053. export const pageJumpHandler = (params) => {
  2054. let { url } = params
  2055. if (url) {
  2056. window.open(url)
  2057. }
  2058. }
  2059. export const getTweetProfileNavTop = (params) => {
  2060. let top = document.querySelector('div[role="tablist"]').closest('nav').getBoundingClientRect().top;
  2061. chrome.runtime.sendMessage({
  2062. actionType: "CONTENT_SEND_GROUP_NAV_TOP", data: {
  2063. top,
  2064. scrollTop: params.scrollTop
  2065. }
  2066. }, () => { })
  2067. }
  2068. export const setGroupInfo = (params = {}) => {
  2069. tweetAccountBindGroupInfo.groupInfo = params;
  2070. let groupTab = getGroupTabNode();
  2071. if (!params.nftGroupId) {
  2072. let { pathname = '' } = window.location;
  2073. if (pathname == "/compose/tweet") {
  2074. return;
  2075. };
  2076. if (groupTab) {
  2077. groupTab.style.display = 'none';
  2078. } else {
  2079. setTimeout(() => {
  2080. groupTab = getGroupTabNode();
  2081. if (groupTab) {
  2082. groupTab.style.display = 'none';
  2083. }
  2084. }, 800)
  2085. }
  2086. } else {
  2087. if (groupTab) {
  2088. let { display } = groupTab.style;
  2089. if (display == 'none') {
  2090. groupTab.style.display = 'block';
  2091. }
  2092. }
  2093. }
  2094. }
  2095. const getSysTheme = () => {
  2096. const themeMedia = window.matchMedia("(prefers-color-scheme: light)");
  2097. if (themeMedia.matches) {
  2098. systemInfo.theme = 'light'
  2099. } else {
  2100. systemInfo.theme = 'dark'
  2101. }
  2102. themeMedia.addListener(e => {
  2103. addGroupTab()
  2104. if (e.matches) {
  2105. systemInfo.theme = 'light'
  2106. } else {
  2107. systemInfo.theme = 'dark'
  2108. }
  2109. });
  2110. }
  2111. /**
  2112. *
  2113. * Group Tab List End
  2114. *
  2115. */
  2116. export const loginSuccessHandle = () => {
  2117. // 检查是否漏出group图标
  2118. checkUserJoinGroup(() => {
  2119. showNFTGroupIcon()
  2120. addEventAction()
  2121. addJoinedGroupList();
  2122. })
  2123. }