PUGE 4 лет назад
Родитель
Сommit
2ec250bada
5 измененных файлов с 83 добавлено и 62 удалено
  1. 0 5
      background.js
  2. 46 39
      js/options.js
  3. 19 12
      js/popup.js
  4. 2 1
      manifest.json
  5. 16 5
      popup.html

+ 0 - 5
background.js

@@ -8,11 +8,6 @@ if (localStorage.getItem('owoOpenList')) {
   openList = JSON.parse(localStorage.getItem('owoOpenList'))
 }
 
-function saveUser (username, password) {
-  localStorage.setItem('owoUsername', username)
-  localStorage.setItem('owoPassword', password)
-}
-
 function saveOpenList () {
   localStorage.setItem('owoOpenList', JSON.stringify(openList))
 }

+ 46 - 39
js/options.js

@@ -1,20 +1,10 @@
 //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
 var bg = chrome.extension.getBackgroundPage();
+
 const serverUrl = 'https://going.run/assist'
-// 登录按钮点击
-document.getElementsByClassName('login-button')[0].onclick = function () {
-	const username = document.getElementById('username').value
-  const password = document.getElementById('password').value
-  if (!username || !password) {
-    chrome.notifications.create(null, {
-      type: 'basic',
-      iconUrl: 'img/48.png',
-      title: '运行方案',
-      message: '用户名或密码不能为空!'
-    })
-    return
-  }
-	fetch(`http://going.run/userServer?route=login`, {
+
+function login (username, password) {
+  fetch(`https://user.hanshu.run/login`, {
     method: 'POST',
     headers: {
       "Content-Type": "application/json"
@@ -27,32 +17,49 @@ document.getElementsByClassName('login-button')[0].onclick = function () {
   }).then((response) => {return response.json();}).then((res) => {
     if (res.err === 0) {
       bg.userInfo = res.data
-      if (bg) bg.saveUser(username, password)
-      location.reload();
+      chrome.storage.sync.set({userInfo: {username, password, session: bg.userInfo.session}})
+      document.querySelector('.login-box').style.display = 'none'
+      let newHtml = `<table border="0"><thead><tr><th>编号</th><th>脚本名称</th><th>开启状态</th></tr></thead><tbody>`
+      fetch(`https://going.run/assist?route=getList&username=${username}&session=${bg.userInfo.session}`).then((response) => {return response.json();}).then((res) => {
+        console.log(bg.openList)
+        res.data.forEach(element => {
+          newHtml += `<tr><td>${element.id}</td><td>${element.name}</td><td><input type="checkbox" key="${element.id}" id="check${element.id}" name="check${element.id}" ${bg.openList[element.id] ? 'checked' : ''}></td></tr>`
+        });
+        newHtml += `</tbody></table>`
+        document.querySelector('#selectTable').innerHTML = newHtml
+        document.querySelector('#selectTable').style.display = 'block'
+        setTimeout(() => {
+          document.querySelectorAll('table input').forEach(element => {
+            element.onchange = function (e) {
+              const itemId = e.target.getAttribute("key")
+              bg.openList[itemId] = !bg.openList[itemId]
+              bg.saveOpenList()
+            }
+          });
+        }, 0);
+      })
     }
   })
 }
-
-if (bg && bg.userInfo) {
-  document.querySelector('.login-box').style.display = 'none'
-  let newHtml = `<table border="0"><thead><tr><th>编号</th><th>脚本名称</th><th>开启状态</th></tr></thead><tbody>`
-  fetch('https://going.run/assist?route=getList').then((response) => {return response.json();}).then((res) => {
-    console.log(bg.openList)
-    res.data.forEach(element => {
-      newHtml += `<tr><td>${element.id}</td><td>${element.name}</td><td><input type="checkbox" key="${element.id}" id="check${element.id}" name="check${element.id}" ${bg.openList[element.id] ? 'checked' : ''}></td></tr>`
-    });
-    newHtml += `</tbody></table>`
-    document.querySelector('#selectTable').innerHTML = newHtml
-    document.querySelector('#selectTable').style.display = 'block'
-    setTimeout(() => {
-      document.querySelectorAll('table input').forEach(element => {
-        element.onchange = function (e) {
-          const itemId = e.target.getAttribute("key")
-          bg.openList[itemId] = !bg.openList[itemId]
-          bg.saveOpenList()
-        }
-      });
-    }, 0);
-  })
-  
+chrome.storage.sync.get('userInfo', function(data) {
+  console.log(data)
+  const userInfo = data.userInfo
+  if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
+    login(userInfo.username, userInfo.password)
+  }
+})
+// 登录按钮点击
+document.getElementsByClassName('login-button')[0].onclick = function () {
+	const username = document.getElementById('username').value
+  const password = document.getElementById('password').value
+  if (!username || !password) {
+    chrome.notifications.create(null, {
+      type: 'basic',
+      iconUrl: 'img/48.png',
+      title: '运行方案',
+      message: '用户名或密码不能为空!'
+    })
+    return
+  }
+	login(username, password)
 }

+ 19 - 12
js/popup.js

@@ -21,12 +21,8 @@ const getSchemeData = new Promise((resolve, reject) => {
   })
 })
 
-let userInfo = localStorage.getItem('userInfo')
-if (userInfo) {
-	userInfo = JSON.parse(userInfo)
-  // document.getElementsByClassName('user')[0].innerHTML = `金币:  ${userInfo.gold}`
-  document.getElementsByClassName('login')[0].getElementsByClassName.display = 'none'
-}
+let userInfo = {}
+
 // 获取当前选项卡ID
 function getCurrentTabId(callback) {
 	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
@@ -35,13 +31,23 @@ function getCurrentTabId(callback) {
 }
 
 // 登录按钮登录
-document.getElementsByClassName('login')[0].onclick = function () {
-  window.open(chrome.extension.getURL('options.html'))
+document.getElementsByClassName('userInfo')[0].onclick = function () {
+  if (!userInfo || !userInfo.username || !userInfo.password || !userInfo.session) {
+    window.open(chrome.extension.getURL('options.html'), "_blank")
+  } else {
+    chrome.storage.sync.set({userInfo: {}})
+    location.reload();
+  }
 }
 
 let dataCopy = null
 
-window.onload = function() { 
+chrome.storage.sync.get('userInfo', function(data) {
+  console.log(data)
+  userInfo = data.userInfo
+  if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
+    document.querySelector('.userInfo').innerHTML = `${userInfo.username}`
+  }
   // alert("页面加载完成!"); 
   // 获取是否有脚本
   getSchemeData.then((dataTemp) => {
@@ -58,10 +64,11 @@ window.onload = function() {
       chrome.tabs.create({url: dataTemp.url})
       return
     }
-    if (dataTemp.err !== 0) {
+    const data = dataTemp['data']
+    if (dataTemp.err !== 0 || data.length == 0) {
       scriptBox.classList.add('no-scheme')
+      scriptBox.classList.remove('scheme')
     } else {
-      const data = dataTemp['data']
       dataCopy = data
       let buttonHtml = ''
       let ind = 0
@@ -107,4 +114,4 @@ window.onload = function() {
       }, 0);
     }
   })
-}
+})

+ 2 - 1
manifest.json

@@ -1,7 +1,7 @@
 {
 	"manifest_version": 2,
 	"name": "脚本助手",
-	"version": "1.1.4",
+	"version": "1.1.6",
 	"description": "脚本助手",
 	"icons": {
 		"16": "./img/16.png",
@@ -26,6 +26,7 @@
 		}
 	],
 	"permissions": [
+		"storage",
 		"background",
 		"activeTab",
 		"webRequest",

+ 16 - 5
popup.html

@@ -51,7 +51,7 @@
       }
       .script-box {
         width: 200px;
-        height: 200px;
+        height: 220px;
         overflow: auto;
         position: relative;
       }
@@ -78,14 +78,25 @@
         color: #999;
         font-size: 14px;
       }
+      .userInfo {
+        position: absolute;
+        bottom: 0;
+        right: 0;
+        color: #009fe9;
+        font-size: 14px;
+        line-height: 26px;
+        padding: 0 5px;
+        cursor: pointer;
+        z-index: 999;
+      }
+      .userInfo:hover {
+        color: #9d9e4e;
+      }
     </style>
   </head>
   <body>
-    <div class="user-bar">
-      <span class="user"></span>
-      <div class="login">登录</div>
-    </div>
     <div class="script-box">
+      <div class="userInfo">未登录用户</div>
       <img class="loading" src="./img/loading.svg">
       <div class="no-script">
         <img src="./img/no-script.svg">