0

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!

  • `chrome.browserAction.addEventListener` does not exist. Please use [chrome.browserAction.onClicked.addListener](https://developer.chrome.com/docs/extensions/reference/browserAction/#event). – Norio Yamamoto Feb 25 '23 at 22:45
  • 1) Also add `"browser_action": {},` to manifest.json. 2) Remove chrome.tabs.query and window.location.href 3) Use `tab` parameter of [onClicked listener](https://developer.chrome.com/docs/extensions/reference/browserAction/#event-onClicked). – wOxxOm Feb 25 '23 at 23:07
  • I have changed as you both said and now I don't have error but it still doesn't work – לידור תדלה Feb 26 '23 at 11:13

0 Answers0