|
@@ -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) })
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|