Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new setting excludedUrlsRegex #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions Vimari Extension/js/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function setSettings(msg) {

function activateExtension(settings) {
if ((typeof settings != "undefined") &&
isExcludedUrl(settings.excludedUrls, document.URL)) {
isExcludedUrl(settings.excludedUrls, settings.excludedUrlsRegex, document.URL)) {
return;
}

Expand All @@ -363,22 +363,28 @@ function activateExtension(settings) {
bindKeyCodesToActions(settings);
}

function isExcludedUrl(storedExcludedUrls, currentUrl) {
if (!storedExcludedUrls.length) {
return false;
}
function isExcludedUrl(storedExcludedUrls, storedExcludedUrlsRegex, currentUrl) {
if (storedExcludedUrls.length > 0) {
var excludedUrls, regexp, url, formattedUrl, _i, _len;
excludedUrls = storedExcludedUrls.split(",");
for (_i = 0, _len = excludedUrls.length; _i < _len; _i++) {
url = excludedUrls[_i];
formattedUrl = stripProtocolAndWww(url);
formattedUrl = formattedUrl.toLowerCase().trim();
regexp = new RegExp('((.*)?(' + formattedUrl + ')+(.*))');
if (currentUrl.toLowerCase().match(regexp)) {
return true;
}
}
}

var excludedUrls, regexp, url, formattedUrl, _i, _len;
excludedUrls = storedExcludedUrls.split(",");
for (_i = 0, _len = excludedUrls.length; _i < _len; _i++) {
url = excludedUrls[_i];
formattedUrl = stripProtocolAndWww(url);
formattedUrl = formattedUrl.toLowerCase().trim();
regexp = new RegExp('((.*)?(' + formattedUrl + ')+(.*))');
if (currentUrl.toLowerCase().match(regexp)) {
if (storedExcludedUrlsRegex.length > 0) {
var regexp = new RegExp(storedExcludedUrlsRegex, 'i');
if (currentUrl.match(regexp)) {
return true;
}
}

return false;
}

Expand Down
1 change: 1 addition & 0 deletions Vimari Extension/json/defaultSettings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"excludedUrls": "",
"excludedUrlsRegex": "",
"linkHintCharacters": "asdfjklqwerzxc",
"detectByCursorStyle": false,
"scrollSize": 150,
Expand Down
96 changes: 80 additions & 16 deletions tests/vimari.spec.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,156 @@
const expect = require('expect.js');

describe('isExcludedUrl', () => {
describe('isExcludedUrl with excludedUrls', () => {
const isExcludedUrl = window.isExcludedUrl;

it('returns true on same exact domain', () => {
const excludedUrl = 'specific-domain.com';
const currentUrl = excludedUrl;
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true on duplicate domains', () => {
const excludedUrls = 'specific-domain.com,specific-domain.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrls, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrls, '', currentUrl)).to.be.ok();
});

it('returns true if any domain match', () => {
const excludedUrls = 'different-domain.com,specific-domain.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrls, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrls, '', currentUrl)).to.be.ok();
});

it('returns true on comma separated domains', () => {
const excludedUrls = 'specific-domain.com,different-domain.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrls, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrls, '', currentUrl)).to.be.ok();
});

it('returns false on different domain', () => {
const excludedUrl = 'www.different-domain.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.not.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.not.be.ok();
});

it('returns false if no domains match', () => {
const excludedUrls = 'www.different-domain.com,www.different-domain-2.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrls, currentUrl)).to.not.be.ok();
expect(isExcludedUrl(excludedUrls, '', currentUrl)).to.not.be.ok();
});

it('returns false on space separated domains', () => {
const excludedUrls = 'specific-domain.com different-domain.com';
const currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrls, currentUrl)).to.not.be.ok();
expect(isExcludedUrl(excludedUrls, '', currentUrl)).to.not.be.ok();
});

it('returns true on string added in front of current URL', () => {
const excludedUrl = 'specific-domain.com';
const currentUrl = 'http://specific-domain.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true on string appended to current URL', () => {
const excludedUrl = 'specific-domain.com';
const currentUrl = 'specific-domain.com/arbitrary-string';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true on string added on both sides of current URL', () => {
const excludedUrl = 'specific-domain.com';
const currentUrl = 'http://specific-domain.com/arbitrary-string';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true if current URL is less specific than excluded domain', () => {
let excludedUrl = 'http://specific-domain.com';
let currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();

excludedUrl = 'http://www.specific-domain.com';
currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true if current URL with appended string is less specific than excluded domain', () => {
const excludedUrl = 'http://specific-domain.com';
const currentUrl = 'specific-domain.com/arbitrary-string';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});

it('returns true even though cases doesn\'t match', () => {
let excludedUrl = 'SPECIFIC-DOMAIN.com';
let currentUrl = 'specific-domain.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();

excludedUrl = 'specific-domain.com';
currentUrl = 'SPECIFIC-DOMAIN.com';
expect(isExcludedUrl(excludedUrl, currentUrl)).to.be.ok();
expect(isExcludedUrl(excludedUrl, '', currentUrl)).to.be.ok();
});
});

describe('isExcludedUrl with excludedUrlsRegex', () => {
const isExcludedUrl = window.isExcludedUrl;

it('returns true on same exact domain', () => {
const excludedUrl = '^https://specific-domain\\.com/';
const currentUrl = 'https://specific-domain.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
});

it('returns true on string appended to current URL', () => {
const excludedUrl = '^https://specific-domain\\.com/';
const currentUrl = 'https://specific-domain.com/foo';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
});

it('returns true on duplicate domains', () => {
const excludedUrls = '^https://(specific-domain\\.com|specific-domain\\.com)/';
const currentUrl = 'https://specific-domain.com/foo';
expect(isExcludedUrl('', excludedUrls, currentUrl)).to.be.ok();
});

it('returns true if any domain match', () => {
const excludedUrls = '^https://(specific-domain|other-domain)\\.com/';
const currentUrl = 'https://specific-domain.com/foo';
expect(isExcludedUrl('', excludedUrls, currentUrl)).to.be.ok();
});

it('returns false on different domain', () => {
const excludedUrl = '^https://www\\.different-domain\\.com';
const currentUrl = 'https://specific-domain.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.not.be.ok();
});

it('returns false if does not match right pattern', () => {
const excludedUrls = '^https://blah\\.com/';
const currentUrl = 'https://foo.com/https://blah.com';
expect(isExcludedUrl('', excludedUrls, currentUrl)).to.not.be.ok();
});

it('returns true on string added on both sides of current URL', () => {
const excludedUrl = 'specific-domain.com';
const currentUrl = 'http://specific-domain.com/arbitrary-string';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
});

it('returns true if current URL is less specific than excluded domain', () => {
let excludedUrl = '^http://(www\\.)?specific-domain\\.com/';
let currentUrl = 'http://specific-domain.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
currentUrl = 'http://www.specific-domain.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
});

it('returns true even though cases doesn\'t match', () => {
let excludedUrl = '^https://SPECIFIC-DOMAIN\\.com/';
let currentUrl = 'https://specific-domain.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();

excludedUrl = '^https://specific-domain\\.com/';
currentUrl = 'https://SPECIFIC-DOMAIN.com/';
expect(isExcludedUrl('', excludedUrl, currentUrl)).to.be.ok();
});
});

Expand Down