twitter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.srcContent ,(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. let twitterArtId = _getTwitterArtId(contentStr);
  113. cb && cb(twitterArtId)
  114. }, 1500)
  115. });
  116. }, 800)
  117. }
  118. /**
  119. * 在输入推文区插入deNet按钮
  120. * @param parent
  121. * @param dom
  122. * @param isClick
  123. * @private
  124. */
  125. function _addDeNetEditBtn(parent, dom, isClick = false) {
  126. setTimeout(() => {
  127. if (parent) {
  128. parent.parentNode.insertBefore(dom, parent.nextSibling);
  129. } else {
  130. setTimeout(() => {
  131. parent = _getScheduleDom(isClick);
  132. parent.parentNode.insertBefore(dom, parent.nextSibling);
  133. }, 1000)
  134. }
  135. })
  136. }
  137. /**
  138. * 在dialog插入deNet按钮
  139. * @private
  140. */
  141. function _addDeNetBtnToDialog() {
  142. setTimeout(() => {
  143. let dialogScheduleBtn = _getScheduleDom(true);
  144. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn2);
  145. }, 800)
  146. }
  147. /**
  148. * 获取左侧twitter按钮
  149. * @private
  150. */
  151. function _getSliderTwitterBtn() {
  152. dom.tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  153. dom.tweetBtn.addEventListener('click', function () {
  154. // _addDeNetBtnToDialog();
  155. })
  156. }
  157. /**
  158. * 添加deNet按钮
  159. * @private
  160. */
  161. function _addDeNetBtn() {
  162. setTimeout(() => {
  163. let node = document.querySelector('header[role="banner"]').querySelector('.r-1habvwh');
  164. if (node) {
  165. node.appendChild(dom.deBtn);
  166. }
  167. let dialogScheduleBtn = _getScheduleDom(false);
  168. _addDeNetEditBtn(dialogScheduleBtn, dom.deBtn1);
  169. }, 800)
  170. }
  171. /**
  172. * 获取推文输入框内dom,用于插入deNet
  173. * @param isDialogInner
  174. * @returns {Element}
  175. * @private
  176. */
  177. function _getScheduleDom(isDialogInner = false) {
  178. let scheduleBtn;
  179. if (isDialogInner) {
  180. scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
  181. } else {
  182. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  183. if (toolBar) {
  184. scheduleBtn = toolBar.querySelector('div[data-testid="createPollButton"]');
  185. }
  186. }
  187. return scheduleBtn;
  188. }
  189. /**
  190. * 插入iframe到页面
  191. * @private
  192. */
  193. function _addIframe() {
  194. // let span = document.createElement('span');
  195. // const shadowRoot = span.attachShadow({mode: 'closed'})
  196. let iframe = document.createElement('iframe');
  197. iframe.src = chrome.runtime.getURL('/iframe/test.html')
  198. iframe.id = 'iframe-test'
  199. iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:0; border: medium none;display:none';
  200. // shadowRoot.appendChild(iframe);
  201. // document.body.appendChild(span)
  202. dom.iframe = iframe;
  203. document.getElementById('layers').appendChild(iframe)
  204. }
  205. /**
  206. * 获取发布推文id
  207. * @returns {string}
  208. * @private
  209. */
  210. function _getTwitterArtId() {
  211. let firstTwitterArtId = document.querySelector('article[data-testid="tweet"]').querySelector('a[dir="auto"]').getAttribute('href');
  212. return firstTwitterArtId.split('/status/')[1];
  213. }
  214. /**
  215. * 点击deNet按钮处理
  216. * @private
  217. */
  218. function _deNetBtnClick(port) {
  219. getUserInfo((res) => {
  220. if (res) {
  221. if(window.location.pathname != '/home') {
  222. if(!dom.homeBtn) {
  223. dom.homeBtn = document.querySelector('a[data-testid="AppTabBar_Home_Link"]');
  224. }
  225. dom.homeBtn.click();
  226. }
  227. showGiveDialogHandler(res);
  228. } else {
  229. port.postMessage({ state: 'CONTENT_TWITTER_LOGIN' })
  230. }
  231. })
  232. }
  233. /**
  234. * 设置发布内容
  235. * @param content
  236. * @private
  237. */
  238. function _setPublishContent(content) {
  239. setTimeout(() => {
  240. document.execCommand("insertText", false, content);
  241. });
  242. }
  243. /**
  244. * 创建deNet按钮 添加到页面
  245. * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
  246. * @private
  247. */
  248. function _createBtnDom(port) {
  249. let deBtn = document.createElement('span');
  250. const shadowRoot = deBtn.attachShadow({ mode: 'closed' })
  251. const shadowDiv = document.createElement('div');
  252. shadowDiv.innerText = 'DeNet';
  253. shadowDiv.id = 'de-btn';
  254. 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;';
  255. shadowRoot.appendChild(shadowDiv);
  256. const deBtn1 = document.createElement('div');
  257. const smallDeBtnStyle = 'width:30px;height: 30px;text-align:center;line-height:30px;background: linear-gradient(274.8deg, #FF9900 -3.69%, #BD00FF 69.71%, #00F0FF 122.65%);color:#fff;font-size:12px;font-weight:500;border-radius:4px;cursor: pointer;';
  258. deBtn1.innerText = 'De';
  259. deBtn1.id = 'de-btn1';
  260. deBtn1.style.cssText = smallDeBtnStyle;
  261. const deBtn2 = document.createElement('div');
  262. deBtn2.innerText = 'De';
  263. deBtn2.id = 'de-btn2';
  264. deBtn2.style.cssText = smallDeBtnStyle;
  265. deBtn.addEventListener('click', () => {
  266. _deNetBtnClick(port);
  267. })
  268. deBtn1.addEventListener('click', () => {
  269. _deNetBtnClick(port);
  270. })
  271. deBtn2.addEventListener('click', () => {
  272. _deNetBtnClick(port);
  273. })
  274. dom.deBtn = deBtn;
  275. dom.deBtn1 = deBtn1;
  276. dom.deBtn2 = deBtn2;
  277. }
  278. /* setInterval(() => {
  279. let toolBar = document.querySelector('div[data-testid="toolBar"]');
  280. let innerDeIcon = document.getElementById('de-icon');
  281. if (toolBar && !innerDeIcon) {
  282. // let dialogScheduleBtn = _getScheduleDom(false);
  283. // _addDeNetEditBtn(dialogScheduleBtn, deBtn1);
  284. }
  285. }, 800) */
  286. function parseDOMRedPacket() {
  287. let denet_arr = document.querySelector('article[data-testid=tweet]').querySelectorAll('a')
  288. let _dom = null
  289. for (let i in denet_arr) {
  290. if (denet_arr[i].innerText == '#denet') {
  291. _dom = denet_arr[i].parentNode.parentNode.parentNode.parentNode.children[1]
  292. // 是否有有红包秘文
  293. }
  294. }
  295. return _dom
  296. }
  297. function replaceDOMRedPacket(_dom) {
  298. if (!_dom || _dom.querySelector('iframe')) {
  299. return
  300. }
  301. _dom.innerHTML = ''
  302. let _iframe = document.createElement('iframe')
  303. _iframe.src = chrome.runtime.getURL('/iframe/red-packet.html');
  304. _iframe.style.cssText = 'border: medium none; width:375px;height:500px;'
  305. _dom.appendChild(_iframe)
  306. }
  307. export function setIframeRedPacket() {
  308. let elment = document.documentElement
  309. // if(window.location.href != 'https://twitter.com/home'){
  310. // return
  311. // }
  312. let _current_top = 0
  313. document.addEventListener('scroll', (e) => {
  314. if ((elment.scrollTop - _current_top) > 500 || (_current_top - elment.scrollTop) > 500) {
  315. _current_top = elment.scrollTop
  316. // 1.解析推文
  317. // 2.替换推文
  318. replaceDOMRedPacket(parseDOMRedPacket())
  319. }
  320. })
  321. }