twitter.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. import { getChromeStorage, setChromeStorage } from '@/uilts/chromeExtension.js'
  2. import { throttle, getQueryString } from '@/uilts/help'
  3. import { discordAuthRedirectUri } from '@/http/configAPI'
  4. import { reportSrcPublishEvent } from '@/http/publishApi'
  5. import Report from "@/log-center/log"
  6. let dom = {};
  7. function twitterPinLogin() {
  8. if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
  9. let code = document.querySelector('code')
  10. if (code) {
  11. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_CODE", code: code.innerText }, () => { })
  12. // port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
  13. }
  14. }
  15. }
  16. function getDiscordAuthCode() {
  17. if (window.location.href.indexOf(discordAuthRedirectUri) > -1) {
  18. const urlParams = new URLSearchParams(window.location.search);
  19. const code = urlParams.get('code');
  20. if (code) {
  21. chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_DISCORD_AUTH_CODE", code }, () => { })
  22. }
  23. }
  24. };
  25. /**
  26. * 渲染要插入的dom,初始化逻辑
  27. * @param port
  28. */
  29. function renderDom() {
  30. if (window.location.href.indexOf('https://twitter.com') > -1) {
  31. _createBtnDom();
  32. onWindowResize();
  33. checkHasDeBtn();
  34. setTimeout(() => {
  35. _addIframe();
  36. _addDeNetBtn();
  37. _getSliderTwitterBtn();
  38. }, 800)
  39. }
  40. }
  41. /**
  42. * 展示give弹窗
  43. */
  44. export function showGiveDialogHandler(userInfo) {
  45. let iframe = document.getElementById('iframe-content');
  46. if (iframe) {
  47. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  48. } else {
  49. _addIframe();
  50. let iframe = document.getElementById('iframe-content');
  51. iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  52. }
  53. }
  54. export function showIframeHandler() {
  55. document.getElementById('iframe-content').style.display = 'block';
  56. }
  57. export function hideIframeHandler() {
  58. document.getElementById('iframe-content').style.display = 'none';
  59. }
  60. /**
  61. * 展示twitter原生发布框
  62. */
  63. export function showTwitterPublishDialogHandler(publishRes) {
  64. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  65. if(bigBtn) {
  66. bigBtn.click();
  67. } else {
  68. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  69. smallBtn && smallBtn.click();
  70. }
  71. _setPublishContent(publishRes.srcContent);
  72. _publishTweetEvent(publishRes.postId, bindTwitterArtMethod);
  73. }
  74. export function showPinTips() {
  75. getChromeStorage('pinData', (res) => {
  76. if (!res || res.show) {
  77. let domPop = document.getElementById('de-pin-pop');
  78. domPop.style.display = 'block';
  79. }
  80. })
  81. }
  82. function addPinedPop() {
  83. let domPop = document.getElementById('de-pin-pop');
  84. if (domPop) {
  85. return;
  86. }
  87. let popWrapper = document.createElement('div');
  88. 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';
  89. popWrapper.id = 'de-pin-pop'
  90. let img = document.createElement('img');
  91. img.src = require("@/assets/img/img-pined-guide.png");
  92. img.width = 253;
  93. let contentDom = document.createElement('div');
  94. 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>"
  95. popWrapper.appendChild(img);
  96. popWrapper.appendChild(contentDom);
  97. document.querySelector('body').appendChild(popWrapper);
  98. let deCheck = document.querySelector('#de-check');
  99. let deRemind = document.querySelector('#de-remind');
  100. deCheck.onclick = function (e) {
  101. e && e.stopPropagation && e.stopPropagation();
  102. setChromeStorage({ pinData: JSON.stringify({ show: !this.checked }) })
  103. }
  104. deRemind.onclick = function () {
  105. deCheck.checked = !deCheck.checked;
  106. setChromeStorage({ pinData: JSON.stringify({ show: !deCheck.checked }) })
  107. }
  108. document.querySelector('.de-pin-skip').onclick = function () {
  109. document.querySelector('#de-pin-pop').style.display = 'none';
  110. }
  111. }
  112. function getUserInfo(cb) {
  113. getChromeStorage('userInfo', (res) => {
  114. cb && cb(res);
  115. })
  116. }
  117. // 绑定推文id所需参数
  118. let bindTwitterArt = {
  119. needBind: false,
  120. postId: '',
  121. isBindIng: false
  122. };
  123. /**
  124. * 监听dialog内点击原生发布按钮事件
  125. * @private
  126. */
  127. function _publishTweetEvent(contentStr, cb) {
  128. setTimeout(() => {
  129. let publishTweetBtn;
  130. let dialog = document.querySelector('div[role="dialog"]');
  131. if (dialog) {
  132. publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
  133. } else {
  134. let domMain = document.querySelector('main[role="main"]');
  135. publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
  136. }
  137. publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
  138. bindTwitterArt.needBind = true;
  139. bindTwitterArt.postId = contentStr;
  140. cb && cb()
  141. });
  142. }, 800)
  143. }
  144. /**
  145. * 在输入推文区插入deNet按钮
  146. * @param parent
  147. * @param dom
  148. * @param isClick
  149. * @private
  150. */
  151. function _addDeNetEditBtn(parent, dom, isClick = false) {
  152. setTimeout(() => {
  153. if (parent && parent.parentNode) {
  154. Report.reportLog({
  155. pageSource: Report.pageSource.mainPage,
  156. businessType: Report.businessType.buttonView,
  157. objectType: Report.objectType.buttonSecond
  158. });
  159. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  160. } else {
  161. setTimeout(() => {
  162. parent = _getScheduleDom(isClick);
  163. if (parent && parent.parentNode) {
  164. Report.reportLog({
  165. pageSource: Report.pageSource.mainPage,
  166. businessType: Report.businessType.buttonView,
  167. objectType: Report.objectType.buttonSecond
  168. });
  169. let innerDeIcon = document.getElementById('de-btn1');
  170. if (!innerDeIcon) {
  171. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  172. }
  173. }
  174. }, 1000)
  175. }
  176. })
  177. }
  178. /**
  179. * 在dialog插入deNet按钮
  180. * @private
  181. */
  182. // function _addDeNetBtnToDialog() {
  183. // setTimeout(() => {
  184. // let dialogScheduleBtn = _getScheduleDom(true);
  185. // _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  186. // }, 800)
  187. // }
  188. /**
  189. * 获取左侧twitter按钮
  190. * @private
  191. */
  192. function _getSliderTwitterBtn() {
  193. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  194. dom.tweetBtn.addEventListener('click', function () {
  195. // _addDeNetBtnToDialog();
  196. })
  197. }
  198. /**
  199. * 添加deNet按钮
  200. * @private
  201. */
  202. function _addDeNetBtn() {
  203. setTimeout(() => {
  204. let navWidth = document.querySelector('nav[role="navigation"]').offsetWidth;
  205. addSliderNavDeBtn(navWidth < 245);
  206. let innerDeIcon = document.getElementById('de-btn1');
  207. if (!innerDeIcon) {
  208. let dialogScheduleBtn = _getScheduleDom(false);
  209. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  210. }
  211. }, 800)
  212. }
  213. /**
  214. * 获取推文输入框内dom,用于插入deNet
  215. * @param isDialogInner
  216. * @returns {Element}
  217. * @private
  218. */
  219. function _getScheduleDom(isDialogInner = false) {
  220. let scheduleBtn;
  221. if (isDialogInner) {
  222. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  223. } else {
  224. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  225. if (toolBar) {
  226. scheduleBtn = toolBar.querySelector('div[data-testid="geoButton"]');
  227. }
  228. }
  229. return scheduleBtn;
  230. }
  231. /**
  232. * 插入iframe到页面
  233. * @private
  234. */
  235. function _addIframe() {
  236. // let span = document.createElement('span');
  237. // const shadowRoot = span.attachShadow({mode: 'closed'})
  238. let iframe = document.createElement('iframe');
  239. iframe.src = chrome.runtime.getURL('/iframe/publish.html')
  240. iframe.id = 'iframe-content'
  241. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:10000; border: medium none;display:none';
  242. // shadowRoot.appendChild(iframe);
  243. // document.body.appendChild(span)
  244. dom.iframe = iframe;
  245. let iframeContent = document.getElementById('iframe-content');
  246. if (!iframeContent) {
  247. document.getElementById('layers').appendChild(iframe);
  248. }
  249. }
  250. /**
  251. * 点击deNet按钮处理
  252. * @private
  253. */
  254. function _deNetBtnClick() {
  255. getUserInfo((res) => {
  256. if (res) {
  257. if (window.location.pathname != '/home') {
  258. if (!dom.homeBtn) {
  259. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  260. }
  261. dom.homeBtn.click();
  262. }
  263. showGiveDialogHandler(res);
  264. } else {
  265. let loadIcon = document.getElementById('de-btn-loading');
  266. if (loadIcon) {
  267. return;
  268. }
  269. dom.deBtn.insertBefore(dom.loadingImg, dom.deBtn.querySelector('span'));
  270. setTimeout(() => {
  271. dom.loadingImg.style.transform = 'rotate(1080deg)'
  272. });
  273. setTimeout(() => {
  274. dom.loadingImg.style.transform = 'rotate(0deg)'
  275. dom.deBtn.innerHTML = '<span>DeNet<span>';
  276. }, 2000)
  277. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_LOGIN", data: '1' }, (res) => { console.log(res) })
  278. }
  279. })
  280. }
  281. /**
  282. * 设置发布内容
  283. * @param content
  284. * @private
  285. */
  286. let isSetContent = false;
  287. const _setPublishContent = throttle(function (content) {
  288. if (!isSetContent) {
  289. isSetContent = true;
  290. setTimeout(() => {
  291. document.execCommand("insertText", false, content);
  292. setTimeout(() => {
  293. isSetContent = false;
  294. }, 2000)
  295. }, 1000);
  296. }
  297. }, 800);
  298. /**
  299. * 创建deNet按钮 添加到页面
  300. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  301. * @private
  302. */
  303. function _createBtnDom() {
  304. let loadingImg = document.createElement('img');
  305. loadingImg.id = 'de-btn-loading'
  306. loadingImg.src = require("@/assets/img/icon-btn-loading.png");
  307. loadingImg.style.cssText = 'width:20px;height: 20px;margin-right:3px;transition-duration: 3s;';
  308. let style = document.createElement('style');
  309. style.innerHTML = "#de-btn:hover{opacity: .9;};@-webkit-keyframes load{from{ transform: rotate(0deg);} to{transform: rotate(360deg);}}";
  310. document.getElementsByTagName('head').item(0).appendChild(style);
  311. // 左侧大屏按钮
  312. let deBtn = document.createElement('span');
  313. // const shadowDiv = document.createElement('div');
  314. deBtn.innerHTML = '<span>DeNet</span>';
  315. deBtn.id = 'de-btn';
  316. 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;';
  317. // 编辑框内按钮
  318. const deBtn1 = document.createElement('img');
  319. let src = require("@/assets/img/icon-gift-pack.png");
  320. const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;padding: 0px 8px';
  321. deBtn1.id = 'de-btn1';
  322. deBtn1.style.cssText = smallDeBtnStyle;
  323. deBtn1.src = src
  324. const deBtn2 = document.createElement('div');
  325. deBtn2.id = 'de-btn2';
  326. deBtn2.style.cssText = smallDeBtnStyle;
  327. deBtn2.src = "@/assets/img/icon-gift-pack.png"
  328. // 小屏按钮
  329. const deBtn3 = document.createElement('img');
  330. deBtn3.id = 'de-btn3'
  331. deBtn3.src = require("@/assets/logo/128.png");
  332. deBtn3.style.cssText = 'width:52px;height: 52px;margin-top:20px;cursor: pointer;';
  333. deBtn.addEventListener('click', () => {
  334. // chrome.runtime.sendMessage({
  335. // actionType: 'CONTENT_SET_BADGE',
  336. // data: {
  337. // text: '2'
  338. // }
  339. // }, res => {
  340. // console.log(res);
  341. // })
  342. Report.reportLog({
  343. pageSource: Report.pageSource.mainPage,
  344. businessType: Report.businessType.buttonClick,
  345. objectType: Report.objectType.buttonMain
  346. });
  347. _deNetBtnClick();
  348. })
  349. deBtn1.addEventListener('click', () => {
  350. Report.reportLog({
  351. pageSource: Report.pageSource.mainPage,
  352. businessType: Report.businessType.buttonClick,
  353. objectType: Report.objectType.buttonSecond
  354. });
  355. _deNetBtnClick();
  356. })
  357. deBtn2.addEventListener('click', () => {
  358. _deNetBtnClick();
  359. })
  360. deBtn3.addEventListener('click', () => {
  361. Report.reportLog({
  362. pageSource: Report.pageSource.mainPage,
  363. businessType: Report.businessType.buttonClick,
  364. objectType: Report.objectType.buttonMain
  365. });
  366. _deNetBtnClick();
  367. })
  368. dom.deBtn = deBtn;
  369. dom.deBtn1 = deBtn1;
  370. dom.deBtn2 = deBtn2;
  371. dom.deBtn3 = deBtn3;
  372. dom.loadingImg = loadingImg;
  373. }
  374. function addSliderNavDeBtn(isSmall = false) {
  375. if (!isSmall) {
  376. let bigDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  377. let deBtn = document.getElementById('de-btn');
  378. if (bigDom && !deBtn) {
  379. bigDom.appendChild(dom.deBtn);
  380. Report.reportLog({
  381. pageSource: Report.pageSource.mainPage,
  382. businessType: Report.businessType.buttonView,
  383. objectType: Report.objectType.buttonMain
  384. });
  385. }
  386. } else {
  387. let smallDom = document.querySelector('a[href="/compose/tweet"]').parentNode.parentNode;
  388. let deBtn3 = document.getElementById('de-btn3');
  389. if (smallDom && !deBtn3) {
  390. smallDom.appendChild(dom.deBtn3);
  391. Report.reportLog({
  392. pageSource: Report.pageSource.mainPage,
  393. businessType: Report.businessType.buttonView,
  394. objectType: Report.objectType.buttonMain
  395. });
  396. }
  397. }
  398. }
  399. function onWindowResize() {
  400. window.onresize = throttle(function () {
  401. try {
  402. if (window.innerWidth < 1273) {
  403. let bigBtn = document.querySelector('#de-btn');
  404. bigBtn && bigBtn.remove();
  405. setTimeout(() => {
  406. addSliderNavDeBtn(true);
  407. })
  408. } else {
  409. let smallBtn = document.querySelector('#de-btn3');
  410. smallBtn && smallBtn.remove();
  411. setTimeout(() => {
  412. addSliderNavDeBtn()
  413. })
  414. }
  415. } catch (e) {
  416. console.log(e)
  417. }
  418. }, 800)
  419. }
  420. function checkHasDeBtn() {
  421. try {
  422. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  423. let innerDeIcon = document.getElementById('de-btn1');
  424. if (toolBar && !innerDeIcon) {
  425. let dialogScheduleBtn = _getScheduleDom(false);
  426. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  427. }
  428. } catch (e) {
  429. console.log(e)
  430. }
  431. }
  432. /**
  433. * 点击发推,后端绑定推特id
  434. */
  435. function bindTwitterArtMethod() {
  436. if (!bindTwitterArt.postId) {
  437. return
  438. }
  439. if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
  440. bindTwitterArt.isBindIng = true;
  441. reportSrcPublishEvent({
  442. params: {
  443. postId: bindTwitterArt.postId,
  444. }
  445. }).then((res) => {
  446. if (res.code == 0) {
  447. Report.reportLog({
  448. objectType: Report.objectType.tweetPostBinded
  449. });
  450. bindTwitterArt.needBind = false;
  451. bindTwitterArt.postId = '';
  452. bindTwitterArt.isBindIng = false;
  453. }
  454. })
  455. }
  456. }
  457. import parseCard from './ParseCard'
  458. // 检测dom改变
  459. // 获取短链接
  460. // 查看本地是否有postid
  461. // 如果有 修改dom 返回
  462. // 如果没有 网络请求
  463. // 获取postid
  464. // 获取twitterid
  465. // 检测当前所有dom 如果没有
  466. let queue_num = 1
  467. export const changeQueueNum = (num = 0) => {
  468. queue_num = queue_num + num
  469. if (queue_num > 5) {
  470. queue_num = 5
  471. }
  472. }
  473. let main_observer = null
  474. function onChangePageMain(targetNode) {
  475. try {
  476. const config = { attributes: false, childList: true, subtree: true };
  477. const callback = (mutationsList, observer) => {
  478. changeQueueNum(1)
  479. }
  480. main_observer = new MutationObserver(callback);
  481. main_observer.observe(targetNode, config);
  482. } catch (error) {
  483. main_observer = null
  484. }
  485. }
  486. // 1.监听main改变
  487. // 2.监听卡片是否可见
  488. // 3.如果可见了 去找
  489. function setIframeRedPacket(type = 'twitter') {
  490. // 获取所有卡片参数
  491. let card_json_data
  492. switch(type) {
  493. case 'facebook':
  494. card_json_data = parseCard.parseFacebookCardParmas()
  495. for (let i in card_json_data) {
  496. parseCard.replaceFacebookPacket(card_json_data[i])
  497. }
  498. break;
  499. default:
  500. card_json_data = parseCard.parseAllDeNetCardParmas()
  501. // 过滤出可以请求的短链接
  502. parseCard.getCardParmas(card_json_data).then((res) => {
  503. for (let i in res.has_post_Id_card_data) {
  504. parseCard.replaceDOMRedPacket(res.has_post_Id_card_data[i])
  505. }
  506. if (res.need_net_short_url.length > 0) {
  507. // 请求短链接
  508. chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, () => { })
  509. }
  510. })
  511. break;
  512. }
  513. }
  514. // 监听点击发推 按钮绑定事件
  515. // document.addEventListener('click', (e) => {
  516. // try {
  517. // if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
  518. // bindTwitterArtMethod()
  519. // } else if (e.target.closest('div[data-testid=tweetButton]')) {
  520. // bindTwitterArtMethod()
  521. // }
  522. // } catch (error) {
  523. // console.error('error', error)
  524. // }
  525. // })
  526. export function initExecuteScript(changes) {
  527. if (changes.executeScript) {
  528. let item = JSON.parse(changes.executeScript.newValue)
  529. if (item.executeScript) {
  530. init()
  531. }
  532. }
  533. }
  534. const createIframe = ({ url, tweet_Id }, callback) => {
  535. let iframe = document.createElement('iframe')
  536. iframe.id = `tweetId${tweet_Id}`
  537. iframe.src = url
  538. iframe.onload = () => {
  539. callback()
  540. }
  541. }
  542. // 初始化
  543. export function init() {
  544. getDiscordAuthCode();
  545. let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
  546. if (where) {
  547. return
  548. }
  549. twitterPinLogin();
  550. // 渲染dom
  551. let timer = setInterval(() => {
  552. let inTwitter = window.location.href.includes('twitter.com');
  553. let inTwitterNode = document.querySelector('main');
  554. let inFacebook = window.location.href.includes('facebook.com');
  555. let inFacebookNode = document.querySelector('div[role="main"]');
  556. if (inTwitter && inTwitterNode) {
  557. clearInterval(timer)
  558. setInterval(() => {
  559. if (!main_observer) {
  560. onChangePageMain(inTwitterNode)
  561. changeQueueNum(1)
  562. }
  563. if (queue_num <= 0) {
  564. return
  565. }
  566. setIframeRedPacket()
  567. checkHasDeBtn()
  568. changeQueueNum(-1)
  569. }, 1000)
  570. } else if (inFacebook && inFacebookNode) {
  571. clearInterval(timer)
  572. setInterval(() => {
  573. if (!main_observer) {
  574. onChangePageMain(inFacebookNode)
  575. changeQueueNum(1)
  576. }
  577. setIframeRedPacket('facebook')
  578. }, 1000)
  579. }
  580. }, 1000);
  581. renderDom();
  582. checkTwitterTaskState()
  583. getChromeStorage("popupShowPublishDialog", (res) => {
  584. console.log("popupShowPublishDialog", res);
  585. if (res && res.show) {
  586. setTimeout(() => {
  587. showTwitterPublishDialogHandler({
  588. srcContent: res.srcContent,
  589. postId: res.postId
  590. });
  591. }, 1500);
  592. chrome.storage.local.remove("popupShowPublishDialog");
  593. }
  594. });
  595. getChromeStorage("userSettings", (res) => {
  596. addPinedPop();
  597. if (res && !res.isOnToolbar) {
  598. setTimeout(() => {
  599. showPinTips();
  600. chrome.storage.local.remove("userSettings");
  601. }, 800);
  602. }
  603. });
  604. }
  605. // 根据推特id找到dom,完成任务
  606. export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_type = 'like') {
  607. // 1.根据推特ID寻找推文,获取卡片
  608. // 获取所有卡片参数
  609. let card_json_data = parseCard.parseAllDeNetCardParmas()
  610. let result = card_json_data.filter((item) => { return item.tweet_Id == tweet_Id }) || []
  611. if (result.length < 1) {
  612. return
  613. }
  614. result = result[0]
  615. switch (task_type) {
  616. case 'like':
  617. // https://twitter.com/intent/retweet?tweet_id=1525900221628223491
  618. if (result.dom_card) {
  619. result.dom_card.querySelector('div[data-testid=like]').click()
  620. result.dom_card.querySelector('iframe').contentWindow.postMessage({ actionType: 'CONTENT_DONE_TASK', task_type, }, '*');
  621. }
  622. break;
  623. case 'retweet':
  624. if (result.dom_card) {
  625. result.dom_card.querySelector('div[data-testid=retweet]').click()
  626. result.dom_card.querySelector('div[data-testid=retweetConfirm]').click()
  627. }
  628. break
  629. case 'follow':
  630. follow_names.forEach((item) => {
  631. window.open(`https://twitter.com/intent/follow?screen_name=${item}&tweet_Id=${tweet_Id}`)
  632. })
  633. break
  634. }
  635. }
  636. function clickByDataTestId(e, id, callback) {
  637. if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
  638. callback()
  639. } else if (e.target.closest('div[data-testid=' + id + ']')) {
  640. callback()
  641. }
  642. }
  643. // 校验关注推特状态
  644. export function checkTwitterTaskState() {
  645. let task_type = ''
  646. let url = window.location.href
  647. let tweet_Id
  648. let task_data = {
  649. follow_name: ''
  650. }
  651. // 校验当前链接
  652. if (url.includes('https://twitter.com/intent/retweet')) {
  653. task_type = 'retweet'
  654. } else if (url.includes('https://twitter.com/intent/follow')) {
  655. task_type = 'follow'
  656. task_data.follow_name = getQueryString('screen_name')
  657. } else if (url.includes('https://twitter.com/intent/like')) {
  658. task_type = 'like'
  659. } else {
  660. return
  661. }
  662. tweet_Id = getQueryString('tweet_id')
  663. // let root_status
  664. document.body.addEventListener('click', (e) => {
  665. // 点击 确认
  666. clickByDataTestId(e, 'confirmationSheetConfirm', () => {
  667. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  668. })
  669. // 点击取消
  670. clickByDataTestId(e, 'confirmationSheetCancel', () => {
  671. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  672. })
  673. // 点击 蒙层
  674. if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
  675. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
  676. }
  677. }, true)
  678. let timer = setInterval(() => {
  679. // 喜欢
  680. if (document.querySelector('div[data-testid=unlike]') && task_type == 'like') {
  681. clearInterval(timer)
  682. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  683. }
  684. // 转推
  685. if (document.querySelector('div[data-testid=unretweet]') && task_type == 'retweet') {
  686. clearInterval(timer)
  687. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  688. }
  689. // 关注
  690. if (task_type == 'follow') {
  691. let follow_area = document.querySelector('div[data-testid=placementTracking]')
  692. if (follow_area && follow_area.querySelectorAll('div')) {
  693. follow_area = follow_area.querySelectorAll('div')
  694. clearInterval(timer)
  695. for (let i in follow_area) {
  696. if (follow_area[i] && follow_area[i].dataset && follow_area[i].dataset.testid && follow_area[i].dataset.testid.indexOf('unfollow') > 0) {
  697. chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
  698. break
  699. }
  700. }
  701. }
  702. }
  703. }, 1000)
  704. }