Skip to content

Commit

Permalink
add close-window scriptlet. #158 AG-10861
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from feature/AG-10861 to master

Squashed commit of the following:

commit 8cb1878
Merge: ef8230e 15d10e3
Author: Slava Leleka <[email protected]>
Date:   Fri Dec 10 16:37:27 2021 +0300

    Merge branch 'master' into feature/AG-10861

commit ef8230e
Author: Slava Leleka <[email protected]>
Date:   Fri Dec 10 12:02:34 2021 +0300

    specify comment for qunitArgs.redirectConsole

commit 08b9e2e
Merge: a87d645 06fa966
Author: Slava Leleka <[email protected]>
Date:   Fri Dec 10 11:51:06 2021 +0300

    Merge branch 'master' into feature/AG-10861

commit a87d645
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 18:28:42 2021 +0300

    improve tests for close-window

commit 945da9f
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 18:28:09 2021 +0300

    rename scriptlet into close-window

commit ea501b6
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 18:27:26 2021 +0300

    fix qunitArgs.targetUrl for close-window scriptlet path matching

commit 661685f
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 00:55:04 2021 +0300

    add force-window-close test for invalid regexp pattern

commit 2c594e9
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 00:49:04 2021 +0300

    add few simple tests for force-window-close

commit ee46c5f
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 00:47:41 2021 +0300

    add testing helpful comment to qunitArgs

commit da2c656
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 00:41:22 2021 +0300

    add lint-staged for pre-commit hook

commit 4ab1727
Author: Slava Leleka <[email protected]>
Date:   Thu Dec 9 00:36:21 2021 +0300

    improve condition for path arg, catch error if window closing is impossible

commit ff97409
Merge: 3ee16f2 bb7ecbc
Author: Slava Leleka <[email protected]>
Date:   Wed Dec 8 00:04:53 2021 +0300

    Merge branch 'master' into feature/AG-10861

commit 3ee16f2
Author: Slava Leleka <[email protected]>
Date:   Mon Dec 6 14:34:45 2021 +0300

    init force-window-close scriptlet
  • Loading branch information
slavaleleka committed Dec 10, 2021
1 parent 15d10e3 commit 8f600c7
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 5 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
"browserstack": "yarn build-test && node browserstack.js",
"gui-test": "yarn build-test && open http://localhost:8585 && node ./tests/server.js",
"lint": "eslint .",
"lint-staged": "lint-staged",
"wiki:build-docs": "node scripts/build-docs.js",
"wiki:check-updates": "node ./scripts/check-sources-updates.js",
"wiki:update": "yarn wiki:check-updates && node ./scripts/build-compatibility-table.js",
"prepare-builds": "yarn corelibs && yarn build && babel-node ./scripts/build-txt.js",
"prepublishOnly": "yarn prepare-builds",
"increment": "yarn version --patch --no-git-tag-version"
},
"lint-staged": {
"*.js": [
"eslint"
]
},
"husky": {
"hooks": {
"pre-commit": "yarn lint && echo \"DO NOT FORGET ABOUT BUILDS UPDATE (yarn prepare-builds)\""
"pre-commit": "lint-staged && echo \"DO NOT FORGET ABOUT BUILDS UPDATE (yarn prepare-builds)\""
}
},
"author": "AdGuard",
Expand Down Expand Up @@ -58,6 +64,7 @@
"eslint-plugin-compat": "^3.9.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^3.1.0",
"lint-staged": "^12.1.2",
"node-qunit-puppeteer": "^2.0.3",
"qunit": "^2.9.3",
"rollup": "^2.21.0",
Expand Down
54 changes: 54 additions & 0 deletions src/scriptlets/close-window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { hit, toRegExp } from '../helpers';

/**
* @scriptlet close-window
*
* @description
* Closes the browser tab immediately.
*
* **Syntax**
* ```
* example.org#%#//scriptlet('close-window'[, path])
*
* - `path` — optional, string or regular expression
* matching the current location's path: `window.location.pathname` + `window.location.search`.
* Defaults to execute on every page.
*
* **Examples**
* ```
* ! closes any example.org tab
* example.org#%#//scriptlet('close-window')
*
* ! closes specific example.org tab
* example.org#%#//scriptlet('close-window', '/example-page.html')
* ```
*/
export function forceWindowClose(source, path = '') {
const closeImmediately = () => {
try {
hit(source);
window.close();
} catch (e) {
// log the error if window closing is impossible
// https://developer.mozilla.org/en-US/docs/Web/API/Window/close
console.log(e); // eslint-disable-line no-console
}
};

if (path === '') {
closeImmediately();
} else {
const pathRegexp = toRegExp(path);
const currentPath = `${window.location.pathname}${window.location.search}`;

if (pathRegexp.test(currentPath)) {
closeImmediately();
}
}
}

forceWindowClose.names = [
'close-window',
];

forceWindowClose.injections = [hit, toRegExp];
1 change: 1 addition & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export * from './set-session-storage-item';
export * from './abort-on-stack-trace';
export * from './log-on-stack-trace';
export * from './prevent-xhr';
export * from './close-window';
4 changes: 3 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const { runQunitPuppeteer, printFailedTests, printResultSummary } = require('nod
const { server, port } = require('./server');

const qunitArgs = {
targetUrl: `http://localhost:${port}/`,
targetUrl: `http://localhost:${port}?test`,
timeout: 15000,
// needed for logging to console while testing run via `yarn test`
// redirectConsole: true,
puppeteerArgs: ['--no-sandbox', '--allow-file-access-from-files'],
};

Expand Down
60 changes: 60 additions & 0 deletions tests/scriptlets/close-window.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable no-underscore-dangle */
import { runScriptlet, clearGlobalProps } from '../helpers';

const { test, module } = QUnit;
const name = 'close-window';

const nativeWindowClose = window.close;

const TEST_PROP = 'run';

const mockedWindowClose = () => {
window[TEST_PROP] = true;
};

const beforeEach = () => {
window.__debug = () => {
window.hit = 'value';
};
window.close = mockedWindowClose;
window[TEST_PROP] = false;
};

const afterEach = () => {
window.close = nativeWindowClose;
clearGlobalProps('hit', '__debug', TEST_PROP);
};

module(name, { beforeEach, afterEach });

test('works: no args', (assert) => {
assert.equal(window.hit, undefined, 'Hit function not executed yet');

const scriptletArgs = [''];
runScriptlet(name, scriptletArgs);

assert.equal(window.hit, 'value', 'Hit function was executed');
// scriptlet calls window.close which is mocked for test purposes
assert.strictEqual(window[TEST_PROP], true, 'mocked window.close() has been called');
});

test('works: string path', (assert) => {
assert.equal(window.hit, undefined, 'Hit function not executed yet');

const scriptletArgs = ['test'];
runScriptlet(name, scriptletArgs);

assert.equal(window.hit, 'value', 'Hit function was executed');
// scriptlet calls window.close which is mocked for test purposes
assert.strictEqual(window[TEST_PROP], true, 'mocked window.close() has been called');
});

test('does not work: invalid regexp pattern', (assert) => {
assert.equal(window.hit, undefined, 'Hit function not executed yet');

const scriptletArgs = ['/*/'];
runScriptlet(name, scriptletArgs);

assert.equal(window.hit, undefined, 'Hit should not be executed');
assert.strictEqual(window[TEST_PROP], false, 'mocked window.close() was not called');
});
1 change: 1 addition & 0 deletions tests/scriptlets/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ import './set-local-storage-item.test';
import './set-session-storage-item.test';
import './abort-on-stack-trace.test';
import './log-on-stack-trace.test';
import './close-window.test';
Loading

0 comments on commit 8f600c7

Please sign in to comment.