twitter.js 70 KB

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