소스 검색

[add][读取cookie和receivedIds逻辑]

zhangwei 3 년 전
부모
커밋
511e73ac35
4개의 변경된 파일75개의 추가작업 그리고 31개의 파일을 삭제
  1. 52 28
      src/logic/twitter.js
  2. 9 0
      src/manifest.development.json
  3. 11 1
      src/manifest.production.json
  4. 3 2
      src/server/twitter.js

+ 52 - 28
src/logic/twitter.js

@@ -41,13 +41,38 @@ export function backTwitterPinLoginCode(code) {
             }
         }
     })
-    // 发送请求
-    // token,code
-    twitterLogin(authToken, code).then(res => {
-        if (res.code == 0) {
-            setChromeStorage({ userInfo: JSON.stringify(res.data) })
+    chrome.cookies.getAll(
+        {
+            name: 'received_log',
+            url: 'http:localhost:3000'
+        }, (e = []) => {
+            let _str = '[]'
+            if (e.length > 0) {
+                _str = e[0].value
+            }
+            let _arr = JSON.parse(decodeURIComponent(_str))
+            let receivedIds = []
+            if (_arr.length > 0) {
+                for (let i in _arr) {
+                    receivedIds.push(_arr[i].receivedId)
+                }
+            }
+            // 发送请求
+            // token,code
+            twitterLogin(authToken, code, receivedIds).then(res => {
+                if (res.code == 0) {
+                    setChromeStorage({ userInfo: JSON.stringify(res.data) })
+                    // chrome.cookies.remove(
+                    //     {
+                    //         name: 'received_log',
+                    //         url: 'http:localhost:3000'
+                    //     }
+                    // )
+                }
+            })
         }
-    })
+    )
+
 }
 
 /**
@@ -86,8 +111,8 @@ export function hideIframeHandler() {
 export function showTwitterPublishDialogHandler(publishRes) {
     dom.tweetBtn.click();
     _setPublishContent(publishRes.srcContent);
-    _publishTweetEvent(publishRes.postId ,(twitterArtId) => {
-        if(twitterArtId) {
+    _publishTweetEvent(publishRes.postId, (twitterArtId) => {
+        if (twitterArtId) {
             getUserInfo((userInfo) => {
                 if (userInfo) {
                     srcPublishSuccess({
@@ -235,15 +260,15 @@ function _getTwitterArtId(contentStr, cb) {
 
     let timer = setInterval(() => {
         let artDomList = document.querySelectorAll('article[data-testid="tweet"]');
-        for(let i = 0; i < artDomList.length; i++) {
+        for (let i = 0; i < artDomList.length; i++) {
             let itemChild = artDomList[i].querySelector('.css-1dbjc4n');
-            if(itemChild && itemChild.querySelector('div[lang="und"]')) {
+            if (itemChild && itemChild.querySelector('div[lang="und"]')) {
                 let innerText = itemChild.querySelector('div[lang="und"]').innerText;
                 let regex = new RegExp(contentStr);
-                if(regex.test(innerText)) {
+                if (regex.test(innerText)) {
                     id = itemChild.parentNode.querySelector('a[dir="auto"]').getAttribute('href').split('/status/')[1];
                     clearInterval(timer);
-                    if(id) {
+                    if (id) {
                         cb && cb(id);
                     }
                     break;
@@ -301,11 +326,11 @@ function _createBtnDom(port) {
     shadowRoot.appendChild(shadowDiv);
 
     const deBtn1 = document.createElement('img');
-    let src = require( "../assets/img/icon-gift-pack.png");
+    let src = require("../assets/img/icon-gift-pack.png");
     const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;marign-left:4px;margin-right:4px';
     deBtn1.id = 'de-btn1';
     deBtn1.style.cssText = smallDeBtnStyle;
-    deBtn1.src =src
+    deBtn1.src = src
 
     const deBtn2 = document.createElement('div');
     deBtn2.id = 'de-btn2';
@@ -374,12 +399,12 @@ function replaceDOMRedPacket(_dom, postId) {
     if (!_dom || _dom.parentNode.parentNode.querySelector('iframe')) {
         return
     }
-    
+
     // _dom.parentNode.parentNode.children[0].innerHTML
     _dom.parentNode.parentNode.children[1].innerHTML = ''
     let _iframe = document.createElement('iframe')
     _iframe.id = postId
-    _iframe.src = chrome.runtime.getURL('/iframe/red-packet.html') + '?postId='+postId;
+    _iframe.src = chrome.runtime.getURL('/iframe/red-packet.html') + '?postId=' + postId;
     _iframe.style.cssText = 'border: medium none; width:375px;height:500px;'
     _dom.parentNode.parentNode.children[1].style = 'height:500px'
     _dom.parentNode.parentNode.children[1].appendChild(_iframe)
@@ -393,17 +418,16 @@ export function setIframeRedPacket() {
     //     return
     // }
     parseDOMRedPacket()
-    let _current_top = 0
-    
-    // setInterval(() => {
-    //     parseDOMRedPacket()    
-    // }, 1000)
-    document.addEventListener('scroll', (e) => {
-    if ((elment.scrollTop - _current_top) > 500 || (_current_top - elment.scrollTop) > 500) {
-        _current_top = elment.scrollTop
-        // 1.解析推文
-        // 2.替换推文
+    // let _current_top = 0
+    setInterval(() => {
         parseDOMRedPacket()
-    }
-    })
+    }, 1000)
+    // document.addEventListener('scroll', (e) => {
+    // if ((elment.scrollTop - _current_top) > 500 || (_current_top - elment.scrollTop) > 500) {
+    // _current_top = elment.scrollTop
+    // 1.解析推文
+    // 2.替换推文
+    // parseDOMRedPacket()
+    // }
+    // })
 }

+ 9 - 0
src/manifest.development.json

@@ -20,8 +20,17 @@
             ]
         }
     ],
+    "host_permissions": [
+        "*://*/*"
+      ],
     "permissions":[
         "tabs",
+        "action",
+        "cookies",
+        "declarativeNetRequest",
+        "webNavigation",
+        "activeTab",
+        "scripting",
         "storage"
     ],
     "options_page": "options.html",

+ 11 - 1
src/manifest.production.json

@@ -20,15 +20,25 @@
             ]
         }
     ],
+    "host_permissions": [
+        "*://*/*"
+      ],
     "permissions":[
         "tabs",
+        "action",
+        "cookies",
+        "declarativeNetRequest",
+        "webNavigation",
+        "activeTab",
+        "scripting",
         "storage"
     ],
     "options_page": "options.html",
     "web_accessible_resources": [
         {
           "resources": [
-            "/iframe/test.html"
+            "/iframe/test.html",
+            "/iframe/red-packet.html"
           ],
           "matches": [
             "<all_urls>"

+ 3 - 2
src/server/twitter.js

@@ -28,7 +28,7 @@ export function getTtwitterRequestToken() {
     })
 }
 
-export function twitterLogin(oauthToken, oauthVerifier) {
+export function twitterLogin(oauthToken, oauthVerifier, receivedIds = []) {
     return new Promise(function (resolve, reject) {
         let _url = `${base_url}/user/twitterLogin`
         fetch(_url, {
@@ -40,7 +40,8 @@ export function twitterLogin(oauthToken, oauthVerifier) {
                 "baseInfo": {},
                 "params": {
                     "oauthToken": oauthToken,
-                    "oauthVerifier": oauthVerifier
+                    "oauthVerifier": oauthVerifier,
+                    "receivedIds": receivedIds
                 }
             }),
         })