Browse Source

Initial commit

PUGE 6 năm trước cách đây
commit
9b0f5f71e6
12 tập tin đã thay đổi với 258 bổ sung0 xóa
  1. 2 0
      .gitattributes
  2. 13 0
      background.html
  3. 9 0
      devtools.html
  4. BIN
      img/128.png
  5. BIN
      img/16.png
  6. BIN
      img/48.png
  7. 2 0
      js/background.js
  8. 31 0
      js/options.js
  9. 77 0
      js/popup.js
  10. 30 0
      manifest.json
  11. 48 0
      options.html
  12. 46 0
      popup.html

+ 2 - 0
.gitattributes

@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto

+ 13 - 0
background.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="zh-CN">
+  <head>
+    <title>test</title>
+  </head>
+  <body>
+    1
+    <script>
+      console.log('sdsd')
+      chrome.tabs.executeScript(tabId, {code: 'document.body.style.backgroundColor="red"'});
+    </script>
+  </body>
+</html>

+ 9 - 0
devtools.html

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html lang="zh-CN">
+  <head>
+    <title>test</title>
+  </head>
+  <body>
+    4
+  </body>
+</html>

BIN
img/128.png


BIN
img/16.png


BIN
img/48.png


+ 2 - 0
js/background.js

@@ -0,0 +1,2 @@
+
+

+ 31 - 0
js/options.js

@@ -0,0 +1,31 @@
+const serverUrl = 'http://127.0.0.1:8005'
+// 登录按钮点击
+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(`${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
+      })
+    }
+	})
+}

+ 77 - 0
js/popup.js

@@ -0,0 +1,77 @@
+const buttonItem = document.getElementsByTagName('button')[0]
+const serverUrl = 'http://127.0.0.1:8005'
+
+const getSchemeData = new Promise((resolve, reject) => {
+  getCurrentTabId((tabInfo) => {
+    fetch(`${serverUrl}/?edition=1&url=${tabInfo.url}`).then(data => data.json()).then(dataTemp => {
+      dataTemp.tabInfo = tabInfo
+      resolve(dataTemp)
+    })
+  })
+})
+
+buttonItem.onclick = function () {
+  getSchemeData.then((dataTemp) => {
+    if (dataTemp.err === 0) {
+      // console.log(dataTemp.type)
+      switch (dataTemp.type) {
+        case 'run': {
+          chrome.notifications.create(null, {
+            type: 'basic',
+            iconUrl: 'img/48.png',
+            title: '运行方案',
+            message: '远程方案以载入并运行!'
+          })
+          // console.log(unescape(dataTemp.data))
+          chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(dataTemp.data)})
+          break
+        }
+      }
+    } else if (dataTemp.err === 999) {
+      chrome.notifications.create(null, {
+        type: 'basic',
+        iconUrl: 'img/48.png',
+        title: '版本过低',
+        message: '有新版本请在弹出页面下载最新插件!'
+      })
+      // console.log(unescape(dataTemp.data))
+      chrome.tabs.create({url: dataTemp.url})
+    } else {
+      chrome.notifications.create(null, {
+        type: 'basic',
+        iconUrl: 'img/48.png',
+        title: '插件提示',
+        message: unescape(dataTemp.message)
+      })
+    }
+  })
+}
+
+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'
+}
+// 获取当前选项卡ID
+function getCurrentTabId(callback) {
+	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
+		if(callback) callback(tabs.length ? tabs[0]: null);
+	})
+}
+
+// 登录按钮登录
+document.getElementsByClassName('login')[0].onclick = function () {
+  window.open(chrome.extension.getURL('options.html'))
+}
+
+window.onload = function() { 
+  // alert("页面加载完成!"); 
+  // 获取是否有脚本
+  getSchemeData.then((dataTemp) => {
+    if (dataTemp.err !== 0) {
+      buttonItem.classList.add('no-scheme')
+      buttonItem.innerText = '此页面没有脚本'
+    }
+  })
+}

+ 30 - 0
manifest.json

@@ -0,0 +1,30 @@
+{
+	"manifest_version": 2,
+	"name": "脚本助手",
+	"version": "1.0.0",
+	"description": "测试",
+	"icons": {
+		"16": "./img/16.png",
+		"48": "./img/48.png",
+		"128": "./img/128.png"
+	},
+	"browser_action":  {
+		"default_icon": "img/48.png",
+		"default_title": "这是一个示例Chrome插件",
+		"default_popup": "popup.html"
+	},
+	"content_scripts": [],
+	"permissions": [
+		"activeTab",
+		"notifications",
+		"webRequest",
+		"http://127.0.0.1/*"
+	],
+	"homepage_url": "https://github.com/PUGE",
+	"options_page": "options.html",
+	"options_ui":
+	{
+		"page": "options.html",
+		"chrome_style": true
+	}
+}

+ 48 - 0
options.html

@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<html lang="zh-CN">
+  <head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
+    <meta name="apple-mobile-web-app-title" content="CodeDay">
+    <meta http-equiv="Cache-Control" content="no-siteapp">
+    <title>配置</title>
+    <style>
+      .label {
+        line-height: 30px;
+        font-size: 16px;
+        color: #666;
+      }
+      input {
+        width: 100%;
+        height: 30px;
+      }
+      .login-button {
+        height: 40px;
+        background-color: #009fe9;
+        background-image: none;
+        color: white;
+        font-weight: normal;
+        margin: 0;
+        padding: 0;
+        border: none;
+        margin: 10px 0;
+        font-size: 16px;
+        box-shadow: none;
+        cursor: pointer;
+        display: block;
+        width: 100%;
+      }
+    </style>
+  </head>
+  <body>
+    <div class="login-box">
+      <div class="label">用户名:</div>
+      <input id="username" type="text">
+      <div class="label">密码:</div>
+      <input id="password" type="password">
+      <button class="login-button">登录</button>
+    </div>
+    <script type="text/javascript" src="js/options.js"></script>
+  </body>
+</html>

+ 46 - 0
popup.html

@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html lang="zh-CN">
+  <head>
+    <title>test</title>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
+    <meta name="apple-mobile-web-app-title" content="CodeDay">
+    <meta http-equiv="Cache-Control" content="no-siteapp">
+    <style>
+      button {
+        width: 180px;
+        border: none;
+        height: 30px;
+        background-color: #009fe9;
+        color: white;
+        cursor: pointer;
+        user-select: none;
+        box-shadow: 1px 1px 4px #968a8a;
+      }
+      .user-bar {
+        font-family: "Microsoft YaHei";
+        font-size: 12px;
+        line-height: 26px;
+        color: #333;
+      }
+      .no-scheme {
+        background-color: #ccc;
+        box-shadow: none;
+        pointer-events: none;
+      }
+      .login {
+        display: none;
+      }
+    </style>
+  </head>
+  <body>
+    <div class="user-bar">
+      <span class="user"></span>
+      <div class="login">登录</div>
+    </div>
+    <button>获取方案</button>
+    <script type="text/javascript" src="js/popup.js"></script>
+  </body>
+  
+</html>