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

v1.1.0增加账号功能,优化网络请求

PUGE 5 éve
szülő
commit
b1c66c4775
7 módosított fájl, 179 hozzáadás és 47 törlés
  1. 72 0
      background.js
  2. 4 27
      insert.js
  3. 0 1
      js/background.js
  4. 41 15
      js/options.js
  5. 4 0
      js/popup.js
  6. 8 3
      manifest.json
  7. 50 1
      options.html

+ 72 - 0
background.js

@@ -0,0 +1,72 @@
+var userInfo = null
+var username = localStorage.getItem('owoUsername')
+var password = localStorage.getItem('owoPassword')
+var tempData = null
+var openList = {}
+
+function saveUser (username, password) {
+  localStorage.setItem('owoUsername', username)
+  localStorage.setItem('owoPassword', password)
+}
+
+
+if (username && password) {
+  fetch(`http://going.run/userServer?route=login`, {
+    method: 'POST',
+    headers: {
+      "Content-Type": "application/json"
+    },
+    body: JSON.stringify({
+      type: "assist",
+      username: username,
+      password: password
+    })
+  }).then((response) => {return response.json();}).then((res) => {
+    if (res.err === 0) {
+      userInfo = res.data
+    }
+  })
+}
+
+// 对数据进行处理
+function clearData (data) {
+  let returnData = []
+  data.forEach(element => {
+    if (openList[element.id]) {
+      returnData.push(element)
+    }
+  })
+  return returnData
+}
+
+// 监听消息
+chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
+  switch (message.name) {
+    case 'getData':
+      const nowTime = Date.parse(new Date())
+      if (tempData && (tempData.time + 1 * 60 * 1000) > nowTime) {
+        console.log('使用缓存返回!', tempData)
+        sendResponse(clearData(tempData.data))
+      } else {
+        const serverUrl = 'https://going.run/assistAll'
+        fetch(`${serverUrl}?route=search`, {
+          method: 'POST',
+          body: JSON.stringify({
+            "edition": 2,
+            "url": message.url
+          }),
+          redirect: 'follow'
+        }).then(data => data.json()).then(dataTemp => {
+          
+          tempData = {
+            time: nowTime,
+            data: dataTemp
+          }
+          sendResponse(clearData(dataTemp))
+        })
+      }
+      break;
+    default:
+      break;
+  }
+})

+ 4 - 27
insert.js

@@ -1,10 +1,10 @@
 const serverUrl = 'https://going.run/assistAll'
-
 // let tempData = localStorage.getItem('tempData')
 // if (tempData) {
 //   tempData = JSON.parse(tempData)
 // }
 
+
 function addScr (data) {
   var script = document.createElement("script");
   script.type = "text/javascript";
@@ -13,33 +13,10 @@ function addScr (data) {
   document.body.appendChild(script)
 }
 
-const getSchemeData = new Promise((resolve, reject) => {
-  const nowTime = Date.parse(new Date())
-  // if (tempData && (tempData.time + 10 * 60 * 1000) > nowTime) {
-  //   console.log('使用缓存返回!', tempData)
-  //   resolve(tempData.data)
-  // }
-  const urlStr = window.location.href
-  fetch(`${serverUrl}?route=search`, {
-    method: 'POST',
-    body: JSON.stringify({
-      "edition": 2,
-      "url": urlStr
-    }),
-    redirect: 'follow'
-  }).then(data => data.json()).then(dataTemp => {
-    resolve(dataTemp)
-  })
-})
-
-
-
-getSchemeData.then((dataTemp) => {
+chrome.runtime.sendMessage({name:"getData", url: window.location.href},function(dataTemp){
+  console.log(dataTemp)
+  if (!dataTemp) return
   let urlStr = window.location.href
-  localStorage.setItem('tempData', JSON.stringify({
-    time: Date.parse(new Date()),
-    data: dataTemp
-  }))
   dataTemp.forEach(element => {
     if (new RegExp(element.url).test(urlStr)) {
       addScr(element.data)

+ 0 - 1
js/background.js

@@ -1 +0,0 @@
-

+ 41 - 15
js/options.js

@@ -1,3 +1,5 @@
+//在popup.js 中调用 backgourd.js 中的变量和方法,很重要
+var bg = chrome.extension.getBackgroundPage();
 const serverUrl = 'https://going.run/assist'
 // 登录按钮点击
 document.getElementsByClassName('login-button')[0].onclick = function () {
@@ -12,20 +14,44 @@ document.getElementsByClassName('login-button')[0].onclick = function () {
     })
     return
   }
-	fetch(`${serverUrl}/login?username=${username}&password=${password}`).then(data => data.json()).then(data => {
-		if (data.err === 0) {
-      localStorage.setItem('userInfo', JSON.stringify({
-        gold: data.data.gold,
-        userName: username,
-        password: password
-      }))
-    } else {
-      chrome.notifications.create(null, {
-        type: 'basic',
-        iconUrl: 'img/48.png',
-        title: '运行方案',
-        message: data.message
-      })
+	fetch(`http://going.run/userServer?route=login`, {
+    method: 'POST',
+    headers: {
+      "Content-Type": "application/json"
+    },
+    body: JSON.stringify({
+      type: "assist",
+      username: username,
+      password: password
+    })
+  }).then((response) => {return response.json();}).then((res) => {
+    if (res.err === 0) {
+      bg.userInfo = res.data
+      bg.saveUser(username, password)
+      // do something
     }
-	})
+  })
+}
+
+if (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]
+        }
+      });
+    }, 0);
+  })
+  
 }

+ 4 - 0
js/popup.js

@@ -1,3 +1,7 @@
+//在popup.js 中调用 backgourd.js 中的变量和方法,很重要
+var bg = chrome.extension.getBackgroundPage();
+// bg.count = bg.count+1;
+console.log(bg)
 const serverUrl = 'https://going.run/assist'
 
 const getSchemeData = new Promise((resolve, reject) => {

+ 8 - 3
manifest.json

@@ -1,7 +1,7 @@
 {
 	"manifest_version": 2,
 	"name": "脚本助手",
-	"version": "1.0.9",
+	"version": "1.1.0",
 	"description": "脚本助手",
 	"icons": {
 		"16": "./img/16.png",
@@ -13,15 +13,20 @@
 		"default_title": "脚本助手",
 		"default_popup": "popup.html"
 	},
+	"background": {
+		"persistent": false,
+		"scripts": ["background.js"]
+	},
 	"content_scripts": [
 		{
-      "matches": ["<all_urls>"],
+      		"matches": ["<all_urls>"],
 			"js": ["insert.js"],
 			"all_frames": true,
 			"run_at": "document_end"
-    }
+    	}
 	],
 	"permissions": [
+		"background",
 		"activeTab",
 		"notifications",
 		"https://going.run/*"

+ 50 - 1
options.html

@@ -33,6 +33,52 @@
         display: block;
         width: 100%;
       }
+      table {
+        border-collapse: collapse;
+        border-spacing: 0;
+        width: 100%;
+      }
+      
+      td,th {
+        padding: 0;
+      }
+      table {
+        border-collapse: collapse;
+        border-spacing: 0;
+        empty-cells: show;
+        border: 1px solid #cbcbcb;
+      }
+      
+      table caption {
+        color: #000;
+        font: italic 85%/1 arial,sans-serif;
+        padding: 1em 0;
+        text-align: center;
+      }
+      
+      table td,table th {
+        border-left: 1px solid #cbcbcb;
+        border-width: 0 0 0 1px;
+        font-size: inherit;
+        margin: 0;
+        overflow: visible;
+        padding: .5em 1em;
+      }
+      
+      table thead {
+        background-color: #e0e0e0;
+        color: #000;
+        text-align: left;
+        vertical-align: bottom;
+      }
+      
+      table td {
+        background-color: transparent;
+      }
+      td input[type="checkbox"] {
+        display: block;
+        margin: 0 auto;
+      }
     </style>
   </head>
   <body>
@@ -41,7 +87,10 @@
       <input id="username" type="text">
       <div class="label">密码:</div>
       <input id="password" type="password">
-      <button class="login-button">登录</button>
+      <button class="login-button" onclick="login()">登录</button>
+    </div>
+    <div id="selectTable" style="display: none;">
+
     </div>
     <script type="text/javascript" src="js/options.js"></script>
   </body>