Browse Source

[edit][多次弹出授权问题]

zhangwei 2 years ago
parent
commit
bb99a42a15
2 changed files with 50 additions and 46 deletions
  1. 29 2
      src/logic/background/twitter.js
  2. 21 44
      src/logic/content/twitter.js

+ 29 - 2
src/logic/background/twitter.js

@@ -2,22 +2,49 @@ import { fetchTtwitterRequestToken, fetchTwitterLogin, fetchTwitterShortUrl, fet
 import { LANDING_PAGE, LANDING_PAGE_MID, setChromeStorage, setChromeCookie, getChromeCookie, getChromeStorage, removeChromeCookie } from '@/uilts/chromeExtension.js'
 import { guid } from '@/uilts/help.js'
 import { pageUrl, discordAuthRedirectUri } from '@/http/configAPI'
+import { trueDependencies } from 'mathjs'
 
 let authToken = ''
 let consumerKey = ''
+let tab_flag = true
+
+const isHasTabByUrl = (url, callback) => {
+    let has = false
+    chrome.tabs.query({}, (tab) => {
+        for (let i in tab) {
+            if (tab[i].url.indexOf(url) >= 0) {
+                has = true
+                break
+            }
+        }
+        callback(has)
+    })
+}
+
 export function twitterPinLoginToken() {
     // 1.判断是否登陆了
     getChromeStorage('userInfo', (res) => {
         // 没有登陆
         if (!res) {
+            if(tab_flag == false){
+                return
+            }
+            tab_flag = false
             fetchTtwitterRequestToken().then((res) => {
+                tab_flag = true
                 if (res.code == 0) {
                     authToken = res.data.authToken
                     consumerKey = res.data.consumerKey
-                    chrome.tabs.create({
-                        url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
+                    isHasTabByUrl('https://api.twitter.com/oauth/authorize?oauth_token', (has) => {
+                        if (!has) {
+                            chrome.tabs.create({
+                                url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
+                            })
+                        }
                     })
                 }
+            }).catch(()=>{
+                tab_flag = true
             })
         }
     })

+ 21 - 44
src/logic/content/twitter.js

@@ -672,7 +672,13 @@ export function findTweetByIdDoTask({ tweet_Id = '', follow_names = [] }, task_t
     }
 }
 
-
+function clickByDataTestId(e, id, callback) {
+    if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == id) {
+        callback()
+    } else if (e.target.closest('div[data-testid=' + id + ']')) {
+        callback()
+    }
+}
 
 // 校验关注推特状态 
 export function checkTwitterTaskState() {
@@ -695,49 +701,20 @@ export function checkTwitterTaskState() {
     }
     tweet_Id = getQueryString('tweet_id')
 
-    // 获取关注名字 获取推特Id    
-    let confirm_btn = document.querySelector('div[data-testid=confirmationSheetConfirm]')
-    let confirm_btn_status
-    let cancel_btn = document.querySelector('div[data-testid=confirmationSheetCancel]')
-    let cancel_btn_status
-    
-    // let page_follow_btn = document.querySelector('div[aria-describedby]')
-    // let page_follow_btn_status
-
-    let observer = new MutationObserver(() => {
-        if (confirm_btn && !confirm_btn_status) {
-            confirm_btn_status = true
-            confirm_btn.onclick = (e) => {
-                chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data ,task_done:true}, () => { })
-            }
-        } else {
-            confirm_btn = document.querySelector('div[data-testid=confirmationSheetConfirm]')
-        }
-        if(cancel_btn && !cancel_btn_status){
-            cancel_btn_status= true
-            cancel_btn.onclick = (e) => {
-                chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data ,task_done:false}, () => { })
-            }
-        }else{
-            cancel_btn = document.querySelector('div[data-testid=confirmationSheetCancel]')
-        }
-        // if (page_follow_btn && page_follow_btn.dataset && page_follow_btn.dataset.testid && !page_follow_btn_status) {
-        //     page_follow_btn_status = true
-        //     if (page_follow_btn.dataset.testid.includes('unfollow')) {
-        //         // 发送
-        //         chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id }, () => { })
-        //     }
-        // } else {
-        //     page_follow_btn = document.querySelector('div[aria-describedby]')
-        // }
-
-        if (confirm_btn_status && cancel_btn_status) {
-            observer.disconnect()
+    // let root_status
+    document.body.addEventListener('click', (e) => {
+        // 点击 确认
+        clickByDataTestId(e, 'confirmationSheetConfirm', () => {
+            chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: true }, () => { })
+        })
+        // 点击取消
+        clickByDataTestId(e, 'confirmationSheetCancel', () => {
+            chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
+        })
+        // 点击 蒙层
+        if (e.target && e.target.nextSibling && e.target.nextSibling.dataset && e.target.nextSibling.dataset.testid == 'confirmationSheetDialog') {
+            chrome.runtime.sendMessage({ actionType: "DO_TASK", tweet_Id, task_type, task_data, task_done: false }, () => { })
         }
-    });
-    observer.observe(document.body, { attributes: false, childList: true, subtree: true });
+    }, true)
 
-    // 打开iframe 
-    // 点击操作
-    // 删除iframe
 }