twitter.js 97 KB

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