twitter.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. import { getChromeStorage, setChromeStorage } from '@/uilts/chromeExtension.js'
  2. import { throttle, getQueryString, getCookie, nextTick } from '@/uilts/help'
  3. import { discordAuthRedirectUri } from '@/http/configAPI'
  4. import { reportSrcPublishEvent } from '@/http/publishApi'
  5. import Report from "@/log-center/log"
  6. import { fetchAddFinishEvent } from '@/logic/background/fetch/facebook';
  7. import { showNFTGroupIcon, hideNFTGroupList, checkUserJoinGroup } from '@/logic/content/nft';
  8. let dom = {};
  9. function twitterPinLogin() {
  10. if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
  11. let code = document.querySelector('code')
  12. if (code) {
  13. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_CODE", code: code.innerText }, () => { })
  14. // port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
  15. }
  16. }
  17. }
  18. function getDiscordAuthCode() {
  19. if (window.location.href.indexOf(discordAuthRedirectUri) > -1) {
  20. const urlParams = new URLSearchParams(window.location.search);
  21. const code = urlParams.get('code');
  22. if (code) {
  23. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_DISCORD_AUTH_CODE", code }, () => { })
  24. }
  25. }
  26. };
  27. /**
  28. * 渲染要插入的dom,初始化逻辑
  29. * @param port
  30. */
  31. function renderDom() {
  32. if (window.location.href.indexOf('https://twitter.com') > -1) {
  33. _createBtnDom();
  34. onWindowResize();
  35. checkHasDeBtn();
  36. setTimeout(() => {
  37. _addIframe();
  38. _addDeNetBtn();
  39. _getSliderTwitterBtn();
  40. }, 800)
  41. }
  42. }
  43. /**
  44. * 展示give弹窗
  45. */
  46. export function showGiveDialogHandler(userInfo) {
  47. let iframe = document.getElementById('iframe-content');
  48. if (iframe) {
  49. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  50. } else {
  51. _addIframe();
  52. let iframe = document.getElementById('iframe-content');
  53. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  54. }
  55. }
  56. export function showIframeHandler() {
  57. document.getElementById('iframe-content').style.display = 'block';
  58. }
  59. export function hideIframeHandler() {
  60. document.getElementById('iframe-content').style.display = 'none';
  61. }
  62. /**
  63. * 展示twitter原生发布框
  64. */
  65. let tweetPublishStore = {
  66. showPublishDialog: false
  67. }
  68. export function showTwitterPublishDialogHandler(publishRes) {
  69. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  70. if (bigBtn) {
  71. bigBtn.click();
  72. tweetPublishStore.showPublishDialog = true;
  73. } else {
  74. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  75. smallBtn && smallBtn.click();
  76. tweetPublishStore.showPublishDialog = true;
  77. }
  78. setChromeStorage({ publishData: JSON.stringify(publishRes) })
  79. addPublishTipsIframe({
  80. srcContent: publishRes.copyContent
  81. })
  82. _setPublishContent(publishRes.srcContent);
  83. _publishTweetEvent(publishRes, bindTwitterArtMethod);
  84. }
  85. export function twitterPublishHandler(res) {
  86. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  87. if (bigBtn) {
  88. bigBtn.click();
  89. } else {
  90. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  91. smallBtn && smallBtn.click();
  92. }
  93. nextTick(() => {
  94. document.execCommand('selectAll');
  95. }, 100).then(() => {
  96. _setPublishContent(res.srcContent, 500);
  97. })
  98. }
  99. export function showPinTips() {
  100. hidePopupPage();
  101. hideNoticeBindTweet();
  102. getChromeStorage('pinData', (res) => {
  103. if (!res || res.show) {
  104. let domPop = document.getElementById('de-pin-pop');
  105. domPop.style.display = 'block';
  106. }
  107. })
  108. }
  109. export function hidePinTips() {
  110. let pop = document.querySelector('#de-pin-pop');
  111. if (pop) {
  112. pop.style.display = 'none';
  113. }
  114. }
  115. function addPinedPop() {
  116. let domPop = document.getElementById('de-pin-pop');
  117. if (domPop) {
  118. return;
  119. }
  120. let popWrapper = document.createElement('div');
  121. 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';
  122. popWrapper.id = 'de-pin-pop'
  123. let img = document.createElement('img');
  124. img.src = require("@/assets/img/img-pined-guide.png");
  125. img.width = 253;
  126. let contentDom = document.createElement('div');
  127. 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>"
  128. popWrapper.appendChild(img);
  129. popWrapper.appendChild(contentDom);
  130. document.querySelector('body').appendChild(popWrapper);
  131. let deCheck = document.querySelector('#de-check');
  132. let deRemind = document.querySelector('#de-remind');
  133. deCheck.onclick = function (e) {
  134. e && e.stopPropagation && e.stopPropagation();
  135. setChromeStorage({ pinData: JSON.stringify({ show: !this.checked }) })
  136. }
  137. deRemind.onclick = function () {
  138. deCheck.checked = !deCheck.checked;
  139. setChromeStorage({ pinData: JSON.stringify({ show: !deCheck.checked }) })
  140. }
  141. document.querySelector('.de-pin-skip').onclick = function () {
  142. document.querySelector('#de-pin-pop').style.display = 'none';
  143. }
  144. }
  145. function getUserInfo(cb) {
  146. getChromeStorage('userInfo', (res) => {
  147. cb && cb(res);
  148. })
  149. }
  150. // 绑定推文id所需参数
  151. let bindTwitterArt = {
  152. needBind: false,
  153. postId: '',
  154. isBindIng: false
  155. };
  156. /**
  157. * 监听dialog内点击原生发布按钮事件
  158. * @private
  159. */
  160. function _publishTweetEvent(params, cb) {
  161. setTimeout(() => {
  162. let publishTweetBtn;
  163. let dialog = document.querySelector('div[role="dialog"]');
  164. if (dialog) {
  165. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  166. onClosePublishDialogHandle(dialog, params)
  167. } else {
  168. let domMain = document.querySelector('main[role="main"]');
  169. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  170. }
  171. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  172. bindTwitterArt.needBind = true;
  173. bindTwitterArt.postId = params.postId;
  174. tweetPublishStore.showPublishDialog = false;
  175. // checkIsShowReSend(dialog, params);
  176. cb && cb()
  177. });
  178. }, 800)
  179. }
  180. function onClosePublishDialogHandle(dom, params) {
  181. dom.querySelector('div[role="group"]').addEventListener('click', function () {
  182. setTimeout(() => {
  183. let parent = document.querySelector('div[data-testid="confirmationSheetDialog"]');
  184. if (parent) {
  185. let btnArr = parent.querySelectorAll('div[role=button]')
  186. for (let i = 0; i < btnArr.length; i++) {
  187. let btn = btnArr[i];
  188. btn.addEventListener('click', function () {
  189. tweetPublishStore.showPublishDialog = false;
  190. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  191. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  192. })
  193. }
  194. } else {
  195. setTimeout(() => {
  196. let dialog = document.querySelector('div[role="dialog"]');
  197. if (!dialog) {
  198. tweetPublishStore.showPublishDialog = false;
  199. }
  200. }, 800)
  201. }
  202. }, 1000)
  203. })
  204. }
  205. function checkIsShowReSend(dom, params) {
  206. let str = dom.querySelector('div[data-contents="true"]').innerHTML;
  207. if (str.indexOf(params.postId) < 0) {
  208. let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
  209. noticeBindTweet({ postId: params.postId, taskLuckdropId });
  210. }
  211. }
  212. /**
  213. * 在输入推文区插入deNet按钮
  214. * @param parent
  215. * @param dom
  216. * @param isClick
  217. * @private
  218. */
  219. function _addDeNetEditBtn(parent, dom, isClick = false) {
  220. setTimeout(() => {
  221. if (parent && parent.parentNode) {
  222. Report.reportLog({
  223. pageSource: Report.pageSource.mainPage,
  224. businessType: Report.businessType.buttonView,
  225. objectType: Report.objectType.buttonSecond
  226. });
  227. let innerDeIcon = document.getElementById('de-btn1');
  228. if (!innerDeIcon) {
  229. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  230. }
  231. } else {
  232. setTimeout(() => {
  233. parent = _getScheduleDom(isClick);
  234. if (parent && parent.parentNode) {
  235. Report.reportLog({
  236. pageSource: Report.pageSource.mainPage,
  237. businessType: Report.businessType.buttonView,
  238. objectType: Report.objectType.buttonSecond
  239. });
  240. let innerDeIcon = document.getElementById('de-btn1');
  241. if (!innerDeIcon) {
  242. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  243. }
  244. }
  245. }, 1000)
  246. }
  247. })
  248. }
  249. /**
  250. * 在dialog插入deNet按钮
  251. * @private
  252. */
  253. // function _addDeNetBtnToDialog() {
  254. // setTimeout(() => {
  255. // let dialogScheduleBtn = _getScheduleDom(true);
  256. // _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  257. // }, 800)
  258. // }
  259. /**
  260. * 获取左侧twitter按钮
  261. * @private
  262. */
  263. function _getSliderTwitterBtn() {
  264. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  265. dom.tweetBtn.addEventListener('click', function () {
  266. // _addDeNetBtnToDialog();
  267. })
  268. }
  269. /**
  270. * 添加deNet按钮
  271. * @private
  272. */
  273. function _addDeNetBtn() {
  274. setTimeout(() => {
  275. let navWidth = document.querySelector('nav[role="navigation"]').offsetWidth;
  276. addSliderNavDeBtn(navWidth < 245);
  277. let innerDeIcon = document.getElementById('de-btn1');
  278. if (!innerDeIcon) {
  279. let dialogScheduleBtn = _getScheduleDom(false);
  280. dom && dom.deBtn1 && _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  281. }
  282. }, 800)
  283. }
  284. /**
  285. * 获取推文输入框内dom,用于插入deNet
  286. * @param isDialogInner
  287. * @returns {Element}
  288. * @private
  289. */
  290. function _getScheduleDom(isDialogInner = false) {
  291. let scheduleBtn;
  292. if (isDialogInner) {
  293. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  294. } else {
  295. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  296. if (toolBar) {
  297. scheduleBtn = toolBar.querySelector('div[data-testid="geoButton"]');
  298. }
  299. }
  300. return scheduleBtn;
  301. }
  302. /**
  303. * 插入iframe到页面
  304. * @private
  305. */
  306. function _addIframe() {
  307. // let span = document.createElement('span');
  308. // const shadowRoot = span.attachShadow({mode: 'closed'})
  309. let iframe = document.createElement('iframe');
  310. iframe.src = chrome.runtime.getURL('/iframe/publish.html')
  311. iframe.id = 'iframe-content'
  312. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:10000; border: medium none;display:none';
  313. // shadowRoot.appendChild(iframe);
  314. // document.body.appendChild(span)
  315. dom.iframe = iframe;
  316. let iframeContent = document.getElementById('iframe-content');
  317. if (!iframeContent) {
  318. document.querySelector('body').appendChild(iframe);
  319. }
  320. }
  321. function addPublishTipsIframe(params = {}) {
  322. let { time = 1000 } = params;
  323. setTimeout(() => {
  324. let dialog = document.querySelector('div[role="dialog"]').querySelector('div[role="dialog"]')
  325. if (dialog) {
  326. let right = dialog.offsetLeft - 15 - 266;
  327. let iframe = document.createElement('iframe');
  328. iframe.id = 'de-publish-tips'
  329. iframe.src = chrome.runtime.getURL('/iframe/publish-tips.html');
  330. iframe.style.cssText = `border: medium none; width:270px;height:500px;position: fixed; right: ${right}px; top: 5%;z-index: -1`
  331. let iframeContent = document.getElementById('de-publish-tips');
  332. if (!iframeContent) {
  333. dialog.appendChild(iframe)
  334. }
  335. }
  336. }, time)
  337. }
  338. export function noticeBindTweet(params) {
  339. hidePinTips();
  340. hidePopupPage();
  341. let iframe = document.createElement('iframe');
  342. iframe.id = 'de-notice-bind-tweet';
  343. iframe.src = chrome.runtime.getURL('/iframe/bind-tweet.html') + `?params=${JSON.stringify(params)}`;
  344. iframe.style.cssText = `border: medium none; width:400px;min-height:313px;position: fixed; right: 16px; top: 16px;`
  345. let iframeContent = document.getElementById('de-notice-bind-tweet');
  346. if (!iframeContent) {
  347. document.querySelector('body').appendChild(iframe)
  348. }
  349. }
  350. export function hideNoticeBindTweet() {
  351. let iframeContent = document.getElementById('de-notice-bind-tweet');
  352. if (iframeContent) {
  353. document.querySelector('body').removeChild(iframeContent)
  354. }
  355. }
  356. /**
  357. * 点击deNet按钮处理
  358. * @private
  359. */
  360. function _deNetBtnClick() {
  361. getUserInfo((res) => {
  362. if (res) {
  363. if (window.location.pathname != '/home') {
  364. if (!dom.homeBtn) {
  365. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  366. }
  367. dom.homeBtn.click();
  368. }
  369. showGiveDialogHandler(res);
  370. } else {
  371. let loadIcon = document.getElementById('de-btn-loading');
  372. if (loadIcon) {
  373. return;
  374. }
  375. dom && dom.deBtn && dom.deBtn.insertBefore(dom.loadingImg, dom.deBtn.querySelector('span'));
  376. setTimeout(() => {
  377. dom.loadingImg.style.transform = 'rotate(1080deg)'
  378. });
  379. setTimeout(() => {
  380. dom.loadingImg.style.transform = 'rotate(0deg)'
  381. dom.deBtn.innerHTML = '<span>DeNet<span>';
  382. }, 2000)
  383. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_LOGIN", data: '1' }, (res) => { console.log(res) })
  384. }
  385. })
  386. }
  387. /**
  388. * 设置发布内容
  389. * @param content
  390. * @private
  391. */
  392. let isSetContent = false;
  393. export const _setPublishContent = throttle(function (content, time = 1000) {
  394. if (!isSetContent) {
  395. isSetContent = true;
  396. let inputEle = document.querySelector('div[contenteditable="true"]');
  397. if (inputEle) {
  398. inputEle.focus();
  399. }
  400. setTimeout(() => {
  401. document.execCommand("insertText", false, content);
  402. setTimeout(() => {
  403. isSetContent = false;
  404. }, 2000)
  405. }, time);
  406. }
  407. }, 800);
  408. /**
  409. * 创建deNet按钮 添加到页面
  410. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  411. * @private
  412. */
  413. function _createBtnDom() {
  414. let loadingImg = document.createElement('img');
  415. loadingImg.id = 'de-btn-loading'
  416. loadingImg.src = require("@/assets/img/icon-btn-loading.png");
  417. loadingImg.style.cssText = 'width:20px;height: 20px;margin-right:3px;transition-duration: 3s;';
  418. let style = document.createElement('style');
  419. style.innerHTML = "#de-btn:hover{opacity: .9;};@-webkit-keyframes load{from{ transform: rotate(0deg);} to{transform: rotate(360deg);}}";
  420. document.getElementsByTagName('head').item(0).appendChild(style);
  421. // 左侧大屏按钮
  422. let deBtn = document.createElement('span');
  423. // const shadowDiv = document.createElement('div');
  424. deBtn.innerHTML = '<span>DeNet</span>';
  425. deBtn.id = 'de-btn';
  426. 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;';
  427. // 编辑框内按钮
  428. const deBtn1 = document.createElement('img');
  429. let src = require("@/assets/img/icon-gift-pack.png");
  430. const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;padding: 0px 8px';
  431. deBtn1.id = 'de-btn1';
  432. deBtn1.style.cssText = smallDeBtnStyle;
  433. deBtn1.src = src
  434. const deBtn2 = document.createElement('div');
  435. deBtn2.id = 'de-btn2';
  436. deBtn2.style.cssText = smallDeBtnStyle;
  437. deBtn2.src = "@/assets/img/icon-gift-pack.png"
  438. // 小屏按钮
  439. const deBtn3 = document.createElement('img');
  440. deBtn3.id = 'de-btn3'
  441. deBtn3.src = require("@/assets/logo/128.png");
  442. deBtn3.style.cssText = 'width:52px;height: 52px;margin-top:20px;cursor: pointer;';
  443. deBtn.addEventListener('click', () => {
  444. // chrome.runtime.sendMessage({
  445. // actionType: 'CONTENT_SET_BADGE',
  446. // data: {
  447. // text: '2'
  448. // }
  449. // }, res => {
  450. // console.log(res);
  451. // })
  452. Report.reportLog({
  453. pageSource: Report.pageSource.mainPage,
  454. businessType: Report.businessType.buttonClick,
  455. objectType: Report.objectType.buttonMain
  456. });
  457. _deNetBtnClick();
  458. })
  459. deBtn1.addEventListener('click', () => {
  460. Report.reportLog({
  461. pageSource: Report.pageSource.mainPage,
  462. businessType: Report.businessType.buttonClick,
  463. objectType: Report.objectType.buttonSecond
  464. });
  465. _deNetBtnClick();
  466. })
  467. deBtn2.addEventListener('click', () => {
  468. _deNetBtnClick();
  469. })
  470. deBtn3.addEventListener('click', () => {
  471. Report.reportLog({
  472. pageSource: Report.pageSource.mainPage,
  473. businessType: Report.businessType.buttonClick,
  474. objectType: Report.objectType.buttonMain
  475. });
  476. _deNetBtnClick();
  477. })
  478. dom.deBtn = deBtn;
  479. dom.deBtn1 = deBtn1;
  480. dom.deBtn2 = deBtn2;
  481. dom.deBtn3 = deBtn3;
  482. dom.loadingImg = loadingImg;
  483. }
  484. function addSliderNavDeBtn(isSmall = false) {
  485. if (!isSmall) {
  486. let bigDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  487. let deBtn = document.getElementById('de-btn');
  488. if (bigDom && !deBtn) {
  489. dom && dom.deBtn && bigDom.appendChild(dom.deBtn);
  490. Report.reportLog({
  491. pageSource: Report.pageSource.mainPage,
  492. businessType: Report.businessType.buttonView,
  493. objectType: Report.objectType.buttonMain
  494. });
  495. }
  496. } else {
  497. let smallDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  498. let deBtn3 = document.getElementById('de-btn3');
  499. if (smallDom && !deBtn3) {
  500. dom && dom.deBtn3 && smallDom.appendChild(dom.deBtn3);
  501. Report.reportLog({
  502. pageSource: Report.pageSource.mainPage,
  503. businessType: Report.businessType.buttonView,
  504. objectType: Report.objectType.buttonMain
  505. });
  506. }
  507. }
  508. }
  509. function onWindowResize() {
  510. window.onresize = throttle(function () {
  511. try {
  512. if (tweetPublishStore.showPublishDialog) {
  513. let dialog = document.querySelector('div[role="dialog"]');
  514. let dePublishTips = document.getElementById('de-publish-tips');
  515. if (dialog && !dePublishTips) {
  516. addPublishTipsIframe({ time: 0 });
  517. } else if (dialog && dePublishTips) {
  518. let dialogContent = dialog.querySelector('div[role=dialog]')
  519. if (dialogContent) {
  520. let right = dialogContent.offsetLeft - 15 - 266;
  521. dePublishTips.style.right = right + 'px';
  522. }
  523. }
  524. }
  525. if (window.innerWidth < 1273) {
  526. let bigBtn = document.querySelector('#de-btn');
  527. bigBtn && bigBtn.remove();
  528. setTimeout(() => {
  529. addSliderNavDeBtn(true);
  530. })
  531. } else {
  532. let smallBtn = document.querySelector('#de-btn3');
  533. smallBtn && smallBtn.remove();
  534. setTimeout(() => {
  535. addSliderNavDeBtn()
  536. })
  537. }
  538. } catch (e) {
  539. console.log(e)
  540. }
  541. }, 800)
  542. }
  543. function checkHasDeBtn() {
  544. try {
  545. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  546. let innerDeIcon = document.getElementById('de-btn1');
  547. if (toolBar && !innerDeIcon) {
  548. let dialogScheduleBtn = _getScheduleDom(false);
  549. dom && dom.deBtn1 && _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  550. }
  551. } catch (e) {
  552. console.log(e)
  553. }
  554. }
  555. /**
  556. * 点击发推,后端绑定推特id
  557. */
  558. function bindTwitterArtMethod() {
  559. if (!bindTwitterArt.postId) {
  560. return
  561. }
  562. if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
  563. bindTwitterArt.isBindIng = true;
  564. reportSrcPublishEvent({
  565. params: {
  566. postId: bindTwitterArt.postId,
  567. }
  568. }).then((res) => {
  569. if (res.code == 0) {
  570. Report.reportLog({
  571. objectType: Report.objectType.tweetPostBinded
  572. });
  573. bindTwitterArt.needBind = false;
  574. bindTwitterArt.postId = '';
  575. bindTwitterArt.isBindIng = false;
  576. }
  577. })
  578. }
  579. }
  580. import parseCard from './ParseCard'
  581. // 检测dom改变
  582. // 获取短链接
  583. // 查看本地是否有postid
  584. // 如果有 修改dom 返回
  585. // 如果没有 网络请求
  586. // 获取postid
  587. // 获取twitterid
  588. // 检测当前所有dom 如果没有
  589. let queue_num = 1
  590. export const changeQueueNum = (num = 0) => {
  591. queue_num = queue_num + num
  592. if (queue_num > 5) {
  593. queue_num = 5
  594. }
  595. }
  596. let main_observer = null
  597. function onChangePageMain(targetNode) {
  598. try {
  599. const config = { attributes: false, childList: true, subtree: true };
  600. const callback = (mutationsList, observer) => {
  601. changeQueueNum(1)
  602. }
  603. main_observer = new MutationObserver(callback);
  604. main_observer.observe(targetNode, config);
  605. } catch (error) {
  606. main_observer = null
  607. }
  608. }
  609. // 1.监听main改变
  610. // 2.监听卡片是否可见
  611. // 3.如果可见了 去找
  612. function setIframeRedPacket(type = 'twitter') {
  613. // 获取所有卡片参数
  614. let card_json_data
  615. switch (type) {
  616. case 'facebook':
  617. card_json_data = parseCard.parseFacebookCardParmas()
  618. for (let i in card_json_data) {
  619. parseCard.replaceFacebookPacket(card_json_data[i])
  620. }
  621. break;
  622. default:
  623. card_json_data = parseCard.parseAllDeNetCardParmas()
  624. // 过滤出可以请求的短链接
  625. parseCard.getCardParmas(card_json_data).then((res) => {
  626. for (let i in res.has_post_Id_card_data) {
  627. let item = res.has_post_Id_card_data[i];
  628. if (item && item.post_Id && item.post_Id.indexOf('nft/') >= 0) {
  629. parseCard.replaceNftDomRedPacket(item)
  630. } else if (item && item.post_Id && item.post_Id.indexOf('nft_group/') >= 0) {
  631. parseCard.replaceNftGroupDomRedPacket(item)
  632. } else {
  633. parseCard.replaceDOMRedPacket(item)
  634. }
  635. }
  636. if (res.need_net_short_url.length > 0) {
  637. // 请求短链接
  638. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, () => { })
  639. }
  640. })
  641. break;
  642. }
  643. }
  644. // 监听点击发推 按钮绑定事件
  645. // document.addEventListener('click', (e) => {
  646. // try {
  647. // if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
  648. // bindTwitterArtMethod()
  649. // } else if (e.target.closest('div[data-testid=tweetButton]')) {
  650. // bindTwitterArtMethod()
  651. // }
  652. // } catch (error) {
  653. // console.error('error', error)
  654. // }
  655. // })
  656. export function initExecuteScript(changes) {
  657. if (changes.executeScript) {
  658. let item = JSON.parse(changes.executeScript.newValue)
  659. if (item.executeScript) {
  660. init()
  661. }
  662. }
  663. }
  664. const createNFTIframe = ({ url, id }, callback) => {
  665. let iframe = document.createElement('iframe')
  666. iframe.id = id
  667. iframe.src = url
  668. iframe.style.cssText = 'border:medium none; width:100%; height:100%; z-index:100; position: fixed; top:0;left:0; display:none;';
  669. iframe.onload = () => {
  670. callback && callback()
  671. }
  672. document.body.appendChild(iframe);
  673. }
  674. function initParseCard() {
  675. let timer = setInterval(() => {
  676. let inTwitter = window.location.href.includes('twitter.com');
  677. let inTwitterNode = document.querySelector('main');
  678. let inFacebook = window.location.href.includes('facebook.com');
  679. let inFacebookNode = document.querySelector('#facebook');
  680. if (inTwitter && inTwitterNode) {
  681. clearInterval(timer)
  682. setInterval(() => {
  683. if (!main_observer) {
  684. onChangePageMain(inTwitterNode)
  685. changeQueueNum(1)
  686. }
  687. if (queue_num <= 0) {
  688. return
  689. }
  690. showNFTGroupIcon()
  691. setIframeRedPacket()
  692. checkHasDeBtn()
  693. checkHasSliderDeBtn();
  694. changeQueueNum(-1)
  695. showNFTCard()
  696. }, 1000)
  697. } else if (inFacebook && inFacebookNode) {
  698. clearInterval(timer)
  699. setInterval(() => {
  700. if (!main_observer) {
  701. onChangePageMain(inFacebookNode)
  702. changeQueueNum(1)
  703. }
  704. if (queue_num <= 0) {
  705. return
  706. }
  707. setIframeRedPacket('facebook')
  708. changeQueueNum(-1)
  709. }, 1000)
  710. }
  711. }, 1000);
  712. }
  713. let inited = false
  714. // 初始化
  715. export function init() {
  716. if (inited) {
  717. return
  718. }
  719. inited = true
  720. console.log('init')
  721. getDiscordAuthCode();
  722. appendPopupPage();
  723. chrome.runtime.sendMessage({
  724. actionType: "CONTENT_SET_POPUP_CONFIG",
  725. data: {
  726. popup: 'popup.html'
  727. }
  728. }, () => { });
  729. let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
  730. if (where) {
  731. return
  732. }
  733. twitterPinLogin();
  734. // 渲染dom
  735. initParseCard()
  736. showNFTCard()
  737. showNFTGroupIcon()
  738. addEventAction();
  739. checkUserJoinGroup();
  740. renderDom();
  741. checkTwitterTaskState();
  742. onBodyClick();
  743. initBuyNFT();
  744. getChromeStorage("popupShowPublishDialog", (res) => {
  745. console.log("popupShowPublishDialog", res);
  746. if (res && res.show) {
  747. setTimeout(() => {
  748. showTwitterPublishDialogHandler({
  749. srcContent: res.srcContent,
  750. postId: res.postId,
  751. copyContent: res.copyContent || ''
  752. });
  753. }, 1500);
  754. chrome.storage.local.remove("popupShowPublishDialog");
  755. }
  756. });
  757. getChromeStorage("userSettings", (res) => {
  758. setTimeout(() => {
  759. addPinedPop();
  760. if (res && !res.isOnToolbar) {
  761. setTimeout(() => {
  762. showPinTips();
  763. chrome.storage.local.remove("userSettings");
  764. }, 800);
  765. }
  766. }, 800);
  767. });
  768. }
  769. function checkHasSliderDeBtn() {
  770. let deBtn = document.getElementById('de-btn');
  771. let deBtn3 = document.getElementById('de-btn3');
  772. if (!deBtn && !deBtn3) {
  773. addSliderNavDeBtn();
  774. }
  775. }
  776. export function facebookReplyTweet(params) {
  777. if (window.location.origin.indexOf('twitter.com')) {
  778. const urlParams = new URLSearchParams(window.location.search);
  779. const actionType = urlParams.get('actionType');
  780. let deReplyParams = urlParams.get('deReplyParams') || '{}';
  781. deReplyParams = JSON.parse(deReplyParams);
  782. if (actionType == 'denetFacebookToTwitterReply') {
  783. if (params.postId == deReplyParams.postId) {
  784. let iframe = document.getElementById(params.postId);
  785. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_FACEBOOK_REPLY', data: deReplyParams }, '*');
  786. }
  787. }
  788. }
  789. }
  790. export function replyHandle(params) {
  791. let iframe = window.parent.document.getElementById(params.postId);
  792. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  793. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  794. if (replyBtn) {
  795. replyBtn.click();
  796. }
  797. onReplyDialogOpen(params, iframe);
  798. // 详情页推文底部评论处理
  799. let pathNameArr = window.location.pathname.split('/');
  800. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  801. let tweetReply = document.querySelector('div[data-testid="tweetButtonInline"]');
  802. if (tweetReply) {
  803. tweetReply.addEventListener('click', function () {
  804. // 详情页回复按钮点击
  805. Report.reportLog({
  806. pageSource: Report.pageSource.mainPage,
  807. businessType: Report.businessType.buttonClick,
  808. objectType: Report.objectType.replyClickByDetailPage
  809. });
  810. let eleList = tweetReply.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll('span[data-text="true"]');
  811. reportReplyResult(eleList, params, () => {
  812. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  813. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  814. });
  815. })
  816. }
  817. }
  818. }
  819. export function onTweetReplyClick(params) {
  820. let iframe = window.parent.document.getElementById(params.postId);
  821. let replyBtn = iframe.parentNode.parentNode.querySelector('div[data-testid="reply"]') ||
  822. iframe.parentNode.parentNode.parentNode.querySelector('div[data-testid="reply"]');
  823. if (replyBtn) {
  824. replyBtn.addEventListener('click', () => {
  825. onReplyDialogOpen(params, iframe);
  826. })
  827. }
  828. }
  829. function onReplyDialogOpen(params, iframe) {
  830. setTimeout(() => {
  831. let dialog = document.querySelector('div[role="dialog"]');
  832. let replyBtn;
  833. if (dialog) {
  834. let dialogContent = dialog.querySelector('div[role="dialog"]');
  835. replyBtn = dialog.querySelector('div[data-testid="toolBar"]').querySelector('div[data-testid="tweetButton"]');
  836. if (dialogContent) {
  837. let width = dialogContent.offsetWidth;
  838. let ele = document.createElement('div');
  839. ele.innerText = 'Tag 3 friends to complete the task';
  840. ele.style.cssText = `width: ${width}px; height: 38px; color: #fff; font-weight: 600;
  841. font-size: 16px; display: flex; align-items: center; justify-content: center; background: #1D9BF0;
  842. opacity: 0.8; position: absolute; top: 18px; left: 50%; transform: translateX(-50%); z-index: 1000`;
  843. dialogContent.style.top = '80px';
  844. dialogContent.parentNode.appendChild(ele);
  845. }
  846. } else {
  847. dialog = document.querySelector('main[role="main"]');
  848. if (dialog) {
  849. replyBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  850. }
  851. }
  852. if (dialog && replyBtn) {
  853. replyBtn.addEventListener('click', function () {
  854. // 推文页回复按钮点击
  855. Report.reportLog({
  856. pageSource: Report.pageSource.mainPage,
  857. businessType: Report.businessType.buttonClick,
  858. objectType: Report.objectType.replyClickByTwitterList
  859. });
  860. let eleList = dialog.querySelector('div[contenteditable="true"]').querySelectorAll('span[data-text="true"]');
  861. reportReplyResult(eleList, params, () => {
  862. // 上報完成
  863. // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
  864. chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
  865. })
  866. });
  867. }
  868. }, 1000);
  869. }
  870. const reportReplyResult = throttle(function (eleList, params, cb) {
  871. //未过滤的回复文本
  872. Report.reportLog({
  873. pageSource: Report.pageSource.mainPage,
  874. businessType: Report.businessType.buttonClick,
  875. objectType: Report.objectType.replyInputTextGet
  876. }, {
  877. replyStr: eleList
  878. });
  879. if (eleList && eleList.length) {
  880. let atList = [];
  881. for (let i = 0; i < eleList.length; i++) {
  882. let item = eleList[i];
  883. // 是否有中文
  884. let reg = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
  885. if (item && item.innerText.startsWith('@') && !reg.test(item.innerText)) {
  886. atList.push(item.innerText);
  887. }
  888. }
  889. // 去重过滤文本
  890. atList = Array.from(new Set(atList));
  891. Report.reportLog({
  892. pageSource: Report.pageSource.mainPage,
  893. businessType: Report.businessType.buttonClick,
  894. objectType: Report.objectType.replyFilterTextGet
  895. }, {
  896. replyStr: atList
  897. });
  898. if (atList.length >= 3) {
  899. //真实上报
  900. Report.reportLog({
  901. pageSource: Report.pageSource.mainPage,
  902. businessType: Report.businessType.buttonClick,
  903. objectType: Report.objectType.replyReport
  904. }, {
  905. replyStr: atList
  906. });
  907. fetchAddFinishEvent({
  908. eventType: params.type,
  909. luckdropId: params.taskLuckdropId
  910. }).then(res => {
  911. if (res.code == 0) {
  912. cb && cb();
  913. }
  914. })
  915. }
  916. }
  917. }, 800);
  918. // 根据推特id找到dom,完成任务
  919. export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_type = 'like') {
  920. // 1.根据推特ID寻找推文,获取卡片
  921. // 获取所有卡片参数
  922. let card_json_data = parseCard.parseAllDeNetCardParmas()
  923. let result = card_json_data.filter((item) => { return item.tweet_Id == tweet_Id }) || []
  924. if (result.length < 1) {
  925. return
  926. }
  927. result = result[0]
  928. switch (task_type) {
  929. case 'like':
  930. // https://twitter.com/intent/retweet?tweet_id=1525900221628223491
  931. if (result.dom_card) {
  932. result.dom_card.querySelector('div[data-testid=like]').click()
  933. result.dom_card.querySelector('iframe').contentWindow.postMessage({ actionType: 'CONTENT_DONE_TASK', task_type, }, '*');
  934. }
  935. break;
  936. case 'retweet':
  937. if (result.dom_card) {
  938. result.dom_card.querySelector('div[data-testid=retweet]').click()
  939. result.dom_card.querySelector('div[data-testid=retweetConfirm]').click()
  940. }
  941. break
  942. case 'follow':
  943. follow_names.forEach((item) => {
  944. window.open(`https://twitter.com/intent/follow?screen_name=${item}&tweet_Id=${tweet_Id}`)
  945. })
  946. break
  947. }
  948. }
  949. function clickByDataTestId(e, id, callback) {
  950. if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
  951. callback()
  952. } else if (e.target.closest('div[data-testid=' + id + ']')) {
  953. callback()
  954. }
  955. }
  956. // 校验关注推特状态
  957. export function checkTwitterTaskState() {
  958. let task_type = ''
  959. let url = window.location.href
  960. let tweet_Id
  961. let task_data = {
  962. follow_name: ''
  963. }
  964. // 校验当前链接
  965. if (url.includes('https://twitter.com/intent/retweet')) {
  966. task_type = 'retweet'
  967. } else if (url.includes('https://twitter.com/intent/follow')) {
  968. task_type = 'follow'
  969. task_data.follow_name = getQueryString('screen_name')
  970. } else if (url.includes('https://twitter.com/intent/like')) {
  971. task_type = 'like'
  972. } else {
  973. return
  974. }
  975. tweet_Id = getQueryString('tweet_id')
  976. // let root_status
  977. document.body.addEventListener('click', (e) => {
  978. // 点击 确认
  979. clickByDataTestId(e, 'confirmationSheetConfirm', () => {
  980. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  981. })
  982. // 点击取消
  983. clickByDataTestId(e, 'confirmationSheetCancel', () => {
  984. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  985. })
  986. // 点击 蒙层
  987. if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
  988. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  989. }
  990. }, true)
  991. let timer = setInterval(() => {
  992. // 喜欢
  993. if (document.querySelector('div[data-testid=unlike]') && task_type == 'like') {
  994. clearInterval(timer)
  995. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  996. }
  997. // 转推
  998. if (document.querySelector('div[data-testid=unretweet]') && task_type == 'retweet') {
  999. clearInterval(timer)
  1000. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1001. }
  1002. // 关注
  1003. if (task_type == 'follow') {
  1004. let follow_area = document.querySelector('div[data-testid=placementTracking]')
  1005. if (follow_area && follow_area.querySelectorAll('div')) {
  1006. follow_area = follow_area.querySelectorAll('div')
  1007. clearInterval(timer)
  1008. for (let i in follow_area) {
  1009. if (follow_area[i] && follow_area[i].dataset && follow_area[i].dataset.testid && follow_area[i].dataset.testid.indexOf('unfollow') > 0) {
  1010. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  1011. break
  1012. }
  1013. }
  1014. }
  1015. }
  1016. }, 1000)
  1017. }
  1018. export function getTweetAuthorByDom(params) {
  1019. let iframe = document.getElementById(params.postId);
  1020. if (!iframe) {
  1021. return;
  1022. }
  1023. let fullNameDom;
  1024. let pathNameArr = window.location.pathname.split('/');
  1025. if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
  1026. fullNameDom = iframe.parentNode.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1027. } else {
  1028. fullNameDom = iframe.parentNode.parentNode.parentNode.querySelector('a[role=link]');
  1029. }
  1030. if (fullNameDom) {
  1031. let arr = fullNameDom.href.split('/');
  1032. if (arr.length) {
  1033. let fullName = arr[arr.length - 1];
  1034. if (fullName) {
  1035. iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_GET_TWEET_AUTHOR', data: { fullName } }, '*');
  1036. }
  1037. }
  1038. }
  1039. }
  1040. export function doTaskTwitterAPI({ task_data, task_type }) {
  1041. switch (task_type) {
  1042. case 'like':
  1043. TwitterLikeAPI(task_data.tweet_Id)
  1044. break
  1045. case 'retweet':
  1046. TwitterRetweetAPI(task_data.tweet_Id)
  1047. break
  1048. case 'follow':
  1049. task_data.follow_data.forEach((item) => {
  1050. if (item.name && item.twitterUserId) {
  1051. TwitterFollowAPI(item, task_data.tweet_Id)
  1052. }
  1053. })
  1054. break
  1055. }
  1056. }
  1057. const TwitterFollowAPI = (item, tweet_Id) => {
  1058. fetch("https://twitter.com/i/api/1.1/friendships/create.json", {
  1059. "headers": {
  1060. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1061. "content-type": "application/x-www-form-urlencoded",
  1062. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1063. "sec-ch-ua-mobile": "?0",
  1064. "sec-ch-ua-platform": "\"macOS\"",
  1065. "x-csrf-token": getCookie('ct0'),
  1066. "x-twitter-active-user": "yes",
  1067. "x-twitter-auth-type": "OAuth2Session",
  1068. "x-twitter-client-language": "zh-cn"
  1069. },
  1070. "referrer": "https://twitter.com/home",
  1071. "referrerPolicy": "strict-origin-when-cross-origin",
  1072. "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 + "",
  1073. "method": "POST",
  1074. "mode": "cors",
  1075. "credentials": "include"
  1076. }).then(() => {
  1077. let task_data = {
  1078. follow_name: item.name
  1079. }
  1080. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: true }, () => { })
  1081. }).catch(() => {
  1082. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'follow', task_data, task_done: false }, () => { })
  1083. })
  1084. }
  1085. const TwitterRetweetAPI = (tweet_Id) => {
  1086. fetch("https://twitter.com/i/api/graphql/ojPdsZsimiJrUGLR1sjUtA/CreateRetweet", {
  1087. "headers": {
  1088. "accept": "*/*",
  1089. "accept-language": "zh,en;q=0.9,zh-CN;q=0.8",
  1090. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1091. "content-type": "application/json",
  1092. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
  1093. "sec-ch-ua-mobile": "?0",
  1094. "sec-ch-ua-platform": "\"macOS\"",
  1095. "sec-fetch-dest": "empty",
  1096. "sec-fetch-mode": "cors",
  1097. "sec-fetch-site": "same-origin",
  1098. "x-csrf-token": getCookie('ct0'),
  1099. "x-twitter-active-user": "yes",
  1100. "x-twitter-auth-type": "OAuth2Session",
  1101. "x-twitter-client-language": "zh-cn"
  1102. },
  1103. "referrer": "https://twitter.com/home",
  1104. "referrerPolicy": "strict-origin-when-cross-origin",
  1105. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\",\"dark_request\":false},\"queryId\":\"ojPdsZsimiJrUGLR1sjUtA\"}",
  1106. "method": "POST",
  1107. "mode": "cors",
  1108. "credentials": "include"
  1109. }).then(() => {
  1110. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: true }, () => { })
  1111. }).catch(() => {
  1112. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'retweet', task_data: '', task_done: false }, () => { })
  1113. })
  1114. }
  1115. const TwitterLikeAPI = (tweet_Id) => {
  1116. fetch("https://twitter.com/i/api/graphql/lI07N6Otwv1PhnEgXILM7A/FavoriteTweet", {
  1117. "headers": {
  1118. "accept": "*/*",
  1119. "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
  1120. "authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
  1121. "content-type": "application/json",
  1122. "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
  1123. "sec-ch-ua-mobile": "?0",
  1124. "sec-ch-ua-platform": "\"Windows\"",
  1125. "sec-fetch-dest": "empty",
  1126. "sec-fetch-mode": "cors",
  1127. "sec-fetch-site": "same-origin",
  1128. "x-csrf-token": getCookie('ct0'),
  1129. "x-twitter-active-user": "yes",
  1130. "x-twitter-auth-type": "OAuth2Session",
  1131. "x-twitter-client-language": "en"
  1132. },
  1133. "referrer": "https://twitter.com/home",
  1134. "referrerPolicy": "strict-origin-when-cross-origin",
  1135. "body": "{\"variables\":{\"tweet_id\":\"" + tweet_Id + "\"},\"queryId\":\"lI07N6Otwv1PhnEgXILM7A\"}",
  1136. "method": "POST",
  1137. "mode": "cors",
  1138. "credentials": "include"
  1139. }).then(() => {
  1140. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: true }, () => { })
  1141. }).catch(() => {
  1142. chrome.runtime.sendMessage({ actionType: "DO_TASK", do_type: 'api', tweet_Id, task_type: 'like', task_data: '', task_done: false }, () => { })
  1143. })
  1144. }
  1145. export const hideBuyNFT = () => {
  1146. let iframe = document.querySelector('#nftProjectId')
  1147. iframe.style.display = 'none'
  1148. iframe.src = ''
  1149. }
  1150. export const showBuyNFT = ({ nft_project_Id }) => {
  1151. if (!nft_project_Id) {
  1152. return
  1153. }
  1154. let iframe = document.querySelector('#nftProjectId')
  1155. iframe.style.display = 'block'
  1156. iframe.src = chrome.runtime.getURL(`/iframe/buy-nft.html?nftProjectId=${nft_project_Id}`)
  1157. }
  1158. const initBuyNFT = () => {
  1159. let url = chrome.runtime.getURL(`/iframe/buy-nft.html`)
  1160. let id = `nftProjectId`
  1161. createNFTIframe({ url, id })
  1162. }
  1163. export const showNFTCard = () => {
  1164. let urlInfo = new URL(window.location.href)
  1165. let isTwitter = urlInfo.hostname === 'twitter.com'
  1166. let userElem = document.querySelector('div[data-testid="UserName"]');
  1167. let sideElem = document.querySelector('div[data-testid="sidebarColumn"]')
  1168. let tabIndex = sideElem && sideElem.querySelector('div[tabindex="0"]');
  1169. let isAppend = document.querySelector('div[id="de-nft-node"]');
  1170. let where = isTwitter && userElem && tabIndex;
  1171. if (where) {
  1172. let iframe = document.createElement('iframe');
  1173. iframe.src = chrome.runtime.getURL(`/iframe/nft-card.html`)
  1174. iframe.style.cssText = 'border:medium none; width:100%; height:290px;';
  1175. let nftElement = document.createElement('div');
  1176. nftElement.id = 'de-nft-node';
  1177. nftElement.innerHTML = `
  1178. ${iframe.outerHTML}
  1179. <style>
  1180. #de-nft-node {height:290px; margin-bottom:17px; display:none;}
  1181. </style>
  1182. `;
  1183. if (tabIndex && tabIndex.firstChild && tabIndex.firstChild.childNodes && !isAppend) {
  1184. tabIndex.firstChild.insertBefore(nftElement, tabIndex.firstChild.childNodes[2]);
  1185. }
  1186. }
  1187. }
  1188. export const showNFTSale = () => {
  1189. document.querySelector('div[id="de-nft-node"]').style.display = 'block';
  1190. }
  1191. export const addEventAction = () => {
  1192. let urlInfo = new URL(window.location.href)
  1193. let isTwitter = urlInfo.hostname === 'twitter.com'
  1194. // 监听发推按钮
  1195. let btn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  1196. btn && btn.addEventListener('click', () => {
  1197. // 重置监听次数
  1198. queue_num = 3;
  1199. })
  1200. // 页面滚动
  1201. if (isTwitter) {
  1202. // 首页
  1203. if (urlInfo.pathname === '/home') {
  1204. window.addEventListener('scroll', () => {
  1205. hideNFTGroupList()
  1206. })
  1207. }
  1208. }
  1209. }
  1210. export const appendPopupPage = (params = {}) => {
  1211. let { path = '' } = params;
  1212. let iframe = document.createElement('iframe');
  1213. iframe.id = 'de-popup-page';
  1214. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1215. iframe.style.cssText = `border: medium none; width: 375px;
  1216. 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;
  1217. animation-duration: 0.5s !important;
  1218. animation-timing-function: ease-in-out !important;
  1219. animation-fill-mode: forwards !important;
  1220. transition: all 1s ease 0s !important;right: -385px;transform: translateX(385px);`
  1221. let iframeContent = document.getElementById('de-popup-page');
  1222. let overlayDom = document.createElement('div');
  1223. overlayDom.id = 'de-popup-overlay';
  1224. overlayDom.style.cssText = `position: fixed;z-index: 88888;top: 0;
  1225. left: 0;width: 100%;height: 100%;opacity: 0;display: none`;
  1226. let overlay = document.getElementById('de-popup-overlay');
  1227. let body = document.querySelector('body');
  1228. if (!iframeContent && body) {
  1229. document.querySelector('body').appendChild(iframe);
  1230. if(!overlay) {
  1231. document.querySelector('body').appendChild(overlayDom);
  1232. overlayDom.addEventListener('click', function() {
  1233. hidePopupPage();
  1234. })
  1235. } else {
  1236. overlay.addEventListener('click', function() {
  1237. hidePopupPage();
  1238. })
  1239. }
  1240. }
  1241. }
  1242. let showPopupPageFrom = '';
  1243. export const showPopupPage = (params = {}) => {
  1244. let { path = '', from } = params;
  1245. showPopupPageFrom = from;
  1246. hidePinTips();
  1247. hideNoticeBindTweet();
  1248. let iframe = document.getElementById('de-popup-page');
  1249. if (!iframe) {
  1250. appendPopupPage();
  1251. iframe = document.getElementById('de-popup-page');
  1252. }
  1253. if(iframe) {
  1254. if(path) {
  1255. iframe.src = chrome.runtime.getURL('/iframe/popup-page.html') + `#${path}`;
  1256. }
  1257. iframe.style.transform = 'translateX(-' + 395 + 'px)';
  1258. chrome.runtime.sendMessage({
  1259. actionType: "CONTENT_POPUP_PAGE_SHOW",
  1260. data: { }
  1261. }, () => { });
  1262. chrome.runtime.sendMessage({
  1263. actionType: "CONTENT_SET_POPUP_CONFIG",
  1264. data: {
  1265. popup: ''
  1266. }
  1267. }, () => { });
  1268. let overlay = document.getElementById('de-popup-overlay');
  1269. overlay.style.display = 'block';
  1270. let htmlDom = document.querySelector('html');
  1271. if(htmlDom) {
  1272. htmlDom.style.overflowY = 'hidden';
  1273. }
  1274. }
  1275. }
  1276. export const hidePopupPage = () => {
  1277. let iframe = document.getElementById('de-popup-page');
  1278. if (iframe) {
  1279. iframe.style.transform = 'translateX(' + 385 + 'px)';
  1280. let overlay = document.getElementById('de-popup-overlay');
  1281. overlay.style.display = 'none';
  1282. let htmlDom = document.querySelector('html');
  1283. if(htmlDom) {
  1284. htmlDom.style.overflowY = 'auto';
  1285. }
  1286. chrome.runtime.sendMessage({
  1287. actionType: "CONTENT_SET_POPUP_CONFIG",
  1288. data: {
  1289. popup: 'popup.html'
  1290. }
  1291. }, () => { });
  1292. if(showPopupPageFrom == 'BUY_NFT_FINISH') {
  1293. chrome.runtime.sendMessage({
  1294. actionType: "CONTENT_GET_PINED",
  1295. data: {}
  1296. }, () => { });
  1297. showPopupPageFrom = '';
  1298. }
  1299. }
  1300. }
  1301. export const tiggerInjectPopupPage = () => {
  1302. let iframeContent = document.getElementById('de-popup-page');
  1303. if (iframeContent) {
  1304. hidePinTips();
  1305. hideNoticeBindTweet();
  1306. let {transform = ''} = iframeContent.style;
  1307. if(transform == 'translateX(385px)' || !transform) {
  1308. showPopupPage();
  1309. } else {
  1310. hidePopupPage();
  1311. }
  1312. } else {
  1313. appendPopupPage();
  1314. setTimeout(() => {
  1315. let iframe = document.getElementById('de-popup-page');
  1316. let {transform = ''} = iframe.style;
  1317. if(transform == 'translateX(385px)' || !transform) {
  1318. showPopupPage();
  1319. } else {
  1320. hidePopupPage();
  1321. }
  1322. }, 300)
  1323. }
  1324. }
  1325. const onBodyClick = () => {
  1326. if(window.location.href.indexOf('api.twitter.com') < 0) {
  1327. document.querySelector('body').addEventListener('click', function () {
  1328. console.log('click')
  1329. // hidePopupPage();
  1330. })
  1331. }
  1332. }
  1333. export const setPopupConfByPopupPage = () => {
  1334. let iframe = document.getElementById('de-popup-page');
  1335. if (iframe) {
  1336. let {transform = ''} = iframe.style;
  1337. if(transform && transform == 'translateX(-395px)') {
  1338. chrome.runtime.sendMessage({
  1339. actionType: "CONTENT_SET_POPUP_CONFIG",
  1340. data: {
  1341. popup: ''
  1342. }
  1343. }, () => { });
  1344. } else {
  1345. chrome.runtime.sendMessage({
  1346. actionType: "CONTENT_SET_POPUP_CONFIG",
  1347. data: {
  1348. popup: 'popup.html'
  1349. }
  1350. }, () => { });
  1351. }
  1352. } else {
  1353. chrome.runtime.sendMessage({
  1354. actionType: "CONTENT_SET_POPUP_CONFIG",
  1355. data: {
  1356. popup: 'popup.html'
  1357. }
  1358. }, () => { });
  1359. }
  1360. }
  1361. export const loginSuccessHandle = () => {
  1362. // 检查是否漏出group图标
  1363. checkUserJoinGroup(() => {
  1364. showNFTGroupIcon()
  1365. addEventAction()
  1366. })
  1367. }