| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
- var bg = chrome.extension.getBackgroundPage();
- // bg.count = bg.count+1;
- // console.log(bg)
- const serverUrl = 'https://hanshu.run/assist'
- function owoReplaceAll(str, s1, s2) {
- while (str.indexOf(s1) >= 0) {
- str = str.replace(s1, s2)
- }
- return str
- }
- const getSchemeData = new Promise((resolve, reject) => {
- getCurrentTabId((tabInfo) => {
- 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);
-
- })
- })
- let userInfo = {}
- // 获取当前选项卡ID
- function getCurrentTabId(callback) {
- chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
- if(callback) callback(tabs.length ? tabs[0]: null);
- })
- }
- // 登录按钮登录
- document.getElementsByClassName('userInfo')[0].onclick = function () {
- if (!userInfo || !userInfo.username || !userInfo.password || !userInfo.session) {
- // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- window.open(chrome.extension.getURL('options.html'), "_blank")
- } else {
- chrome.storage.sync.set({userInfo: {}})
- chrome.runtime.sendMessage({name:"reloadUser"},function() {})
- location.reload();
- }
- }
- document.getElementsByClassName('clearTemp')[0].onclick = function () {
- chrome.runtime.sendMessage({name:"clear"},function() {
- alert('缓存清除成功!')
- })
- }
- let dataCopy = null
- function load() {
- chrome.storage.sync.get('userInfo', function(data) {
- // alert(data)
- userInfo = data.userInfo
- if (!userInfo) {
- userInfo = {}
- }
- if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
- document.querySelector('.userInfo').innerHTML = `${userInfo.username}`
- }
- // alert("页面加载完成!");
- // 获取是否有脚本
- getSchemeData.then((dataTemp) => {
- // console.log(dataTemp)
- const scriptBox = document.getElementsByClassName('script-box')[0]
- if (dataTemp.edition > 5) {
- chrome.notifications.create(null, {
- type: 'basic',
- iconUrl: 'img/48.png',
- title: '版本过低',
- message: '有新版本请在弹出页面下载最新插件!'
- })
- // console.log(unescape(dataTemp.data))
- chrome.tabs.create({url: dataTemp.url})
- return
- }
- const data = dataTemp['data']
- if (dataTemp.err !== 0 || data.length == 0) {
- scriptBox.classList.add('no-scheme')
- scriptBox.classList.remove('scheme')
- } else {
- dataCopy = data
- let buttonHtml = ''
- let ind = 0
- data.forEach(element => {
- buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
- ind++
- });
- document.querySelector('.button-box').innerHTML = buttonHtml
- scriptBox.classList.add('scheme')
- setTimeout(() => {
- const buttonList = document.getElementsByTagName('button')
- for (const key in buttonList) {
- if (Object.hasOwnProperty.call(buttonList, key)) {
- const element = buttonList[key];
- element.onclick = function () {
-
- let index = this.getAttribute("data-ind")
- index = parseInt(index)
- let dataTempCopy = dataCopy[index]
- switch (dataTempCopy.type) {
- case 'run': {
- chrome.notifications.create(null, {
- type: 'basic',
- iconUrl: 'img/48.png',
- title: dataTempCopy.name,
- message: '远程方案已载入并运行!'
- })
- // console.log(unescape(dataTemp.data))
- function decodeOwo (value) {
- value = owoReplaceAll(value, "'", 'owovar1')
- value = owoReplaceAll(value, "`", 'owovar2')
- value = owoReplaceAll(value, '"', 'owovar3')
- value = owoReplaceAll(value, '$', 'owovar4')
- return value
- }
- if (dataTempCopy.data) {
- let execTwmp = `
- function owoReplaceAll(str, s1, s2) {
- while (str.indexOf(s1) >= 0) {
- str = str.replace(s1, s2)
- }
- return str
- }
- function clearOwo (value) {
- value = owoReplaceAll(value, 'owovar1', "'")
- value = owoReplaceAll(value, 'owovar2', "\`")
- value = owoReplaceAll(value, 'owovar3', '"')
- value = owoReplaceAll(value, 'owovar4', '$')
- return value
- }
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.charset = "UTF-8";
- script.innerHTML = clearOwo(\`${decodeOwo(dataTempCopy.data)}\`);
- document.body.appendChild(script)
- `
- chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: execTwmp})
- }
- if (dataTempCopy.style || dataTempCopy.script) {
- dataTempCopy.style = dataTempCopy.style || '[]'
- dataTempCopy.script = dataTempCopy.script || '[]'
- let temp = `
- function loadScript(url, callback) {
- var script = document.createElement("script")
- script.type = "text/javascript";
- if (script.readyState) { //IE
- script.onreadystatechange = function () {
- if (script.readyState == "loaded" || script.readyState == "complete") {
- script.onreadystatechange = null;
- if (callback) callback();
- }
- };
- } else { //Others
- script.onload = function () {
- if (callback) callback();
- };
- }
- script.src = url;
- var head = document.head || document.getElementsByTagName('head')[0];
- head.appendChild(script);
- }
-
- function loadCSS (url) {
- var link = document.createElement("link");
- link.rel = "stylesheet";
- link.type = "text/css";
-
- link.href = url;
-
- document.getElementsByTagName("head")[0].appendChild(link);
- }
- ${dataTempCopy.style}.forEach(element => {
- loadCSS(element)
- });
- ${dataTempCopy.script}.forEach(element => {
- loadScript(element)
- });
- `
- chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(temp)})
- }
- break
- }
- }
- }
- }
- }
- }, 0);
- }
- })
- })
- }
- load()
- // document.getElementsByClassName('.no-script')[0].addEventListener("click", function(){
- // load()
- // })
|