Discussions » Development

How does // @match & // @include works in specific situation.

NotYouMod
§
Posted: 2022-02-18
Edited: 2022-02-18

Hi there! I had a question about // @match and // @include, I'll show example for better understanding:

// ==UserScript==
// @name Example User-Script
// @namespace -
// @description blah blah, does something.
// @author NotYou
// @include https://example.com/search/
// @include https://example.com/search
// @license GPL-3.0
// @grant none
// ==/UserScript==

// Some code here

There are some examples of @include, okay, now question:

Should script work if location.search does not equal location.origin + location.pathname + location.search written in script?

§
Posted: 2022-02-18
Edited: 2022-02-18

// @match & // @include has nothing really to do with location.origin + location.pathname + location.search

location.origin + location.pathname + location.search should be inside an if or switch condition.

Include is better than match if you want to use regex to match the website name, and if you are good at regex. Match is kind of a basic version of include.

Your question doesn't really make sense.

NotYouMod
§
Posted: 2022-02-18

// @match & // @include has nothing really to do with location.origin + location.pathname + location.search

location.origin + location.pathname + location.search should be inside an if or switch condition.

Include is better than match if you want to use regex to match the website name, and if you are good at regex. Match is kind of a basic version of include.

Ooooh, I did not know differences before, thank you.

§
Posted: 2022-02-26
Edited: 2022-02-26

@match is more robust and recommended over @include.
@include supports Regular Expression but @match doesn't.
Both are converted to RegEx at the end but implications are different.
There can be unintended results with @include.

@match
The @match metadata imperative is very similar to @include, however it is safer. It sets more strict rules on what the * character means.
For details, see the documentation on Match Patterns for Google Chrome. Chrome implemented @match first, and Greasemonkey has been designed to be compatible.

Examples;

// @include     *://*google.com/*

Above can also catch:

google.com
notgoogle.com
some.other.blahgoogle.com
abc.def.blog.com/google.com

@match can catch with or without subdomains, so no need for extra entry for subdomains

// @match     `*://*.google.com/*

Above will also catch:

google.com
www.google.com
xyz.google.com

Post reply

Sign in to post a reply.