Przeglądaj źródła

[bug][调用多次init函数问题]

zhangwei 2 lat temu
rodzic
commit
c921694483
1 zmienionych plików z 60 dodań i 49 usunięć
  1. 60 49
      src/logic/content/twitter.js

+ 60 - 49
src/logic/content/twitter.js

@@ -73,7 +73,7 @@ export function hideIframeHandler() {
  * 展示twitter原生发布框
  */
 let tweetPublishStore = {
-    showPublishDialog : false
+    showPublishDialog: false
 }
 export function showTwitterPublishDialogHandler(publishRes) {
     let bigBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
@@ -126,7 +126,7 @@ function addPinedPop() {
     popWrapper.appendChild(contentDom);
 
     let layersDom = document.getElementById('layers');
-    if(layersDom) {
+    if (layersDom) {
         layersDom.appendChild(popWrapper);
     } else {
         document.querySelector('body').appendChild(popWrapper);
@@ -192,33 +192,33 @@ function onClosePublishDialogHandle(dom, params) {
     dom.querySelector('div[role="group"]').addEventListener('click', function () {
         setTimeout(() => {
             let parent = document.querySelector('div[data-testid="confirmationSheetDialog"]');
-             if(parent) {
-                 let btnArr = parent.querySelectorAll('div[role=button]')
-                 for(let i = 0; i < btnArr.length; i++) {
-                     let btn = btnArr[i];
-                     btn.addEventListener('click', function() {
+            if (parent) {
+                let btnArr = parent.querySelectorAll('div[role=button]')
+                for (let i = 0; i < btnArr.length; i++) {
+                    let btn = btnArr[i];
+                    btn.addEventListener('click', function () {
                         tweetPublishStore.showPublishDialog = false;
                         let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
-                        noticeBindTweet({ postId: params.postId, taskLuckdropId});
-                     })
-                 }
-             } else {
-                 setTimeout(() => {
+                        noticeBindTweet({ postId: params.postId, taskLuckdropId });
+                    })
+                }
+            } else {
+                setTimeout(() => {
                     let dialog = document.querySelector('div[role="dialog"]');
-                    if(!dialog) {
+                    if (!dialog) {
                         tweetPublishStore.showPublishDialog = false;
                     }
-                 }, 800)
-             }
+                }, 800)
+            }
         }, 1000)
-     })
+    })
 }
 
 function checkIsShowReSend(dom, params) {
     let str = dom.querySelector('div[data-contents="true"]').innerHTML;
     if (str.indexOf(params.postId) < 0) {
         let taskLuckdropId = JSON.parse(params.postBizData).taskLuckdropId;
-        noticeBindTweet({ postId: params.postId, taskLuckdropId});
+        noticeBindTweet({ postId: params.postId, taskLuckdropId });
     }
 }
 
@@ -331,17 +331,17 @@ function _addIframe() {
     let iframeContent = document.getElementById('iframe-content');
     if (!iframeContent) {
         let layersDom = document.getElementById('layers');
-        if(layersDom) {
+        if (layersDom) {
             layersDom.appendChild(iframe);
         } else {
             document.querySelector('body').appendChild(iframe);
         }
-        
+
     }
 }
 
 function addPublishTipsIframe(params = {}) {
-    let {time = 1000} = params;
+    let { time = 1000 } = params;
     setTimeout(() => {
         let dialog = document.querySelector('div[role="dialog"]').querySelector('div[role="dialog"]')
 
@@ -421,7 +421,7 @@ const _setPublishContent = throttle(function (content) {
     if (!isSetContent) {
         isSetContent = true;
         let inputEle = document.querySelector('div[contenteditable="true"]');
-        if(inputEle) {
+        if (inputEle) {
             inputEle.focus();
         }
         setTimeout(() => {
@@ -546,20 +546,20 @@ function addSliderNavDeBtn(isSmall = false) {
 function onWindowResize() {
     window.onresize = throttle(function () {
         try {
-            if(tweetPublishStore.showPublishDialog) {
+            if (tweetPublishStore.showPublishDialog) {
                 let dialog = document.querySelector('div[role="dialog"]');
                 let dePublishTips = document.getElementById('de-publish-tips');
-                if(dialog && !dePublishTips) {
-                    addPublishTipsIframe({time: 0});
-                } else if(dialog && dePublishTips){
+                if (dialog && !dePublishTips) {
+                    addPublishTipsIframe({ time: 0 });
+                } else if (dialog && dePublishTips) {
                     let dialogContent = dialog.querySelector('div[role=dialog]')
-                    if(dialogContent) {
+                    if (dialogContent) {
                         let right = dialogContent.offsetLeft - 15 - 266;
                         dePublishTips.style.right = right + 'px';
                     }
                 }
             }
-            
+
             if (window.innerWidth < 1273) {
                 let bigBtn = document.querySelector('#de-btn');
                 bigBtn && bigBtn.remove();
@@ -717,16 +717,8 @@ const createIframe = ({ url, tweet_Id }, callback) => {
     }
 }
 
-// 初始化
-export function init() {
-    getDiscordAuthCode();
-    let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
-    if (where) {
-        return
-    }
-    twitterPinLogin();
-    // 渲染dom
 
+function initParseCard() {
     let timer = setInterval(() => {
         let inTwitter = window.location.href.includes('twitter.com');
         let inTwitterNode = document.querySelector('main');
@@ -762,6 +754,25 @@ export function init() {
             }, 1000)
         }
     }, 1000);
+}
+let inited = false
+// 初始化
+export function init() {
+    if(inited){
+        return 
+    }
+    inited = true
+    console.log('init')
+
+    getDiscordAuthCode();
+    let where = window.location.href.indexOf('twitter.com') < 0 && window.location.href.indexOf('facebook.com') < 0;
+    if (where) {
+        return
+    }
+    twitterPinLogin();
+    // 渲染dom
+    initParseCard()
+
     renderDom();
     checkTwitterTaskState();
 
@@ -794,7 +805,7 @@ export function init() {
 function checkHasSliderDeBtn() {
     let deBtn = document.getElementById('de-btn');
     let deBtn3 = document.getElementById('de-btn3');
-    if(!deBtn && !deBtn3) {
+    if (!deBtn && !deBtn3) {
         addSliderNavDeBtn();
     }
 }
@@ -827,12 +838,12 @@ export function replyHandle(params) {
     let pathNameArr = window.location.pathname.split('/');
     if (pathNameArr.length >= 2 && pathNameArr[pathNameArr.length - 2] == 'status') {
         let tweetReply = document.querySelector('div[data-testid="tweetButtonInline"]');
-        if(tweetReply) {
-            tweetReply.addEventListener('click', function() {
+        if (tweetReply) {
+            tweetReply.addEventListener('click', function () {
                 let eleList = tweetReply.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll('span[data-text="true"]');
                 reportReplyResult(eleList, params, () => {
                     // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
-                    chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data : {postId: params.postId}}, () => { })
+                    chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
                 });
             })
         }
@@ -853,36 +864,36 @@ export function onTweetReplyClick(params) {
 function onReplyDialogOpen(params, iframe) {
     setTimeout(() => {
         let dialog = document.querySelector('div[role="dialog"]');
-        let replyBtn; 
-        
+        let replyBtn;
+
         if (dialog) {
-            let dialogContent  = dialog.querySelector('div[role="dialog"]');
+            let dialogContent = dialog.querySelector('div[role="dialog"]');
             replyBtn = dialog.querySelector('div[data-testid="toolBar"]').querySelector('div[data-testid="tweetButton"]');
-            if(dialogContent) {
+            if (dialogContent) {
                 let width = dialogContent.offsetWidth;
                 let ele = document.createElement('div');
                 ele.innerText = 'Tag 3 friends to complete the task';
                 ele.style.cssText = `width: ${width}px; height: 38px; color: #fff; font-weight: 600;
                 font-size: 16px; display: flex; align-items: center; justify-content: center; background: #1D9BF0;
                 opacity: 0.8; position: absolute; top: 18px; left: 50%; transform: translateX(-50%); z-index: 1000`;
-    
+
                 dialogContent.style.top = '80px';
                 dialogContent.parentNode.appendChild(ele);
             }
         } else {
             dialog = document.querySelector('main[role="main"]');
-            if(dialog) {
+            if (dialog) {
                 replyBtn = dialog.querySelector('div[data-testid="tweetButton"]');
             }
         }
 
-        if(dialog && replyBtn) {
+        if (dialog && replyBtn) {
             replyBtn.addEventListener('click', function () {
                 let eleList = dialog.querySelector('div[contenteditable="true"]').querySelectorAll('span[data-text="true"]');
                 reportReplyResult(eleList, params, () => {
                     // 上報完成
                     // iframe.contentWindow.postMessage({ actionType: 'CONTENT_RED_PACKET_REPLY_RASK_FINSH', data: {} }, '*');
-                    chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data : {postId: params.postId}}, () => { })
+                    chrome.runtime.sendMessage({ actionType: "CONTENT_RED_PACKET_REPLY_RASK_FINSH", data: { postId: params.postId } }, () => { })
                 })
             });
         }
@@ -1027,7 +1038,7 @@ export function checkTwitterTaskState() {
 
 export function getTweetAuthorByDom(params) {
     let iframe = document.getElementById(params.postId);
-    if(!iframe) {
+    if (!iframe) {
         return;
     }
     let fullNameDom;