nft.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { getOffsetRect, nextTick } from '@/uilts/help'
  2. import { listJoinNftGroup } from '@/http/nft';
  3. import { getChromeStorage } from '@/uilts/chromeExtension.js'
  4. import { _setPublishContent, publishNFTTweetPost, bindTwitterArt, bindTwitterArtMethod } from './twitter';
  5. import { jumpTwitterDetailByAlert } from '@/logic/content/help/twitter.js'
  6. import Report from "@/log-center/log"
  7. var ifShowNftGroup = false;
  8. var tempNftGroupPost = null;
  9. var groupImgNoSelect = require("@/assets/img/icon-nft-group-entry.png");
  10. var groupImgSelect = require("@/assets/img/icon-nft-group-select.png");
  11. export const showNFTGroupIcon = () => {
  12. let urlInfo = new URL(window.location.href)
  13. let isTwitter = urlInfo.hostname === 'twitter.com'
  14. let toolElem = document.querySelector('div[data-testid="toolBar"]');
  15. let isAppend = toolElem && toolElem.querySelector('#de-nft-group-enter');
  16. let where = isTwitter && toolElem && !isAppend && ifShowNftGroup;
  17. if (where) {
  18. let oDiv = document.createElement(`div`);
  19. oDiv.id = 'de-nft-group-enter';
  20. let oImg = document.createElement('img');
  21. oImg.src = groupImgNoSelect;
  22. oImg.className = 'addGroup';
  23. oDiv.innerHTML = `
  24. ${oImg.outerHTML}
  25. <style>
  26. #de-nft-group-enter {position:relative; display:flex; padding:0 8px;}
  27. #de-nft-group-enter .addGroup {cursor:pointer; height:32px;}
  28. </style>
  29. `;
  30. oDiv.addEventListener('click', (e) => {
  31. showNFTGroupList(e);
  32. e.stopPropagation();
  33. // report
  34. Report.reportLog({
  35. pageSource: Report.pageSource.mainPage,
  36. businessType: Report.businessType.buttonClick,
  37. objectType: Report.objectType.buttonSecond
  38. }, {
  39. type: 2
  40. });
  41. })
  42. toolElem.firstChild.appendChild(oDiv)
  43. // report
  44. Report.reportLog({
  45. pageSource: Report.pageSource.mainPage,
  46. businessType: Report.businessType.buttonView,
  47. objectType: Report.objectType.buttonSecond
  48. }, {
  49. type: 2
  50. });
  51. }
  52. }
  53. export const showNFTGroupList = (e) => {
  54. let rectObject = e.target.getBoundingClientRect();
  55. let { top, left, height } = rectObject;
  56. let oTop = top + height + 10;
  57. // 居底判断
  58. let wHeight = document.body.offsetHeight || document.body.clientHeight;
  59. if ((top + height + 290) > wHeight) oTop = top - 290;
  60. let iframe = document.createElement('iframe');
  61. iframe.src = 'chrome-extension://omadcdhfdfhbklafpaddghnjimpfemgh' + (`/iframe/nft-group.html`)
  62. iframe.style.cssText = 'border:medium none; width:315px; height:260px;';
  63. let html = document.createElement('div');
  64. html.id = 'de-nft-group-list';
  65. html.innerHTML = `
  66. <div class="de-nft-group-div">
  67. ${iframe.outerHTML}
  68. </div>
  69. <div class="de-nft-group-mask"></div>
  70. <style>
  71. .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);}
  72. .de-nft-group-mask {position:fixed; z-index:99; width:100%; height:100%; left:0; top:0;}
  73. </style>`;
  74. document.body.appendChild(html);
  75. document.querySelector('.de-nft-group-mask').addEventListener('click', () => {
  76. hideNFTGroupList();
  77. })
  78. }
  79. export const hideNFTGroupList = () => {
  80. let dom = document.querySelector('#de-nft-group-list');
  81. dom && document.body.removeChild(dom);
  82. }
  83. export const checkUserJoinGroup = (fn) => {
  84. getChromeStorage('userInfo', (res) => {
  85. // 如果登录
  86. if (res) {
  87. listJoinNftGroup({
  88. params: {
  89. pageNum: 1,
  90. pageSize: 1
  91. }
  92. }).then(res => {
  93. let { data = [] } = res;
  94. if (data !== null && data.length > 0) {
  95. ifShowNftGroup = true;
  96. if (fn) fn()
  97. }
  98. })
  99. }
  100. })
  101. }
  102. export const clearPostContent = (fn) => {
  103. let edit = document.querySelector('div[contenteditable="true"]')
  104. let where = (edit.innerText === '\n') || (edit.innerText === '')
  105. if (where) {
  106. fn()
  107. } else {
  108. nextTick(() => {
  109. let inputEle = document.querySelector('div[contenteditable="true"]');
  110. if (inputEle) {
  111. inputEle.focus();
  112. }
  113. }, 20).then(() => {
  114. document.execCommand('selectAll');
  115. document.execCommand('delete');
  116. clearPostContent(fn)
  117. })
  118. }
  119. }
  120. export const setPostContent = (res) => {
  121. nextTick(() => {
  122. let inputEle = document.querySelector('div[contenteditable="true"]');
  123. if (inputEle) {
  124. inputEle.focus();
  125. }
  126. setGroupIconStatus();
  127. }, 100).then(() => {
  128. _setPublishContent(res.srcContent + ' ');
  129. })
  130. }
  131. export const endPostContent = () => {
  132. return new Promise((resolve) => {
  133. let inputEle = document.querySelector('div[contenteditable="true"]');
  134. let range = document.createRange();
  135. range.selectNodeContents(inputEle);
  136. range.collapse(false);
  137. let sel = window.getSelection();
  138. sel.removeAllRanges();
  139. sel.addRange(range);
  140. resolve()
  141. })
  142. }
  143. export const setNFTGroupContent = (res) => {
  144. tempNftGroupPost = res;
  145. let dialogDiv = document.querySelectorAll('div[role="dialog"]');
  146. if (dialogDiv.length === 0) {
  147. let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
  148. if (bigBtn) {
  149. bigBtn.click();
  150. } else {
  151. let smallBtn = document.querySelector('a[href="/compose/tweet"]')
  152. smallBtn && smallBtn.click();
  153. }
  154. }
  155. let edit = document.querySelector('div[contenteditable="true"]')
  156. let where = (edit.innerText === '\n') || (edit.innerText === '')
  157. if (where) {
  158. setPostContent(res)
  159. nextTick(() => {
  160. _addTweetButtonListen()
  161. }, 100)
  162. } else {
  163. endPostContent().then(() => {
  164. let inputEle = document.querySelector('div[contenteditable="true"]');
  165. if (inputEle) {
  166. inputEle.focus();
  167. }
  168. setPostContent(res)
  169. nextTick(() => {
  170. _addTweetButtonListen()
  171. }, 100)
  172. })
  173. }
  174. }
  175. export const elemAddEventListener = (elem, action, fn) => {
  176. if (elem) {
  177. elem.removeEventListener(action, fn)
  178. elem.addEventListener(action, fn)
  179. }
  180. }
  181. export const addJoinedGroupList = () => {
  182. if (ifShowNftGroup) {
  183. let { pathname } = window.location;
  184. let iframe = document.createElement('iframe');
  185. iframe.id = 'de-joined-group-list';
  186. iframe.src = 'chrome-extension://omadcdhfdfhbklafpaddghnjimpfemgh' + ('/iframe/joined-group-list.html');
  187. iframe.style.cssText = `border: medium none;height: 120px;border-radius: 16px;margin-bottom: 16px`
  188. let iframeContent = document.getElementById('de-joined-group-list');
  189. if (!iframeContent && pathname == '/home') {
  190. let sidebarColumn = document.querySelector('div[data-testid="sidebarColumn"]');
  191. if (sidebarColumn) {
  192. let searchDom = sidebarColumn.querySelector('form[role="search"]');
  193. if (searchDom) {
  194. let listWrapperDom = searchDom.parentElement.parentElement.parentElement.parentElement;
  195. if (listWrapperDom) {
  196. let listParent = listWrapperDom.parentElement;
  197. if (listParent) {
  198. listParent.insertBefore(iframe, listWrapperDom.nextElementSibling.nextElementSibling);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. };
  206. export const setJoinedGroupIframeStyle = (params) => {
  207. let { height = '321px' } = params;
  208. let iframeContent = document.getElementById('de-joined-group-list');
  209. if (iframeContent) {
  210. iframeContent.style.height = height;
  211. }
  212. }
  213. export function setGroupIconStatus() {
  214. let editElem = document.querySelector('div[contenteditable="true"]');
  215. let main_observer = new MutationObserver(() => {
  216. let groupImg = document.querySelector('#de-nft-group-enter');
  217. let innerText = editElem.innerText || '';
  218. if (groupImg && innerText) {
  219. let where = innerText.indexOf('#DNFT') !== -1 && innerText.indexOf('nft_group') !== -1;
  220. if (where) {
  221. groupImg.querySelector('.addGroup').src = groupImgSelect;
  222. } else {
  223. groupImg.querySelector('.addGroup').src = groupImgNoSelect;
  224. }
  225. }
  226. });
  227. main_observer.observe(editElem, { attributes: false, childList: true, subtree: true })
  228. }
  229. function _addTweetButtonListen() {
  230. let btn = document.querySelector('div[data-testid="tweetButton"]');
  231. btn.removeEventListener('click', _postTweetContent);
  232. btn.addEventListener('click', _postTweetContent)
  233. }
  234. function _postTweetContent() {
  235. if (tempNftGroupPost && tempNftGroupPost.groupId) {
  236. publishNFTTweetPost(tempNftGroupPost)
  237. bindTwitterArt.needBind = true;
  238. bindTwitterArt.postId = tempNftGroupPost.postId;
  239. bindTwitterArtMethod();
  240. // 非首页处理
  241. let homeTag = document.querySelectorAll('div[data-testid="toolBar"]');
  242. if (homeTag.length === 1) {
  243. jumpTwitterDetailByAlert()
  244. }
  245. }
  246. }