Forráskód Böngészése

[edit][bug for card]

zhangwei 3 éve
szülő
commit
426eadcd7b

+ 3 - 1
src/entry/background.js

@@ -24,7 +24,9 @@ chrome.runtime.onConnect.addListener(function (port) {
                 break;
             case "CONTENT_TWITTER_SHORT_LINK":
                 res.arr_url.forEach(item => {
-                    twitterShortUrl(item)
+                    if(item){
+                        twitterShortUrl(port,item)
+                    }
                 });
                 break
         }

+ 6 - 1
src/entry/content.js

@@ -10,7 +10,8 @@ import {
     showTwitterPublishDialogHandler,
     showPinTips,
     init,
-    initExecuteScript
+    initExecuteScript,
+    addChangeNum
 } from "@/logic/content/twitter.js";
 
 
@@ -19,6 +20,10 @@ port.onMessage.addListener(function (res) {
         case "BACK_TWITTER_LOGIN_SUCCESS":
             showGiveDialogHandler();
             break;
+        case 'BACK_TWITTER_SHORT_URL':
+            console.log('addChangeNum')
+            addChangeNum()
+            break
     }
 });
 

+ 33 - 36
src/logic/background/twitter.js

@@ -58,47 +58,44 @@ export function twitterPinLoginCode(port, code) {
 
 }
 
-export function twitterShortUrl(url) {
-    return new Promise(function (resolve, reject) {
-        fetchTwitterShortUrl(url).then(res => {
-            let str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
-            let post_Id = str_arr[1] || ''
-            if (!post_Id) {
-                return
-            }
-            // 解析
-            let _obj = {
-                short_url: url,
-                post_Id
-            }
-            console.log('123', _obj)
-            getChromeStorage('cardData', item => {
-                if (item) {
-                    let has = false
-                    for (let i in item) {
-                        if (item[i].short_url == _obj.short_url) {
-                            item[i].short_url = _obj.short_url
-                            item[i].post_Id = _obj.post_Id
-                            setChromeStorage({ cardData: JSON.stringify(item) })
-                            has = true
-                            break
-                        }
-                    }
-                    if (!has) {
-                        item.push(_obj)
+export function twitterShortUrl(port, url) {
+    fetchTwitterShortUrl(url).then(res => {
+        let str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
+        let post_Id = str_arr[1] || ''
+        if (!post_Id) {
+            return
+        }
+        // 解析
+        let _obj = {
+            short_url: url,
+            post_Id
+        }
+        getChromeStorage('cardData', item => {
+            if (item) {
+                let has = false
+                for (let i in item) {
+                    if (item[i].short_url == _obj.short_url) {
+                        item[i].short_url = _obj.short_url
+                        item[i].post_Id = _obj.post_Id
                         setChromeStorage({ cardData: JSON.stringify(item) })
+                        has = true
+                        break
                     }
-
-                } else {
-                    setChromeStorage({ cardData: JSON.stringify([_obj]) })
                 }
-                resolve({
-                    post_Id
-                })
-            })
+                if (!has) {
+                    item.push(_obj)
+                    setChromeStorage({ cardData: JSON.stringify(item) })
+                }
+
+            } else {
+                setChromeStorage({ cardData: JSON.stringify([_obj]) })
+            }
+            console.log('BACK_TWITTER_SHORT_URL')
+            port.postMessage({
+                state: "BACK_TWITTER_SHORT_URL",
+            });
         })
     })
-
 }
 
 

+ 41 - 33
src/logic/content/twitter.js

@@ -495,16 +495,14 @@ import parseCard from './ParseCard'
 
 
 
-let change_time = new Date().getTime()
+let change_num = 0
 function onChangePageMain(port, targetNode) {
     const config = { attributes: false, childList: true, subtree: true };
     let article = null
     const callback = (mutationsList, observer) => {
         for (let i in mutationsList) {
             if (mutationsList[i].target && mutationsList[i].target.querySelector('article')) {
-                setTimeout(()=>{
-                    setIframeRedPacket(port)
-                },1000)
+                change_num++
                 break
             }
         }
@@ -521,36 +519,33 @@ function onChangePageMain(port, targetNode) {
 function setIframeRedPacket(port) {
     // let elment = document.documentElement
 
-    if (window.location.href.includes('twitter.com)')) {
-        return
-    }
-    let new_time = new Date().getTime()
-    if(new_time - change_time < 1000){
-        return
-    }
-    change_time = new_time
-  
-    let stop_time = 0
-    let timer = setInterval(() => {
-        stop_time++
-        // 获取所有卡片参数
-        let card_json_data = parseCard.parseAllDeNetCardParmas()
-        // 过滤出可以请求的短链接
-        parseCard.getCardParmas(card_json_data).then((res) => {
-            for (let i in res.has_post_Id_card_data) {
-                parseCard.replaceDOMRedPacket(res.has_post_Id_card_data[i])
-            }
-            if (res.need_net_short_url.length > 0) {
-                // 请求短链接
-                port.postMessage({ state: 'CONTENT_TWITTER_SHORT_LINK', arr_url: res.need_net_short_url })
-            }
-            if (card_json_data.length == res.has_post_Id_card_data.length || stop_time >= 5) {
-                clearInterval(timer)
-            }
-        })
-    }, 1000)
+
+
+    // let new_time = new Date().getTime()
+    // console.log('new_time',new_time)
+    // if (new_time - change_time < 1000) {
+    //     return
+    // }
+
+    // change_time = new_time
+    // 获取所有卡片参数
+    let card_json_data = parseCard.parseAllDeNetCardParmas()
+    // 过滤出可以请求的短链接
+    parseCard.getCardParmas(card_json_data).then((res) => {
+        for (let i in res.has_post_Id_card_data) {
+            parseCard.replaceDOMRedPacket(res.has_post_Id_card_data[i])
+        }
+        if (res.need_net_short_url.length > 0) {
+            // 请求短链接
+            port.postMessage({ state: 'CONTENT_TWITTER_SHORT_LINK', arr_url: res.need_net_short_url })
+        }
+        // card_json_data.length == res.has_post_Id_card_data.length || 
+    })
+
 }
 
+
+
 export function initExecuteScript(port, changes) {
     if (changes.executeScript) {
         let item = JSON.parse(changes.executeScript.newValue)
@@ -559,7 +554,9 @@ export function initExecuteScript(port, changes) {
         }
     }
 }
-
+export function addChangeNum() {
+    change_num++
+}
 // 初始化
 export function init(port) {
     if (window.location.href.indexOf('twitter.com') < 0) {
@@ -573,6 +570,17 @@ export function init(port) {
         if (targetNode) {
             clearInterval(timer)
             onChangePageMain(port, targetNode)
+            setInterval(() => {
+                if (window.location.href.includes('twitter.com)')) {
+                    return
+                }
+                console.log('change_num',change_num)
+                if (change_num <= 0) {
+                    return
+                }
+                change_num = change_num - 1
+                setIframeRedPacket(port)
+            }, 1000)
         }
     }, 1000);
 

+ 6 - 4
src/view/iframe/red-packet/red-packet.vue

@@ -530,10 +530,12 @@ function init() {
             srcContentId: data.tweetId
           }
         }).then((res) => {
-          Report.reportLog({
-            objectType: Report.objectType.tweetPostBinded
-          });
-          init()
+          if (res.code == 0) {
+            Report.reportLog({
+              objectType: Report.objectType.tweetPostBinded
+            });
+            init()
+          }
         })
         return
       }