nft.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { getOffsetRect, nextTick } from '@/uilts/help'
  2. import { listJoinNftGroup } from '@/http/nft';
  3. import { getChromeStorage } from '@/uilts/chromeExtension.js'
  4. import { _setPublishContent, publishNFTTweetPost } from './twitter';
  5. import { jumpTwitterDetailByAlert } from '@/logic/content/help/twitter.js'
  6. var ifShowNftGroup = false;
  7. var tempNftGroupPost = null;
  8. export const showNFTGroupIcon = () => {
  9. let urlInfo = new URL(window.location.href)
  10. let isTwitter = urlInfo.hostname === 'twitter.com'
  11. let toolElem = document.querySelector('div[data-testid="toolBar"]');
  12. let isAppend = toolElem && toolElem.querySelector('#de-nft-group-enter');
  13. let where = isTwitter && toolElem && !isAppend && ifShowNftGroup;
  14. if (where) {
  15. let oDiv = document.createElement(`div`);
  16. oDiv.id = 'de-nft-group-enter';
  17. let oImg = document.createElement('img');
  18. oImg.src = require("@/assets/img/icon-nft-group-entry.png");
  19. oImg.className = 'addGroup';
  20. oDiv.innerHTML = `
  21. ${oImg.outerHTML}
  22. <style>
  23. #de-nft-group-enter {position:relative; display:flex; padding:0 8px;}
  24. #de-nft-group-enter .addGroup {cursor:pointer; height:32px;}
  25. </style>
  26. `;
  27. oDiv.addEventListener('click', (e) => {
  28. showNFTGroupList(e);
  29. e.stopPropagation();
  30. })
  31. toolElem.firstChild.appendChild(oDiv)
  32. }
  33. }
  34. export const showNFTGroupList = (e) => {
  35. let { top, left } = getOffsetRect(e.target);
  36. let oTop = top + e.target.offsetHeight + 10;
  37. // 居底判断
  38. let wHeight = document.body.offsetHeight || document.body.clientHeight;
  39. if ((top + e.target.offsetHeight + 290) > wHeight) oTop = top - 290;
  40. let iframe = document.createElement('iframe');
  41. iframe.src = chrome.runtime.getURL(`/iframe/nft-group.html`)
  42. iframe.style.cssText = 'border:medium none; width:315px; height:260px;';
  43. let html = document.createElement('div');
  44. html.id = 'de-nft-group-list';
  45. html.innerHTML = `
  46. <div class="de-nft-group-div">
  47. ${iframe.outerHTML}
  48. </div>
  49. <div class="de-nft-group-mask"></div>
  50. <style>
  51. .de-nft-group-div {position:fixed; display:flex; align-items:center; justify-content:center; z-index:100; width:350px; height:280px; left:${left}px; top:${oTop}px; background:#fff; border-radius:20px; box-shadow:0px 4px 20px 0px rgba(0, 0, 0, 0.26);}
  52. .de-nft-group-mask {position:fixed; z-index:99; width:100%; height:100%; left:0; top:0;}
  53. </style>`;
  54. document.body.appendChild(html);
  55. document.querySelector('.de-nft-group-mask').addEventListener('click', () => {
  56. hideNFTGroupList();
  57. })
  58. }
  59. export const hideNFTGroupList = () => {
  60. let dom = document.querySelector('#de-nft-group-list');
  61. dom && document.body.removeChild(dom);
  62. }
  63. export const checkUserJoinGroup = (fn) => {
  64. getChromeStorage('userInfo', (res) => {
  65. // 如果登录
  66. if (res) {
  67. listJoinNftGroup({
  68. params: {
  69. pageNum: 1,
  70. pageSize: 1
  71. }
  72. }).then(res => {
  73. let { data = [] } = res;
  74. if (data !== null && data.length > 0) {
  75. ifShowNftGroup = true;
  76. if (fn) fn()
  77. }
  78. })
  79. }
  80. })
  81. }
  82. export const clearPostContent = (fn) => {
  83. let edit = document.querySelector('div[contenteditable="true"]')
  84. let where = (edit.innerText === '\n') || (edit.innerText === '')
  85. if (where) {
  86. fn()
  87. } else {
  88. nextTick(() => {
  89. let inputEle = document.querySelector('div[contenteditable="true"]');
  90. if (inputEle) {
  91. inputEle.focus();
  92. }
  93. }, 20).then(() => {
  94. document.execCommand('selectAll');
  95. document.execCommand('delete');
  96. clearPostContent(fn)
  97. })
  98. }
  99. }
  100. export const setPostContent = (res) => {
  101. nextTick(() => {
  102. let inputEle = document.querySelector('div[contenteditable="true"]');
  103. if (inputEle) {
  104. inputEle.focus();
  105. }
  106. }, 100).then(() => {
  107. _setPublishContent(res.srcContent);
  108. })
  109. }
  110. export const setNFTGroupContent = (res) => {
  111. tempNftGroupPost = res;
  112. let dialogDiv = document.querySelectorAll('div[role="dialog"]');
  113. if (dialogDiv.length === 0) {
  114. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  115. if (bigBtn) {
  116. bigBtn.click();
  117. } else {
  118. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  119. smallBtn && smallBtn.click();
  120. }
  121. }
  122. let edit = document.querySelector('div[contenteditable="true"]')
  123. let where = (edit.innerText === '\n') || (edit.innerText === '')
  124. if (where) {
  125. setPostContent(res)
  126. nextTick(() => {
  127. _addTweetButtonListen()
  128. }, 100)
  129. } else {
  130. clearPostContent(() => {
  131. let inputEle = document.querySelector('div[contenteditable="true"]');
  132. if (inputEle) {
  133. inputEle.focus();
  134. }
  135. setPostContent(res)
  136. nextTick(() => {
  137. _addTweetButtonListen()
  138. }, 100)
  139. })
  140. }
  141. }
  142. export const elemAddEventListener = (elem, action, fn) => {
  143. if (elem) {
  144. elem.addEventListener(action, fn)
  145. }
  146. }
  147. export const addJoinedGroupList = () => {
  148. if(ifShowNftGroup) {
  149. let {pathname} = window.location;
  150. let iframe = document.createElement('iframe');
  151. iframe.id = 'de-joined-group-list';
  152. iframe.src = chrome.runtime.getURL('/iframe/joined-group-list.html');
  153. iframe.style.cssText = `border: medium none;width: 350px;height: 120px;border-radius: 16px;margin-bottom: 16px`
  154. let iframeContent = document.getElementById('de-joined-group-list');
  155. if (!iframeContent && pathname == '/home') {
  156. let sidebarColumn = document.querySelector('div[data-testid="sidebarColumn"]');
  157. if(sidebarColumn) {
  158. let searchDom = sidebarColumn.querySelector('form[role="search"]');
  159. if(searchDom) {
  160. let listWrapperDom = searchDom.parentElement.parentElement.parentElement.parentElement;
  161. if(listWrapperDom) {
  162. let listParent = listWrapperDom.parentElement;
  163. if(listParent) {
  164. listParent.insertBefore(iframe, listWrapperDom.nextElementSibling.nextElementSibling);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. };
  172. export const setJoinedGroupIframeStyle = (params) => {
  173. let {height = '321px'} = params;
  174. let iframeContent = document.getElementById('de-joined-group-list');
  175. if(iframeContent) {
  176. iframeContent.style.height = height;
  177. }
  178. }
  179. function _addTweetButtonListen() {
  180. let btn = document.querySelector('div[data-testid="tweetButton"]');
  181. btn.removeEventListener('click', _postTweetContent);
  182. btn.addEventListener('click', _postTweetContent)
  183. }
  184. function _postTweetContent() {
  185. if (tempNftGroupPost && tempNftGroupPost.groupId) {
  186. publishNFTTweetPost(tempNftGroupPost)
  187. // 非首页处理
  188. let jumpUrl = new URL(tempNftGroupPost['entryUrl'] || '');
  189. let pathname = jumpUrl.pathname || '';
  190. if (pathname !== '/home') {
  191. jumpTwitterDetailByAlert()
  192. }
  193. }
  194. }