I am trying to learn how to create chrome extentions.
The one I'm working on right now is suppose to work as follows: 1.user click on the extention button. 2. the current url is being checked to see if it include a number 3. if it does, the user will be redirected to the next url.
example - https://the-gamer-manga.com/manga/the-gamer-chapter-344/ would be changed to - https://the-gamer-manga.com/manga/the-gamer-chapter-345/
I'll add the files I created - manifest.json:
{
"name": "URL Analyzer",
"version": "1.0",
"description": "A Chrome extension that analyzes and modifies URLs.",
"manifest_version": 2,
"background" : {
"scripts" : [
"popup.js"
]
},
"browser_action": {},
"permissions": [
"tabs", "<all_urls>","activeTab"
]
}
popup.js:
function analyzeUrl() {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let currentUrl = tabs[0].url;
console.log('Analyzing URL:', currentUrl);
var matches = currentUrl.match(/\d+/);
if (matches) {
var number = parseInt(matches[0]);
var newNumber = number + 1;
var newUrl = currentUrl.replace(/\d+/, newNumber.toString());
console.log('Modified URL:', newUrl);
chrome.tabs.update({url: newUrl});
};
});
};
console.log('starting....');
chrome.browserAction.onClicked.addListener(analyzeUrl);
console.log('end....');
I'm trying to make it as simple as I can so I don't need a real popup page, just the action to be done when the extention is being clicked on.
Currently, I am getting this error :
Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener')
would love to hear more idea on how to solve this.
update: Changed the manifest with relevant changes but still not working, also I added prints before and after onclick but they just being printed the moment I start the extension
Last edit: I found my problem, removed the 'click' and now it works!