Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a Greasemonkey script across multiple domains

This is a bit of an oddly specific question.

I'm writing a Greasemonkey script that will run across ten domains. The websites all have identical structures, but the domain name for each one is different. For example, the script will run on:

http://first-domain.com/
http://another-one.com/
http://you-get-the-point.com/

I also need it to run on other pages across the same domains, so the list for just one of these domains would be something like:

http://first-domain.com/admin/edit/*
http://first-domain.com/blog/*
http://first-domain.com/user/*/history

Obviously if I'm including these three paths for all ten domains, that's 30 URLs I need to list as @includes.

So I'm wondering if there's a way to do something like:

// Obviously fake code:

var list_of_sites = ["first-domain", "another-one", "you-get-the-point"];

@include http:// + list_of_sites[any] + .com/admin/edit/*
@include http:// + list_of_sites[any] + .com/blog/*
@include http:// + list_of_sites[any] + .com/user/*/history

If something like this possible, it would cut the list of @includes from 30 down to 3.

So is this possible, or am I dreaming?

P.S. I know I can just @include http://first-domain.com/* and then use if statements to run certain parts of the script on certain paths within that domain, but the number of pages that the script is intended to run on is only about 2% of the site, so it seems wasteful to include the script on every page of each website.

like image 220
blimpage Avatar asked Jun 18 '13 12:06

blimpage


1 Answers

Reference:

  • Greasemonkey Include and exclude rules
  • Match Patterns
  • Match patterns and globs

The solutions that work on Greasemonkey (which is Firefox), may be different on Chrome and on Tampermonkey.

Three basic approaches:

  1. Use 30 different @include lines: While this may be unpalatable in terms of cut-and-paste coding, it is the one approach that will work the same across browsers and the one that will have the best browser performance. The other approaches require the browser to do (more) checks against possibly every page or iframe visited.

  2. Use a regex @include:

    @include /^http:\/\/(1stDomain\.com|2ndDomain\.com|3rdDomain\.net|etc.)\/(admin\/edit|blog|user\/.+?\/history)/
    

This is one line, and performs fairly well, but the line can get unwieldy, and this will only work on Greasemonkey and Tampermonkey (and probably Scriptish).

  1. Use various combinations of @match, @include and @exclude: I only mention this as a possibility. It's the best-performing approach on straight Chrome, but not very cross-browser for this kind of thing. For Greasemonkey or Tampermonkey use approach 1 or approach 2.

I recommend that you avoid leading wildcards, as much as possible. They slow the browser down the most. EG, don't use something like @include /^.+ .../, or @include http:/*/* if you can avoid it.

like image 135
Brock Adams Avatar answered Nov 02 '22 14:11

Brock Adams