| 12345678910111213141516171819202122232425262728293031323334353637 |
- // Background script for the extension
- // Function to navigate a tab to a new URL
- function navigateTab(tabId, url) {
- return browser.tabs.update(tabId, { url: url });
- }
- // Listen for messages from popup or content scripts
- browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
- if (message.command === 'navigate') {
- if (message.tabId && message.url) {
- navigateTab(message.tabId, message.url)
- .then(() => sendResponse({ status: 'success' }))
- .catch(error => sendResponse({ status: 'error', message: error.message }));
- return true; // Required for async sendResponse
- }
- }
- });
- // Listen for tab updates to potentially inject scripts on certain URLs
- browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
- if (changeInfo.status === 'complete' && tab.url) {
- // Check if the URL matches your patterns and inject scripts as needed
- const urlPatterns = [
- /example\.com/,
- /another-site\.org/
- // Add more URL patterns as needed
- ];
-
- const shouldInject = urlPatterns.some(pattern => pattern.test(tab.url));
-
- if (shouldInject) {
- browser.tabs.executeScript(tabId, { file: '/content_scripts/content.js' })
- .catch(error => console.error('Error injecting script:', error));
- }
- }
- });
|