//在popup.js 中调用 backgourd.js 中的变量和方法,很重要 var bg = chrome.extension.getBackgroundPage(); // bg.count = bg.count+1; // console.log(bg) const serverUrl = 'https://assist.lamp.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(() => { const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); fetch(`${serverUrl}?route=search`, { method: 'POST', headers: myHeaders, body: JSON.stringify({ "edition": 8, "url": tabInfo.url }), redirect: 'follow' }).then(data => data.json()).then(tabData => { tabData.tabInfo = tabInfo resolve(tabData) }) }, 100); }) }) // 获取当前选项卡ID function getCurrentTabId(callback) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { if(callback) callback(tabs.length ? tabs[0]: null); }) } let dataCopy = null function load() { // 获取是否有脚本 getSchemeData.then((tabData) => { // console.log(tabData) const scriptBox = document.getElementsByClassName('script-box')[0] const data = tabData['data'] if (tabData.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 += `` 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': { // 判断是否在js主世界执行 if (dataTempCopy.id == '378') { chrome.scripting.executeScript({ world: 'MAIN', // 在页面主世界中执行 target: { tabId: tabData.tabInfo.id }, files: ['./scripts/378.js'] }); } else { chrome.scripting.executeScript({ target: { tabId: tabData.tabInfo.id }, files: ['insert-popup.js'] }); } chrome.notifications.create(null, { type: 'basic', iconUrl: 'img/48.png', title: dataTempCopy.name, message: '远程方案已载入并运行!' }) break } } } } } }, 0); } }) } load(); document.getElementsByClassName('lxkf')[0].addEventListener("click", function(){ console.log('点击客服标签') window.open('https://work.weixin.qq.com/kfid/kfc7a6930ede9575277') })