twitter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* eslint-disable */
  2. import { getChromeStorage, setChromeStorage } from '../uilts/chromeExtension.js'
  3. import { getTtwitterRequestToken, twitterLogin } from '../server/twitter.js'
  4. import { srcPublishSuccess } from '../http/publishApi'
  5. let dom = {};
  6. export function contentTwitterPinLogin(port) {
  7. if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
  8. let code = document.querySelector('code')
  9. if (code) {
  10. port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
  11. }
  12. }
  13. }
  14. let authToken = ''
  15. export function backTwitterPinLoginToken() {
  16. // 1.判断是否登陆了
  17. getChromeStorage('userInfo', (res) => {
  18. // 没有登陆
  19. if (!res) {
  20. getTtwitterRequestToken().then((res) => {
  21. authToken = res.data.authToken
  22. chrome.tabs.create({
  23. url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
  24. })
  25. })
  26. }
  27. })
  28. }
  29. export function backTwitterPinLoginCode(code) {
  30. // 关闭code页面
  31. chrome.tabs.query({}, (tab) => {
  32. for (let i in tab) {
  33. console.log(tab[i])
  34. if (tab[i].url == 'https://api.twitter.com/oauth/authorize') {
  35. chrome.tabs.remove(tab[i].id)
  36. }
  37. }
  38. })
  39. // 发送请求
  40. // token,code
  41. twitterLogin(authToken, code).then(res => {
  42. if (res.code == 0) {
  43. setChromeStorage({ userInfo: JSON.stringify(res.data) })
  44. }
  45. })
  46. }
  47. /**
  48. * 渲染要插入的dom,初始化逻辑
  49. * @param port
  50. */
  51. export function renderDom(port) {
  52. if (window.location.href.indexOf('https://twitter.com') > -1) {
  53. _createBtnDom(port);
  54. setTimeout(() => {
  55. _addIframe();
  56. _addDeNetBtn();
  57. _getSliderTwitterBtn();
  58. }, 800)
  59. }
  60. }
  61. /**
  62. * 展示give弹窗
  63. */
  64. export function showGiveDialogHandler(userInfo) {
  65. dom.iframe.contentWindow.postMessage({ actionType: 'CONTENT_SHOW_GIVE_DIALOG', userInfo }, '*');
  66. }
  67. export function showIframeHandler() {
  68. dom.iframe.style.display = 'block';
  69. }
  70. export function hideIframeHandler() {
  71. dom.iframe.style.display = 'none';
  72. }
  73. /**
  74. * 展示twitter原生发布框
  75. */
  76. export function showTwitterPublishDialogHandler(publishRes) {
  77. dom.tweetBtn.click();
  78. _setPublishContent(publishRes.srcContent);
  79. _publishTweetEvent(publishRes.postId ,(twitterArtId) => {
  80. if(twitterArtId) {
  81. getUserInfo((userInfo) => {
  82. if (userInfo) {
  83. srcPublishSuccess({
  84. baseInfo: {
  85. token: userInfo.accessToken
  86. },
  87. params: {
  88. postId: publishRes.postId,
  89. srcContentId: twitterArtId
  90. }
  91. }).then((res) => {
  92. })
  93. }
  94. })
  95. }
  96. });
  97. }
  98. function getUserInfo(cb) {
  99. getChromeStorage('userInfo', (res) => {
  100. cb && cb(res);
  101. })
  102. }
  103. /**
  104. * 监听dialog内点击原生发布按钮事件
  105. * @private
  106. */
  107. function _publishTweetEvent(contentStr, cb) {
  108. setTimeout(() => {
  109. let publishTweetBtn = document.querySelector('div[role="dialog"]').querySelector('div[data-testid="tweetButton"]');
  110. publishTweetBtn.addEventListener('click', function () {
  111. setTimeout(() => {
  112. _getTwitterArtId(contentStr, (twitterArtId) => {
  113. cb && cb(twitterArtId)
  114. });
  115. }, 1500)
  116. });
  117. }, 800)
  118. }
  119. /**
  120. * 在输入推文区插入deNet按钮
  121. * @param parent
  122. * @param dom
  123. * @param isClick
  124. * @private
  125. */
  126. function _addDeNetEditBtn(parent, dom, isClick = false) {
  127. setTimeout(() => {
  128. if (parent) {
  129. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  130. } else {
  131. setTimeout(() => {
  132. parent = _getScheduleDom(isClick);
  133. parent.parentNode.insertBefore(dom, parent.nextElementSibling);
  134. }, 1000)
  135. }
  136. })
  137. }
  138. /**
  139. * 在dialog插入deNet按钮
  140. * @private
  141. */
  142. function _addDeNetBtnToDialog() {
  143. setTimeout(() => {
  144. let dialogScheduleBtn = _getScheduleDom(true);
  145. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  146. }, 800)
  147. }
  148. /**
  149. * 获取左侧twitter按钮
  150. * @private
  151. */
  152. function _getSliderTwitterBtn() {
  153. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  154. dom.tweetBtn.addEventListener('click', function () {
  155. // _addDeNetBtnToDialog();
  156. })
  157. }
  158. /**
  159. * 添加deNet按钮
  160. * @private
  161. */
  162. function _addDeNetBtn() {
  163. setTimeout(() => {
  164. let node = document.querySelector('header[role="banner"]').querySelector('.r-1habvwh');
  165. if (node) {
  166. node.appendChild(dom.deBtn);
  167. }
  168. let dialogScheduleBtn = _getScheduleDom(false);
  169. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  170. }, 800)
  171. }
  172. /**
  173. * 获取推文输入框内dom,用于插入deNet
  174. * @param isDialogInner
  175. * @returns {Element}
  176. * @private
  177. */
  178. function _getScheduleDom(isDialogInner = false) {
  179. let scheduleBtn;
  180. if (isDialogInner) {
  181. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  182. } else {
  183. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  184. if (toolBar) {
  185. scheduleBtn = toolBar.querySelector('div[data-testid="geoButton"]');
  186. }
  187. }
  188. return scheduleBtn;
  189. }
  190. /**
  191. * 插入iframe到页面
  192. * @private
  193. */
  194. function _addIframe() {
  195. // let span = document.createElement('span');
  196. // const shadowRoot = span.attachShadow({mode: 'closed'})
  197. let iframe = document.createElement('iframe');
  198. iframe.src = chrome.runtime.getURL('/iframe/test.html')
  199. iframe.id = 'iframe-test'
  200. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:0; border: medium none;display:none';
  201. // shadowRoot.appendChild(iframe);
  202. // document.body.appendChild(span)
  203. dom.iframe = iframe;
  204. document.getElementById('layers').appendChild(iframe)
  205. }
  206. /**
  207. * 获取发布推文id
  208. * @returns {string}
  209. * @private
  210. */
  211. function _getTwitterArtId(contentStr, cb) {
  212. // let firstTwitterArtId = document.querySelector('article[data-testid="tweet"]').querySelector('a[dir="auto"]').getAttribute('href');
  213. // return firstTwitterArtId.split('/status/')[1];
  214. let id = '';
  215. let timer = setInterval(() => {
  216. let artDomList = document.querySelectorAll('article[data-testid="tweet"]');
  217. for(let i = 0; i < artDomList.length; i++) {
  218. let itemChild = artDomList[i].querySelector('.css-1dbjc4n');
  219. if(itemChild && itemChild.querySelector('div[lang="und"]')) {
  220. let innerText = itemChild.querySelector('div[lang="und"]').innerText;
  221. let regex = new RegExp(contentStr);
  222. if(regex.test(innerText)) {
  223. id = itemChild.parentNode.querySelector('a[dir="auto"]').getAttribute('href').split('/status/')[1];
  224. clearInterval(timer);
  225. if(id) {
  226. cb && cb(id);
  227. }
  228. break;
  229. }
  230. }
  231. }
  232. }, 1000);
  233. }
  234. /**
  235. * 点击deNet按钮处理
  236. * @private
  237. */
  238. function _deNetBtnClick(port) {
  239. getUserInfo((res) => {
  240. if (res) {
  241. if (window.location.pathname != '/home') {
  242. if (!dom.homeBtn) {
  243. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  244. }
  245. dom.homeBtn.click();
  246. }
  247. showGiveDialogHandler(res);
  248. } else {
  249. port.postMessage({ state: 'CONTENT_TWITTER_LOGIN' })
  250. }
  251. })
  252. }
  253. /**
  254. * 设置发布内容
  255. * @param content
  256. * @private
  257. */
  258. function _setPublishContent(content) {
  259. setTimeout(() => {
  260. document.execCommand("insertText", false, '');
  261. document.execCommand("insertText", false, content);
  262. }, 1300);
  263. }
  264. /**
  265. * 创建deNet按钮 添加到页面
  266. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  267. * @private
  268. */
  269. function _createBtnDom(port) {
  270. let deBtn = document.createElement('span');
  271. const shadowRoot = deBtn.attachShadow({ mode: 'closed' })
  272. const shadowDiv = document.createElement('div');
  273. shadowDiv.innerText = 'DeNet';
  274. shadowDiv.id = 'de-btn';
  275. shadowDiv.style.cssText = 'width:220px;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:15px;font-weight:700;border-radius:100px;cursor: pointer;';
  276. shadowRoot.appendChild(shadowDiv);
  277. const deBtn1 = document.createElement('img');
  278. let src = require( "../assets/img/icon-gift-pack.png");
  279. const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;marign-left:4px;margin-right:4px';
  280. deBtn1.id = 'de-btn1';
  281. deBtn1.style.cssText = smallDeBtnStyle;
  282. deBtn1.src =src
  283. const deBtn2 = document.createElement('div');
  284. deBtn2.id = 'de-btn2';
  285. deBtn2.style.cssText = smallDeBtnStyle;
  286. deBtn2.src = "../assets/img/icon-gift-pack.png"
  287. deBtn.addEventListener('click', () => {
  288. _deNetBtnClick(port);
  289. })
  290. deBtn1.addEventListener('click', () => {
  291. _deNetBtnClick(port);
  292. })
  293. deBtn2.addEventListener('click', () => {
  294. _deNetBtnClick(port);
  295. })
  296. dom.deBtn = deBtn;
  297. dom.deBtn1 = deBtn1;
  298. dom.deBtn2 = deBtn2;
  299. }
  300. /* setInterval(() => {
  301. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  302. let innerDeIcon = document.getElementById('de-icon');
  303. if (toolBar && !innerDeIcon) {
  304. // let dialogScheduleBtn = _getScheduleDom(false);
  305. // _addDeNetEditBtn(dialogScheduleBtn, deBtn1);
  306. }
  307. }, 800) */
  308. function parseDOMRedPacket() {
  309. let _dom = null
  310. // let denet_arr = document.querySelector('article[data-testid=tweet]').querySelectorAll('a')
  311. // let _dom = null
  312. // for (let i in denet_arr) {
  313. // if (denet_arr[i].innerText == '#DeNet') {
  314. // _dom = denet_arr[i].parentNode.parentNode.parentNode.parentNode.children[1]
  315. // // 是否有有红包秘文
  316. // }
  317. // }
  318. // //
  319. // denet_arr = document.querySelector('section').querySelectorAll('a')
  320. // for (let i in denet_arr) {
  321. // if (denet_arr[i].innerText == '#DeNet') {
  322. // _dom = denet_arr[i].parentNode
  323. // // 是否有有红包秘文
  324. // }
  325. // }
  326. // return _dom
  327. let arr = document.querySelectorAll('a') || []
  328. arr.forEach((item) => {
  329. if (item.innerText == '#DeNet') {
  330. if (item.parentNode && item.parentNode.parentNode && item.parentNode.parentNode.innerText.length > 5) {
  331. let _postId = item.parentNode.parentNode.innerText || ''
  332. _postId = _postId.match(/###([\s\S]*?)###/)[1];
  333. _dom = item.parentNode.parentNode
  334. replaceDOMRedPacket(_dom, _postId)
  335. }
  336. }
  337. })
  338. }
  339. function replaceDOMRedPacket(_dom, postId) {
  340. if (!_dom || _dom.parentNode.parentNode.querySelector('iframe')) {
  341. return
  342. }
  343. // _dom.parentNode.parentNode.children[0].innerHTML
  344. _dom.parentNode.parentNode.children[1].innerHTML = ''
  345. let _iframe = document.createElement('iframe')
  346. _iframe.id = postId
  347. _iframe.src = chrome.runtime.getURL('/iframe/red-packet.html') + '?postId='+postId;
  348. _iframe.style.cssText = 'border: medium none; width:375px;height:500px;'
  349. _dom.parentNode.parentNode.children[1].style = 'height:500px'
  350. _dom.parentNode.parentNode.children[1].appendChild(_iframe)
  351. }
  352. export function setIframeRedPacket() {
  353. // let elment = document.documentElement
  354. // if(window.location.href != 'https://twitter.com/home'){
  355. // return
  356. // }
  357. // parseDOMRedPacket()
  358. // let _current_top = 0
  359. setInterval(() => {
  360. parseDOMRedPacket()
  361. }, 1000)
  362. // document.addEventListener('scroll', (e) => {
  363. // if ((elment.scrollTop - _current_top) > 500 || (_current_top - elment.scrollTop) > 500) {
  364. // _current_top = elment.scrollTop
  365. // // 1.解析推文
  366. // // 2.替换推文
  367. // parseDOMRedPacket()
  368. // }
  369. // })
  370. }