Explorar o código

增加永久缓存功能

PUGE %!s(int64=3) %!d(string=hai) anos
pai
achega
9f0b54f4a3
Modificáronse 7 ficheiros con 68 adicións e 41 borrados
  1. 4 3
      back/background.js
  2. 46 21
      back/insert.js
  3. 17 16
      back/popup.js
  4. 0 0
      background.js
  5. 0 0
      insert.js
  6. 0 0
      js/popup.js
  7. 1 1
      manifest.json

+ 4 - 3
back/background.js

@@ -47,8 +47,9 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
   switch (message.name) {
     case 'getData':
       const nowTime = Date.parse(new Date())
-      if (tempData && (tempData.time + 1 * 0 * 1000) > nowTime) {
-        console.log('使用缓存返回!', tempData)
+      // 缓存
+      if (tempData && (tempData.time + 60 * 60 * 1000) > nowTime) {
+        // console.log('使用缓存返回!', tempData)
         sendResponse(clearData(tempData.data))
       } else {
         const serverUrl = 'https://going.run/assistAll'
@@ -102,7 +103,7 @@ function lanjie (blockList) {
       
       for (const key in blockList) {
         if (details.url.includes(key) || new RegExp(key).test(details.url)) {
-          console.log(details.url)
+          // console.log(details.url)
           return {
             redirectUrl: blockList[key]
           }

+ 46 - 21
back/insert.js

@@ -42,28 +42,53 @@ function loadCSS (url) {
   cssObj.href = url;
   headObj.appendChild(cssObj);
 }
-chrome.runtime.sendMessage({name:"getData", url: window.location.href},function(dataTemp){
-  // console.log(dataTemp)
-  if (!dataTemp) return
+
+// 判断是否有永久的加载数据
+
+if (localStorage.getItem('owoLoadData')) {
+  const element = JSON.parse(localStorage.getItem('owoLoadData'))
   let urlStr = window.location.href
-  dataTemp.forEach(element => {
-    if (new RegExp(element.url).test(urlStr)) {
+  if (new RegExp(element.url).test(urlStr)) {
       
-      if (element.script) {
-        JSON.parse(element.script).forEach(element => {
-          console.log(`load: ${element}`)
-          loadJS(element)
-        });
-      }
-      if (element.style) {
-        JSON.parse(element.style).forEach(element => {
-          loadCSS(element)
-        });
-      }
-      setTimeout(() => {
-        addScr(element.data)
-      }, 0);
-      return
+    if (element.script) {
+      element.script.forEach(element => {
+        loadJS(element)
+      });
+    }
+    if (element.style) {
+      element.style.forEach(element => {
+        loadCSS(element)
+      });
     }
+    setTimeout(() => {
+      if (element.data) addScr(element.data)
+    }, 0);
+  }
+} else {
+  // 尝试网络加载
+  chrome.runtime.sendMessage({name:"getData", url: window.location.href},function(dataTemp){
+    // console.log(dataTemp)
+    if (!dataTemp) return
+    let urlStr = window.location.href
+    dataTemp.forEach(element => {
+      if (new RegExp(element.url).test(urlStr)) {
+        
+        if (element.script) {
+          JSON.parse(element.script).forEach(element => {
+            loadJS(element)
+          });
+        }
+        if (element.style) {
+          JSON.parse(element.style).forEach(element => {
+            loadCSS(element)
+          });
+        }
+        setTimeout(() => {
+          if (element.data) addScr(element.data)
+        }, 0);
+        return
+      }
+    })
   })
-})
+}
+

+ 17 - 16
back/popup.js

@@ -1,7 +1,7 @@
 //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
 var bg = chrome.extension.getBackgroundPage();
 // bg.count = bg.count+1;
-console.log(bg)
+// console.log(bg)
 const serverUrl = 'https://going.run/assist'
 
 function owoReplaceAll(str, s1, s2) {
@@ -13,18 +13,20 @@ function owoReplaceAll(str, s1, s2) {
 
 const getSchemeData = new Promise((resolve, reject) => {
   getCurrentTabId((tabInfo) => {
-    // alert(userInfo.username)
-    fetch(`${serverUrl}?route=search&username=${userInfo.username || 'nologin'}`, {
-      method: 'POST',
-      body: JSON.stringify({
-        "edition": 5,
-        "url": tabInfo.url
-      }),
-      redirect: 'follow'
-    }).then(data => data.json()).then(dataTemp => {
-      dataTemp.tabInfo = tabInfo
-      resolve(dataTemp)
-    })
+    setTimeout(() => {
+        fetch(`${serverUrl}?route=search&username=${userInfo.username || 'nologin'}`, {
+            method: 'POST',
+            body: JSON.stringify({
+              "edition": 5,
+              "url": tabInfo.url
+            }),
+            redirect: 'follow'
+          }).then(data => data.json()).then(dataTemp => {
+            dataTemp.tabInfo = tabInfo
+            resolve(dataTemp)
+          })
+    }, 100);
+    
   })
 })
 
@@ -57,7 +59,7 @@ let dataCopy = null
 
 function load() {
   chrome.storage.sync.get('userInfo', function(data) {
-    console.log(data)
+    // alert(data)
     userInfo = data.userInfo
     if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
       document.querySelector('.userInfo').innerHTML = `${userInfo.username}`
@@ -65,7 +67,7 @@ function load() {
     // alert("页面加载完成!"); 
     // 获取是否有脚本
     getSchemeData.then((dataTemp) => {
-      console.log(dataTemp)
+    //   console.log(dataTemp)
       const scriptBox = document.getElementsByClassName('script-box')[0]
       if (dataTemp.edition > 5) {
         chrome.notifications.create(null, {
@@ -137,7 +139,6 @@ function load() {
                       script.type = "text/javascript";
                       script.charset = "UTF-8";
                       script.innerHTML = clearOwo(\`${decodeOwo(dataTempCopy.data)}\`);
-                      console.log(script.innerHTML)
                       document.body.appendChild(script)
                       `
                       chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: execTwmp})

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
background.js


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
insert.js


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
js/popup.js


+ 1 - 1
manifest.json

@@ -1,7 +1,7 @@
 {
 	"manifest_version": 2,
 	"name": "脚本助手",
-	"version": "1.1.15",
+	"version": "1.1.17",
 	"description": "脚本助手",
 	"icons": {
 		"16": "./img/16.png",

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio