у меня есть расширение код выглядит так manifest { "manifestversion": 3, "name": "Trade…
- User
у меня есть расширение код выглядит так
manifest { "manifest_version": 3, "name": "Trade Info Extractor", "version": "1.0", "description": "Extracts trade information from the current page.", "permissions": [ "activeTab", "scripting", "tabs" ], "background": { "service_worker": "background.js" }, "action": { "default_popup": "popup.html", "default_icon": { "128": "icons/icon128.png" } }, "icons": { "128": "icons/icon128.png" } }
background chrome.action.onClicked.addListener((tab) => { chrome.scripting.executeScript({ target: { tabId: tab.id }, function: getPositions }).then((results) => { if (results && results.length > 0 && results[0].result) { const positions = results[0].result; createPositionWindow(positions); } }); });
function getPositions() { const positionElements = document.querySelectorAll('.by-table-cell-group'); const positions = []; positionElements.forEach((element, index) => { if (index !== 0) { // Пропускаем первую позицию positions.push(element.innerText); } }); return positions; }
// Слушаем сообщения от popup.js chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === 'updatePositions') { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, function: getPositions }).then((results) => { if (results && results.length > 0 && results[0].result) { const positions = results[0].result; sendResponse(positions); } else { sendResponse([]); } }); }); return true; // Указываем, что ответ будет отправлен асинхронно } });
function createPositionWindow(positions) { const htmlContent =
<html> <head> <title>Открытые позиции</title> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; } h1 { text-align: center; } ul { list-style-type: none; padding: 0; } li { margin: 10px 0; } </style> </head> <body> <h1>Открытые позиции</h1> <ul id="positionsList"> ${positions.map(pos =>${pos}).join('')} </ul> <script src="positionWindow.js"></script> </body> </html>;const blob = new Blob([htmlContent], { type: 'text/html;charset=utf-8' }); const url = URL.createObjectURL(blob);
chrome.windows.create({ url: url, type: 'popup', width: 400, height: 600 }); }
popup.html
popup.js document.addEventListener('DOMContentLoaded', function() { const tradeInfoDiv = document.getElementById('trade-info');
function updateTradeInfo() { chrome.runtime.sendMessage({ action: 'updatePositions' }, function(response) { if (!response || response.length === 0) { tradeInfoDiv.innerHTML = 'Информация о сделках не найдена.'; console.error('Ответ от background.js не содержит информации о сделках:', response); return; }
// Очищаем текущие данные tradeInfoDiv.innerHTML = ''; response.forEach(tradeText => { const tradeDiv = document.createElement('div'); tradeDiv.className = 'trade'; tradeDiv.innerText = decodeWindows1251(tradeText); tradeInfoDiv.appendChild(tradeDiv); }); }); - Assistant
- User
напиши полный код
- Assistant
- User
кнопка не появилась в созданном окно
- Assistant
- User
разве я не должен поменять popup html что бы там появилась кнопка
- Assistant
- User
напиши полный скрипт учитывая что мне нужно было
- Assistant