Skip to content

Commit

Permalink
Merge pull request #23 from modos189/humanize_match
Browse files Browse the repository at this point in the history
Humanize match
  • Loading branch information
modos189 authored Feb 26, 2023
2 parents d9bb191 + 879af08 commit e03f48e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lib-iitc-manager",
"version": "1.5.0",
"version": "1.6.0",
"description": "Library for managing IITC plugins",
"main": "src/index.js",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import { Manager } from './manager.js';
import { parseMeta, ajaxGet, getUniqId, getUID, check_meta_match_pattern, wait, clearWait } from './helpers.js';
import { check_matching } from './matching.js';
import { check_matching, humanize_match } from './matching.js';

export { Manager, parseMeta, ajaxGet, getUniqId, getUID, check_meta_match_pattern, wait, clearWait, check_matching };
export { Manager, parseMeta, ajaxGet, getUniqId, getUID, check_meta_match_pattern, wait, clearWait, check_matching, humanize_match };
29 changes: 29 additions & 0 deletions src/matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,32 @@ function matchHost(rule, data) {
function matchPath(rule, data) {
return str2RE(rule).test(data);
}

/**
* Returns information about the domains for which the script will be enabled.
* Returns null if @match and @include are not specified
* Returns <all_urls> if the script will be run for all domains. Additional URL Scheme and Path filters are not taken into account.
* Otherwise, it returns a list of strings with domains.
*
* @param {plugin} meta - Object with data from ==UserScript== header.
* @return {null|string|[]}
*/
export function humanize_match(meta) {
const match = meta.match || [];
const include = meta.include || [];
const matches = match.concat(include);

if (!matches.length) return null;
if (matches.includes('<all_urls>')) return '<all_urls>';

const domains = [];
for (const item of matches) {
const parts = item.match(RE_URL);
if (!parts) continue;

const [, , domain] = parts;
if (domain === '*') return '<all_urls>';
if (!domains.includes(domain)) domains.push(domain);
}
return domains;
}
30 changes: 29 additions & 1 deletion test/matching.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { describe, it } from 'mocha';
import { check_matching } from '../src/matching.js';
import { check_matching, humanize_match } from '../src/matching.js';
import { expect } from 'chai';

describe('scheme', function () {
Expand Down Expand Up @@ -121,3 +121,31 @@ describe('<all_ingress>', function () {
expect(check_matching(script, '<all_ingress>'), 'should match keyword `<all_ingress>`').to.be.true;
});
});

describe('testing humanize_match() function', function () {
it('return null if @match and @include are not set', function () {
const script = {};
expect(humanize_match(script), 'should return null').to.be.null;
});
it('return <all_urls> if @match or @include are set to <all_urls> or domain contains *', function () {
const script1 = {
match: ['*://*/*'],
include: ['*://*/*'],
};
const script2 = {
include: ['<all_urls>'],
};
const script3 = {
include: ['http://www.example.com/*', '<all_urls>'],
};
expect(humanize_match(script1), 'should return <all_urls>').to.be.equal('<all_urls>');
expect(humanize_match(script2), 'should return <all_urls>').to.be.equal('<all_urls>');
expect(humanize_match(script3), 'should return <all_urls>').to.be.equal('<all_urls>');
});
it('return list of domains', function () {
const script = {
match: ['http://www.example.com/*', 'http://www.example2.com/*'],
};
expect(humanize_match(script), 'should return list of domains').deep.equal(['www.example.com', 'www.example2.com']);
});
});

0 comments on commit e03f48e

Please sign in to comment.