mdn/webextensions-examples

[navigation-stats]: is the code still valid?

elnath78 opened this issue · 3 comments

Seems I have no way to make it work, I have added the relative permissions on manifers:

  "permissions": [
      "*://*.creoweb.com/*",
      "webNavigation",
      "activeTab",
      "tabs"
  ],

I have the filter match my URL with RegEx validating it:

	const filter = {
	  url:
	  [
	    {urlMatches: "^\w*:\/\/\w*\.creoweb\.com\/user\/$"},
	    {urlMatches: "^\w*:\/\/\w*\.creoweb\.com\/billing\/\w*\/data\/$"}
	  ]
  }

And finally the actual code:

function navSample(details) {
  console.log(`onCompleted => navToPostPages: ${details.url}`);
}
browser.webNavigation.onCompleted.addListener(navSample, filter);

If I put a console.log before and after, only the one before is fired, I don't understand what I'm doing wrong or if this method is obsolete.

What are the actual URLs that you are trying to match? \w is not likely what you're looking for. Have you tried [^/]+ instead?

And instead of the first \w*, use https?*.

@Rob--W

What are the actual URLs that you are trying to match? `

Example:
http(s)://www.creoweb.com/user
http(s)://www.creoweb.com/billing/johnholmes/data // the name is variable letter and numbers
http(s)://www.creoweb.com/id/12559846/settings // the number is variable, only numbers

\wis not likely what you're looking for. Have you tried[^/]+` instead?

\w* should mean a string made by any non symbol character from 0 to unlimited characters long

And instead of the first \w*, use https?*.

I used a couple of RegEx validators, and both confirmed my strings to be correct, what is the problem with my strings?

I'll answer once more, but close the issue since this is just a coding question and not specific to existing code samples in the repository.

Your code here:

	    {urlMatches: "^\w*:\/\/\w*\.creoweb\.com\/billing\/\w*\/data\/$"}

is equivalent to:

	    {urlMatches: "^w*://w*.creoweb.com/billing/w*/data/$"}

Note that all backslashes (\) have been swallowed, because they are used in a string literal, and the escapes are not meaningful.

Usually, you would use \w etc. in regular expression literals (/regexhere/).
To make sure that the \ are preserved for use in a regular expression, you could either double the backslashes, or use template literals instead with the String.raw tag (note: it's not necessary to escape the single slashes because it's not a regexp literal):

	    {urlMatches: "^\\w*://\\w*\\.creoweb\\.com/billing/\\w*/data/$"}
// or
	    {urlMatches: String.raw`^\w*://\w*\.creoweb\.com/billing/\w*/data/$`}