소스 검색

[add][twitter][pin]

zhangwei 3 년 전
부모
커밋
2f0d1fd239
6개의 변경된 파일105개의 추가작업 그리고 51개의 파일을 삭제
  1. 13 37
      src/entry/background.js
  2. 13 10
      src/entry/content.js
  3. 58 0
      src/logic/twitter.js
  4. 2 1
      src/manifest.development.json
  5. 4 3
      src/server/twitter.js
  6. 15 0
      src/uilts/chromeExtension.js

+ 13 - 37
src/entry/background.js

@@ -1,42 +1,18 @@
 console.log('hello world background todo something~')
 
-// twitter
-// import { getTtwitterRequestToken,twitterLogin } from '../server/twitter.js'
-
-// getTtwitterRequestToken().then((res)=>{
-//     chrome.tabs.create({
-//         url:`https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
-//     },()=>{
-
-//     })
-// })
-
-// chrome.tabs.onUpdated.addListener((tabId,changeInfo,tab)=>{
-//     // console.log()
-//     if(tab.url == 'https://api.twitter.com/oauth/authorize'){
-
-//     }
-// })
-
-// chrome.tabs.getAllInWindow(
-//     windowId?: number,
-//     callback?: function,
-//   )
-
-
-//   chrome.tabs.remove(
-//     tabIds: number | number[],
-//     callback?: function,
-//   )
+import { backTwitterPinLoginToken ,backTwitterPinLoginCode} from '../logic/twitter.js'
 
 
 // 消息通讯
-// chrome.runtime.onConnect.addListener(function (port) {
-//     port.onMessage.addListener(function (res) {
-//         switch (res.state) {
-//             case 'SEDN_CODE':
-//                 // res.code
-//                 break
-//         }
-//     })
-// })
+chrome.runtime.onConnect.addListener(function (port) {
+    port.onMessage.addListener(function (res) {
+        switch (res.state) {
+            case 'CONTENT_SEND_CODE':
+                backTwitterPinLoginCode(res.code)
+                break
+            case 'CONTENT_TWITTER_LOGIN':
+                backTwitterPinLoginToken()
+                break
+        }
+    })
+})

+ 13 - 10
src/entry/content.js

@@ -1,11 +1,14 @@
-console.log('hello world content todo something~')
-// var port = chrome.runtime.connect({ name: "hello" });
-
-// window.onload = ()=> {
-//     if(window.location.href == 'https://api.twitter.com/oauth/authorize'){
-//         let code = document.querySelector('code')
-//         if(code){
-//             port.postMessage({ state: 'SEND_CODE', code:code.innerText })
-//         }
+var port = chrome.runtime.connect({ name: "hello" });
+
+import { contentTwitterPinLogin,contentAddButton } from '../logic/twitter.js'
+
+// port.onMessage.addListener(function (res) {
+//     switch (res.state) {
+
 //     }
-// }
+// })
+
+window.onload = () => {
+    contentTwitterPinLogin(port)
+    contentAddButton(port)
+}

+ 58 - 0
src/logic/twitter.js

@@ -0,0 +1,58 @@
+import { getChromeStorage, setChromeStorage } from '../uilts/chromeExtension.js'
+import { getTtwitterRequestToken, twitterLogin } from '../server/twitter.js'
+
+export function contentTwitterPinLogin(port) {
+    if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
+        let code = document.querySelector('code')
+
+        if (code) {
+            port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
+        }
+    }
+}
+export function contentAddButton(port) {
+    let button = document.querySelector('input')
+    button.type = 'button'
+    button.value = '123123'
+    button.id = 'id123'
+    button.onclick = () => {
+        port.postMessage({ state: 'CONTENT_TWITTER_LOGIN' })
+    }
+    document.body.appendChild(button)
+}
+
+let authToken = ''
+export function backTwitterPinLoginToken() {
+    // 1.判断是否登陆了
+    getChromeStorage('userInfo', (res) => {
+        // 没有登陆
+        if (!res) {
+            getTtwitterRequestToken().then((res) => {
+                authToken = res.data.authToken
+                chrome.tabs.create({
+                    url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
+                })
+            })
+        }
+    })
+}
+
+export function backTwitterPinLoginCode(code) {
+    // 关闭code页面
+    chrome.tabs.query({}, (tab) => {
+        for (let i in tab) {
+            console.log(tab[i])
+            if (tab[i].url == 'https://api.twitter.com/oauth/authorize') {
+                chrome.tabs.remove(tab[i].id)
+                break
+            }
+        }
+    })
+    // 发送请求
+    // token,code
+    twitterLogin(authToken, code).then(res => {
+        if (res.code == 0) {
+            setChromeStorage({ userInfo: JSON.stringify(res.data) })
+        }
+    })
+}

+ 2 - 1
src/manifest.development.json

@@ -20,7 +20,8 @@
         }
     ],
     "permissions":[
-        "tabs"
+        "tabs",
+        "storage"
     ],
     "options_page": "options.html",
     "web_accessible_resources": [

+ 4 - 3
src/server/twitter.js

@@ -1,11 +1,11 @@
 // twitter专用api
 // import axios from 'axios'
 
-let base_url = 'http://192.168.80.44:8080'
+let base_url = 'http://192.168.80.44:8080/denet'
 
 export function getTtwitterRequestToken() {
     return new Promise(function (resolve, reject) {
-        let _url = `${base_url}/denet/user/twitterRequestToken`
+        let _url = `${base_url}/user/twitterRequestToken`
         fetch(_url, {
             method: 'POST', // or 'PUT'
             headers: {
@@ -54,4 +54,5 @@ export function twitterLogin(oauthToken, oauthVerifier) {
             });
 
     })
-}
+}
+

+ 15 - 0
src/uilts/chromeExtension.js

@@ -0,0 +1,15 @@
+export function setChromeStorage(params) {
+    chrome.storage.local.set(params)
+}
+
+export function getChromeStorage(key = '',callback) {
+    let params = {}
+    params[key] = ''
+    chrome.storage.local.get(params, (item) => {
+        if (item[key]) {
+            callback(JSON.parse(item[key]))
+        }else{
+            callback(null)
+        }
+    });
+}