I try to copy and paste all the file you provided and did some editing, so the extension is loaded.
As shown in the following figure, the console.log
are successfully processed, tho it doesn't return the actual replacedText
because you don't provide the file.
// manifest.json
{
"name": "SearchNow",
"description": "Search ServiceNow",
"manifest_version": 3,
"version": "0.9",
"background": {
"service_worker": "background-simplified.js",
"type": "module"
},
// "icons": {
// "16": "icons/SNow16.png",
// "24": "icons/SNow24.png",
// "32": "icons/SNow32.png",
// "48": "icons/SNow48.png",
// "128": "icons/SNow128.png"
// },
// "action": {
// "default_icon": {
// "16": "icons/SNow16.png",
// "24": "icons/SNow24.png",
// "32": "icons/SNow32.png"
// },
// "default_title": "Click for advanced search options",
// "default_popup": "popup.html"
// },
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*",
"file:///*/*"
],
// "css": [
// "styles.css"
// ],
"js": [
"content-script-hyperlinker.js"
],
"run_at": "document_end"
}
],
// "options_ui": {
// "page": "options.html",
// "open_in_tab": true
// },
"permissions": [
"activeTab",
"alarms",
"tabs",
"scripting",
"storage",
"contextMenus"
],
"host_permissions": [
"<all_urls>"
],
"commands": {
"autoSearch": {
"suggested_key": {
"default": "Ctrl+Shift+1"
},
"description": "AutoSearch selected text"
},
"autoNav": {
"suggested_key": {
"default": "Ctrl+Shift+2"
},
"description": "Automatically navigate to selected record"
}
}
}
// background-simplified.js
// import { autoNav } from './handlers/autoNav.js';
// import { autoSearch } from './handlers/autoSearch.js';
// import { constructUrl } from './utils/urlConstructor.js';
// import * as eventListeners from './eventListeners.js';
// import * as regexPatterns from './utils/regexPatterns.js';
/* jshint esversion: 6*/
// Initialize event listeners
// eventListeners.setupEventListeners();
// Create context menu entries
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'autoNav',
title: 'Open "%s"',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'autoSearch',
title: 'Search ServiceNow for "%s"',
contexts: ['selection']
});
});
// Handle message passing with content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'getRegexPatterns') {
sendResponse({ regexPatterns: '' });
} else if (request.action === 'constructUrl') {
// const url = constructUrl(request.base, request.path, request.query);
sendResponse({ url: '' });
}
});
// Handle context menu item clicks
chrome.contextMenus.onClicked.addListener((info, tab) => {
// if (info.menuItemId === 'autoNav') {
// autoNav(info, tab);
// } else if (info.menuItemId === 'autoSearch') {
// autoSearch(info, tab);
// }
console.log('contextMenus')
});
// content-script-hyperlinker.js
console.log('Content script loaded.');
runner();
function hyperlinkMatches(node, regexPatterns) {
if (node.nodeType === Node.TEXT_NODE) {
let text = node.nodeValue;
let replacedText = text;
console.log('Original text:', text);
for (const [patternName, regex] of Object.entries(regexPatterns)) {
replacedText = replacedText.replace(new RegExp(regex), (match) => {
chrome.runtime.sendMessage(
{
action: 'constructUrl',
base: 'https://gsa.servicenowservices.com',
path: '/nav_to.do',
query: match
},
(response) => {
const url = response.url;
console.log(`Match found: ${match}, URL: ${url}`);
return `<a href="${url}" target="_blank">${match}</a>`;
}
);
});
}
console.log('Replaced text:', replacedText);
if (replacedText !== text) {
console.log('Replaced text:', replacedText);
const span = document.createElement('span');
span.innerHTML = replacedText;
node.parentNode.replaceChild(span, node);
}
} else if (node.nodeType === Node.ELEMENT_NODE && node.nodeName !== 'SCRIPT' && node.nodeName !== 'STYLE') {
for (let child = node.firstChild; child; child = child.nextSibling) {
hyperlinkMatches(child, regexPatterns);
}
}
}
function runner() {
console.log('Document loaded, starting hyperlinking process.');
chrome.runtime.sendMessage({ action: 'getRegexPatterns' }, (response) => {
const regexPatterns = response.regexPatterns;
hyperlinkMatches(document.body, regexPatterns);
});
}