Best JavaScript code snippet using playwright-internal
test_MatchURLFilters.js
Source:test_MatchURLFilters.js
1/* Any copyright is dedicated to the Public Domain.2 http://creativecommons.org/publicdomain/zero/1.0/ */3"use strict";4Components.utils.import("resource://gre/modules/MatchPattern.jsm");5Components.utils.import("resource://gre/modules/Services.jsm");6function createTestFilter({url, filters}) {7 let m = new MatchURLFilters(filters);8 return m.matches(url);9}10function expectPass({url, filters}) {11 ok(createTestFilter({url, filters}),12 `Expected match: ${JSON.stringify(filters)}, ${url}`);13}14function expectFail({url, filters}) {15 ok(!createTestFilter({url, filters}),16 `Expected no match: ${JSON.stringify(filters)}, ${url}`);17}18function expectThrow({url, filters, exceptionMessageContains}) {19 let logData = {filters, url};20 Assert.throws(21 () => {22 createTestFilter({url, filters});23 },24 exceptionMessageContains,25 `Check received exception for expected message: ${JSON.stringify(logData)}`26 );27}28add_task(async function test_match_url_filters() {29 const shouldPass = true;30 const shouldFail = true;31 const shouldThrow = true;32 var testCases = [33 // Empty, undefined and null filters.34 {shouldThrow, exceptionMessageContains: "filters array should not be empty",35 filters: [], url: "http://mozilla.org", },36 {shouldThrow, exceptionMessageContains: "filters should be an array",37 filters: undefined, url: "http://mozilla.org"},38 {shouldThrow, exceptionMessageContains: "filters should be an array",39 filters: null, url: "http://mozilla.org"},40 // Wrong formats (in a real webextension this will be blocked by the schema validation).41 {shouldThrow, exceptionMessageContains: "filters should be an array", filters: {},42 url: "http://mozilla.org"},43 {shouldThrow, exceptionMessageContains: "filters should be an array",44 filters: {nonExistentCriteria: true}, url: "http://mozilla.org", },45 {shouldPass, filters: [{nonExistentCriteria: true}], url: "http://mozilla.org"},46 // Schemes filter over various url schemes.47 {shouldPass, filters: [{schemes: ["http"]}], url: "http://mozilla.org"},48 {shouldPass, filters: [{schemes: ["https"]}], url: "https://mozilla.org"},49 {shouldPass, filters: [{schemes: ["ftp"]}], url: "ftp://fake/ftp/url"},50 {shouldPass, filters: [{schemes: ["about"]}], url: "about:blank"},51 {shouldPass, filters: [{schemes: ["data"]}], url: "data:,testDataURL"},52 {shouldFail, filters: [{schemes: ["http"]}], url: "ftp://fake/ftp/url"},53 // Multiple schemes: pass when at least one scheme matches.54 {shouldPass, filters: [{schemes: ["https", "about"]}], url: "https://mozilla.org"},55 {shouldPass, filters: [{schemes: ["about", "https"]}], url: "https://mozilla.org"},56 {shouldFail, filters: [{schemes: ["about", "http"]}], url: "https://mozilla.org"},57 // Port filter: standard (implicit) ports.58 {shouldPass, filters: [{ports: [443]}], url: "https://mozilla.org"},59 {shouldPass, filters: [{ports: [80]}], url: "http://mozilla.org"},60 {shouldPass, filters: [{ports: [21]}], url: "ftp://ftp.mozilla.org"},61 // Port filter: schemes without a default port.62 {shouldFail, filters: [{ports: [-1]}], url: "about:blank"},63 {shouldFail, filters: [{ports: [-1]}], url: "data:,testDataURL"},64 {shouldFail, filters: [{ports: [[1, 65535]]}], url: "about:blank"},65 {shouldFail, filters: [{ports: [[1, 65535]]}], url: "data:,testDataURL"},66 // Host filters (hostEquals, hostContains, hostPrefix, hostSuffix): schemes with an host.67 {shouldFail, filters: [{hostEquals: ""}], url: "https://mozilla.org"},68 {shouldPass, filters: [{hostEquals: null}], url: "https://mozilla.org"},69 {shouldPass, filters: [{hostEquals: "mozilla.org"}], url: "https://mozilla.org"},70 {shouldFail, filters: [{hostEquals: "mozilla.com"}], url: "https://mozilla.org"},71 // NOTE: trying at least once another valid protocol.72 {shouldPass, filters: [{hostEquals: "mozilla.org"}], url: "ftp://mozilla.org"},73 {shouldFail, filters: [{hostEquals: "mozilla.com"}], url: "ftp://mozilla.org"},74 {shouldPass, filters: [{hostEquals: "mozilla.org"}], url: "https://mozilla.org:8888"},75 {shouldPass, filters: [{hostContains: "moz"}], url: "https://mozilla.org"},76 // NOTE: an implicit '.' char is inserted into the host.77 {shouldPass, filters: [{hostContains: ".moz"}], url: "https://mozilla.org"},78 {shouldFail, filters: [{hostContains: "com"}], url: "https://mozilla.org"},79 {shouldPass, filters: [{hostContains: ""}], url: "https://mozilla.org"},80 {shouldPass, filters: [{hostContains: null}], url: "https://mozilla.org"},81 {shouldPass, filters: [{hostPrefix: "moz"}], url: "https://mozilla.org"},82 {shouldFail, filters: [{hostPrefix: "org"}], url: "https://mozilla.org"},83 {shouldPass, filters: [{hostPrefix: ""}], url: "https://mozilla.org"},84 {shouldPass, filters: [{hostPrefix: null}], url: "https://mozilla.org"},85 {shouldPass, filters: [{hostSuffix: ".org"}], url: "https://mozilla.org"},86 {shouldFail, filters: [{hostSuffix: "moz"}], url: "https://mozilla.org"},87 {shouldPass, filters: [{hostSuffix: ""}], url: "https://mozilla.org"},88 {shouldPass, filters: [{hostSuffix: null}], url: "https://mozilla.org"},89 {shouldPass, filters: [{hostSuffix: "lla.org"}], url: "https://mozilla.org:8888"},90 // hostEquals: urls without an host.91 // TODO: should we explicitly cover hostContains, hostPrefix, hostSuffix for92 // these sub-cases?93 {shouldFail, filters: [{hostEquals: "blank"}], url: "about:blank"},94 {shouldFail, filters: [{hostEquals: "blank"}], url: "about://blank"},95 {shouldFail, filters: [{hostEquals: "testDataURL"}], url: "data:,testDataURL"},96 {shouldPass, filters: [{hostEquals: ""}], url: "about:blank"},97 {shouldPass, filters: [{hostEquals: ""}], url: "about://blank"},98 {shouldPass, filters: [{hostEquals: ""}], url: "data:,testDataURL"},99 // Path filters (pathEquals, pathContains, pathPrefix, pathSuffix).100 {shouldFail, filters: [{pathEquals: ""}], url: "https://mozilla.org/test/path"},101 {shouldPass, filters: [{pathEquals: null}], url: "https://mozilla.org/test/path"},102 {shouldPass, filters: [{pathEquals: "/test/path"}], url: "https://mozilla.org/test/path"},103 {shouldFail, filters: [{pathEquals: "/wrong/path"}], url: "https://mozilla.org/test/path"},104 {shouldPass, filters: [{pathEquals: "/test/path"}], url: "https://mozilla.org:8888/test/path"},105 // NOTE: trying at least once another valid protocol106 {shouldPass, filters: [{pathEquals: "/test/path"}], url: "ftp://mozilla.org/test/path"},107 {shouldFail, filters: [{pathEquals: "/wrong/path"}], url: "ftp://mozilla.org/test/path"},108 {shouldPass, filters: [{pathContains: "st/"}], url: "https://mozilla.org/test/path"},109 {shouldPass, filters: [{pathContains: "/test"}], url: "https://mozilla.org/test/path"},110 {shouldFail, filters: [{pathContains: "org"}], url: "https://mozilla.org/test/path"},111 {shouldPass, filters: [{pathContains: ""}], url: "https://mozilla.org/test/path"},112 {shouldPass, filters: [{pathContains: null}], url: "https://mozilla.org/test/path"},113 {shouldFail, filters: [{pathContains: "param"}], url: "https://mozilla.org:8888/test/path?param=1"},114 {shouldFail, filters: [{pathContains: "ref"}], url: "https://mozilla.org:8888/test/path#ref"},115 {shouldPass, filters: [{pathContains: "st/pa"}], url: "https://mozilla.org:8888/test/path"},116 {shouldPass, filters: [{pathPrefix: "/te"}], url: "https://mozilla.org/test/path"},117 {shouldFail, filters: [{pathPrefix: "org/"}], url: "https://mozilla.org/test/path"},118 {shouldPass, filters: [{pathPrefix: ""}], url: "https://mozilla.org/test/path"},119 {shouldPass, filters: [{pathPrefix: null}], url: "https://mozilla.org/test/path"},120 {shouldPass, filters: [{pathSuffix: "/path"}], url: "https://mozilla.org/test/path"},121 {shouldFail, filters: [{pathSuffix: "th/"}], url: "https://mozilla.org/test/path"},122 {shouldPass, filters: [{pathSuffix: ""}], url: "https://mozilla.org/test/path"},123 {shouldPass, filters: [{pathSuffix: null}], url: "https://mozilla.org/test/path"},124 {shouldFail, filters: [{pathSuffix: "p=1"}], url: "https://mozilla.org:8888/test/path?p=1"},125 {shouldFail, filters: [{pathSuffix: "ref"}], url: "https://mozilla.org:8888/test/path#ref"},126 // Query filters (queryEquals, queryContains, queryPrefix, querySuffix).127 {shouldFail, filters: [{queryEquals: ""}], url: "https://mozilla.org/?param=val"},128 {shouldPass, filters: [{queryEquals: null}], url: "https://mozilla.org/?param=val"},129 {shouldPass, filters: [{queryEquals: "param=val"}], url: "https://mozilla.org/?param=val"},130 {shouldFail, filters: [{queryEquals: "?param=val"}], url: "https://mozilla.org/?param=val"},131 {shouldFail, filters: [{queryEquals: "/path?param=val"}], url: "https://mozilla.org/path?param=val"},132 // NOTE: about scheme urls cannot be matched by query.133 {shouldFail, filters: [{queryEquals: "param=val"}], url: "about:blank?param=val"},134 {shouldFail, filters: [{queryEquals: "param"}], url: "ftp://mozilla.org?param=val"},135 {shouldPass, filters: [{queryContains: "ram"}], url: "https://mozilla.org/?param=val"},136 {shouldPass, filters: [{queryContains: "=val"}], url: "https://mozilla.org/?param=val"},137 {shouldFail, filters: [{queryContains: "?param"}], url: "https://mozilla.org/?param=val"},138 {shouldFail, filters: [{queryContains: "path"}], url: "https://mozilla.org/path/?p=v#ref"},139 {shouldPass, filters: [{queryContains: ""}], url: "https://mozilla.org/?param=val"},140 {shouldPass, filters: [{queryContains: null}], url: "https://mozilla.org/?param=val"},141 {shouldPass, filters: [{queryPrefix: "param"}], url: "https://mozilla.org/?param=val"},142 {shouldFail, filters: [{queryPrefix: "p="}], url: "https://mozilla.org/?param=val"},143 {shouldFail, filters: [{queryPrefix: "path"}], url: "https://mozilla.org/path?param=val"},144 {shouldPass, filters: [{queryPrefix: ""}], url: "https://mozilla.org/?param=val"},145 {shouldPass, filters: [{queryPrefix: null}], url: "https://mozilla.org/?param=val"},146 {shouldPass, filters: [{querySuffix: "=val"}], url: "https://mozilla.org/?param=val"},147 {shouldFail, filters: [{querySuffix: "=wrong"}], url: "https://mozilla.org/?param=val"},148 {shouldPass, filters: [{querySuffix: ""}], url: "https://mozilla.org/?param=val"},149 {shouldPass, filters: [{querySuffix: null}], url: "https://mozilla.org/?param=val"},150 // URL filters (urlEquals, urlContains, urlPrefix, urlSuffix).151 {shouldFail, filters: [{urlEquals: ""}], url: "https://mozilla.org/?p=v#ref"},152 {shouldPass, filters: [{urlEquals: null}], url: "https://mozilla.org/?p=v#ref"},153 {shouldPass, filters: [{urlEquals: "https://mozilla.org/?p=v#ref"}],154 url: "https://mozilla.org/?p=v#ref"},155 {shouldFail, filters: [{urlEquals: "https://mozilla.org/?p=v#ref2"}],156 url: "https://mozilla.org/?p=v#ref"},157 {shouldPass, filters: [{urlEquals: "about:blank?p=v#ref"}], url: "about:blank?p=v#ref"},158 {shouldPass, filters: [{urlEquals: "ftp://mozilla.org?p=v#ref"}],159 url: "ftp://mozilla.org?p=v#ref"},160 {shouldPass, filters: [{urlContains: "org/?p"}], url: "https://mozilla.org/?p=v#ref"},161 {shouldPass, filters: [{urlContains: "=v#ref"}], url: "https://mozilla.org/?p=v#ref"},162 {shouldFail, filters: [{urlContains: "ftp"}], url: "https://mozilla.org/?p=v#ref"},163 {shouldPass, filters: [{urlContains: ""}], url: "https://mozilla.org/?p=v#ref"},164 {shouldPass, filters: [{urlContains: null}], url: "https://mozilla.org/?p=v#ref"},165 {shouldPass, filters: [{urlPrefix: "http"}], url: "https://mozilla.org/?p=v#ref"},166 {shouldFail, filters: [{urlPrefix: "moz"}], url: "https://mozilla.org/?p=v#ref"},167 {shouldPass, filters: [{urlPrefix: ""}], url: "https://mozilla.org/?p=v#ref"},168 {shouldPass, filters: [{urlPrefix: null}], url: "https://mozilla.org/?p=v#ref"},169 {shouldPass, filters: [{urlSuffix: "#ref"}], url: "https://mozilla.org/?p=v#ref"},170 {shouldFail, filters: [{urlSuffix: "=wrong"}], url: "https://mozilla.org/?p=v#ref"},171 {shouldPass, filters: [{urlSuffix: ""}], url: "https://mozilla.org/?p=v#ref"},172 {shouldPass, filters: [{urlSuffix: null}], url: "https://mozilla.org/?p=v#ref"},173 // More url filters: urlMatches.174 {shouldPass, filters: [{urlMatches: ".*://mozilla"}], url: "https://mozilla.org/?p=v#ref"},175 {shouldPass, filters: [{urlMatches: ".*://mozilla"}], url: "ftp://mozilla.org/?p=v#ref"},176 {shouldPass, filters: [{urlMatches: ".*://.*/\?p"}], url: "ftp://mozilla.org/?p=v#ref"},177 // NOTE: urlMatches should not match the url without the ref.178 {shouldFail, filters: [{urlMatches: "v#ref$"}], url: "https://mozilla.org/?p=v#ref"},179 {shouldPass, filters: [{urlMatches: "^ftp"}], url: "ftp://mozilla.org/?p=v#ref"},180 // More url filters: originAndPathMatches.181 {shouldPass, filters: [{originAndPathMatches: ".*://mozilla"}],182 url: "https://mozilla.org/?p=v#ref"},183 {shouldPass, filters: [{originAndPathMatches: ".*://mozilla"}],184 url: "ftp://mozilla.org/?p=v#ref"},185 // NOTE: urlMatches should not match the url without the query and the ref.186 {shouldFail, filters: [{originAndPathMatches: ".*://.*/\?p"}],187 url: "ftp://mozilla.org/?p=v#ref"},188 {shouldFail, filters: [{originAndPathMatches: "v#ref$"}],189 url: "https://mozilla.org/?p=v#ref"},190 {shouldPass, filters: [{originAndPathMatches: "^ftp"}],191 url: "ftp://mozilla.org/?p=v#ref"},192 // Filter with all criteria: all matches, none matches, some matches.193 // All matches.194 {shouldPass, filters: [195 {196 schemes: ["https", "http"],197 ports: [443, 80],198 hostEquals: "www.mozilla.org",199 hostContains: ".moz",200 hostPrefix: "www",201 hostSuffix: "org",202 pathEquals: "/sub/path",203 pathContains: "b/p",204 pathPrefix: "/sub",205 pathSuffix: "/path",206 queryEquals: "p=v",207 queryContains: "1=",208 queryPrefix: "p1",209 querySuffix: "=v",210 urlEquals: "https://www.mozilla.org/sub/path?p1=v#ref",211 urlContains: "org/sub",212 urlPrefix: "https://moz",213 urlSuffix: "#ref",214 urlMatches: "v#ref$",215 originAndPathMatches: ".*://moz.*/"216 },217 ], url: "https://www.mozilla.org/sub/path?p1=v#ref"},218 // None matches.219 {shouldFail, filters: [220 {221 schemes: ["http"],222 ports: [80],223 hostEquals: "mozilla.com",224 hostContains: "www.moz",225 hostPrefix: "www",226 hostSuffix: "com",227 pathEquals: "/wrong/path",228 pathContains: "g/p",229 pathPrefix: "/wrong",230 pathSuffix: "/wrong",231 queryEquals: "p2=v",232 queryContains: "2=",233 queryPrefix: "p2",234 querySuffix: "=value",235 urlEquals: "http://mozilla.com/sub/path?p1=v#ref",236 urlContains: "com/sub",237 urlPrefix: "http://moz",238 urlSuffix: "#ref2",239 urlMatches: "value#ref2$",240 originAndPathMatches: ".*://moz.*com/"241 },242 ], url: "https://mozilla.org/sub/path?p1=v#ref"},243 // Some matches244 {shouldFail, filters: [245 {246 schemes: ["https"],247 ports: [80],248 hostEquals: "mozilla.com",249 hostContains: "www.moz",250 hostPrefix: "www",251 hostSuffix: "com",252 pathEquals: "/wrong/path",253 pathContains: "g/p",254 pathPrefix: "/wrong",255 pathSuffix: "/wrong",256 queryEquals: "p2=v",257 queryContains: "2=",258 queryPrefix: "p2",259 querySuffix: "=value",260 urlEquals: "http://mozilla.com/sub/path?p1=v#ref",261 urlContains: "com/sub",262 urlPrefix: "http://moz",263 urlSuffix: "#ref2",264 urlMatches: "value#ref2$",265 originAndPathMatches: ".*://moz.*com/"266 },267 ], url: "https://mozilla.org/sub/path?p1=v#ref"},268 // Filter with multiple filters: all matches, some matches, none matches.269 // All matches.270 {shouldPass, filters: [271 {schemes: ["https", "http"]},272 {ports: [443, 80]},273 {hostEquals: "www.mozilla.org"},274 {hostContains: ".moz"},275 {hostPrefix: "www"},276 {hostSuffix: "org"},277 {pathEquals: "/sub/path"},278 {pathContains: "b/p"},279 {pathPrefix: "/sub"},280 {pathSuffix: "/path"},281 {queryEquals: "p=v"},282 {queryContains: "1="},283 {queryPrefix: "p1"},284 {querySuffix: "=v"},285 {urlEquals: "https://www.mozilla.org/sub/path?p1=v#ref"},286 {urlContains: "org/sub"},287 {urlPrefix: "https://moz"},288 {urlSuffix: "#ref"},289 {urlMatches: "v#ref$"},290 {originAndPathMatches: ".*://moz.*/"},291 ], url: "https://www.mozilla.org/sub/path?p1=v#ref"},292 // None matches.293 {shouldFail, filters: [294 {schemes: ["http"]},295 {ports: [80]},296 {hostEquals: "mozilla.com"},297 {hostContains: "www.moz"},298 {hostPrefix: "www"},299 {hostSuffix: "com"},300 {pathEquals: "/wrong/path"},301 {pathContains: "g/p"},302 {pathPrefix: "/wrong"},303 {pathSuffix: "/wrong"},304 {queryEquals: "p2=v"},305 {queryContains: "2="},306 {queryPrefix: "p2"},307 {querySuffix: "=value"},308 {urlEquals: "http://mozilla.com/sub/path?p1=v#ref"},309 {urlContains: "com/sub"},310 {urlPrefix: "http://moz"},311 {urlSuffix: "#ref2"},312 {urlMatches: "value#ref2$"},313 {originAndPathMatches: ".*://moz.*com/"},314 ], url: "https://mozilla.org/sub/path?p1=v#ref"},315 // Some matches.316 {shouldPass, filters: [317 {schemes: ["https"]},318 {ports: [80]},319 {hostEquals: "mozilla.com"},320 {hostContains: "www.moz"},321 {hostPrefix: "www"},322 {hostSuffix: "com"},323 {pathEquals: "/wrong/path"},324 {pathContains: "g/p"},325 {pathPrefix: "/wrong"},326 {pathSuffix: "/wrong"},327 {queryEquals: "p2=v"},328 {queryContains: "2="},329 {queryPrefix: "p2"},330 {querySuffix: "=value"},331 {urlEquals: "http://mozilla.com/sub/path?p1=v#ref"},332 {urlContains: "com/sub"},333 {urlPrefix: "http://moz"},334 {urlSuffix: "#ref2"},335 {urlMatches: "value#ref2$"},336 {originAndPathMatches: ".*://moz.*com/"},337 ], url: "https://mozilla.org/sub/path?p1=v#ref"},338 ];339 // Run all the the testCases defined above.340 for (let currentTest of testCases) {341 let {342 exceptionMessageContains,343 url, filters,344 } = currentTest;345 if (currentTest.shouldThrow) {346 expectThrow({url, filters, exceptionMessageContains})347 } else if (currentTest.shouldFail) {348 expectFail({url, filters});349 } else {350 expectPass({url, filters});351 }352 }...
background.js
Source:background.js
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4// When the extension is installed or upgraded ...5chrome.runtime.onInstalled.addListener(function() {6 // Replace all rules ...7 chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {8 // With a new rule ...9 chrome.declarativeContent.onPageChanged.addRules([10 {11 // That fires when a page's URL is a known package manager12 conditions: [13 new chrome.declarativeContent.PageStateMatcher({14 pageUrl: { urlMatches: 'rubygems\.org\/gems\/*' },15 }),16 new chrome.declarativeContent.PageStateMatcher({17 pageUrl: { urlMatches: 'npmjs\.com\/package\/*' },18 }),19 new chrome.declarativeContent.PageStateMatcher({20 pageUrl: { urlMatches: 'packagist\.org\/packages\/*' },21 }),22 new chrome.declarativeContent.PageStateMatcher({23 pageUrl: { urlMatches: 'pypi\.python\.org\/pypi\/*' },24 }),25 new chrome.declarativeContent.PageStateMatcher({26 pageUrl: { urlMatches: 'nuget\.org\/packages\/*' },27 }),28 new chrome.declarativeContent.PageStateMatcher({29 pageUrl: { urlMatches: 'wordpress\.org\/plugins\/*' },30 }),31 new chrome.declarativeContent.PageStateMatcher({32 pageUrl: { urlMatches: 'metacpan\.org\/release\/*' },33 }),34 new chrome.declarativeContent.PageStateMatcher({35 pageUrl: { urlMatches: 'cocoapods\.org\/pods\/*' },36 }),37 new chrome.declarativeContent.PageStateMatcher({38 pageUrl: { urlMatches: 'cran\.r-project\.org\/web\/packages\/*' },39 }),40 new chrome.declarativeContent.PageStateMatcher({41 pageUrl: { urlMatches: 'hackage\.haskell\.org\/package\/*' },42 }),43 new chrome.declarativeContent.PageStateMatcher({44 pageUrl: { urlMatches: 'atom\.io\/packages\/*' },45 }),46 new chrome.declarativeContent.PageStateMatcher({47 pageUrl: { urlMatches: 'crates\.io\/crates\/*' },48 }),49 new chrome.declarativeContent.PageStateMatcher({50 pageUrl: { urlMatches: 'brewformulas\.org\/*' },51 }),52 new chrome.declarativeContent.PageStateMatcher({53 pageUrl: { urlMatches: 'hex\.pm\/packages\/*' },54 }),55 new chrome.declarativeContent.PageStateMatcher({56 pageUrl: { urlMatches: 'melpa.org\/*' },57 }),58 new chrome.declarativeContent.PageStateMatcher({59 pageUrl: { urlMatches: 'pub\.dartlang\.org\/packages\/*' },60 }),61 new chrome.declarativeContent.PageStateMatcher({62 pageUrl: { urlMatches: 'code\.dlang\.org\/packages\/*' },63 }),64 new chrome.declarativeContent.PageStateMatcher({65 pageUrl: { urlMatches: 'lib\.haxe\.org\/p\/*' },66 }),67 new chrome.declarativeContent.PageStateMatcher({68 pageUrl: { urlMatches: 'inqlude\.org\/libraries\/*' },69 }),70 new chrome.declarativeContent.PageStateMatcher({71 pageUrl: { urlMatches: 'github\.com\/([-_\\w]+)\\/([-_.\\w]+)' },72 }),73 new chrome.declarativeContent.PageStateMatcher({74 pageUrl: { urlMatches: 'gitlab\.com\/([-_\\w]+)\\/([-_.\\w]+)' },75 }),76 new chrome.declarativeContent.PageStateMatcher({77 pageUrl: { urlMatches: 'bitbucket\.org\/([-_\\w]+)\\/([-_.\\w]+)' },78 }),79 ],80 // And shows the extension's page action.81 actions: [ new chrome.declarativeContent.ShowPageAction() ]82 }83 ]);84 });...
ViewSwitch.js
Source:ViewSwitch.js
1import React from 'react';2import PropTypes from 'prop-types';3import Button from '../../molecules/Button/Button';4import './ViewSwitch.styl';5const cookies = require('cookies-js');6class ViewSwitch extends React.Component {7 constructor(props) {8 super(props);9 this.handleClick = this.handleClick.bind(this);10 this.newView = this.props.view === 'list' ? 'grid' : 'list';11 this.cookieName = 'layout';12 }13 getCurrentDomain() {14 return window.location.host;15 }16 getCurrentPath() {17 return window.location.pathname;18 }19 getSegmentUrl() {20 const url = this.getCurrentPath();21 const regExpPagination = /^(.*)(\/[0-9]+_[0-9]+\/)/g;22 const regExpNoPagination = /^(.*)(\/[^/]+-criteria\.html)$/g;23 let urlMatches = regExpPagination.exec(url);24 if (urlMatches === null) {25 urlMatches = regExpNoPagination.exec(url);26 }27 return urlMatches !== null && urlMatches[1] !== undefined ? urlMatches[1] : '/';28 }29 handleClick(event) {30 const optionsDelete = {31 domain: this.getCurrentDomain(),32 path: this.getSegmentUrl()33 };34 const optionsAdd = {35 domain: this.getCurrentDomain()36 };37 if (event) {38 event.preventDefault();39 }40 /*41 * We need Cookie.expire for users who have old cookies set with path.42 * It can be removed later.43 */44 cookies.expire(this.cookieName, optionsDelete);45 cookies.set(this.cookieName, this.newView, optionsAdd);46 window.location.reload();47 }48 render() {49 const { props } = this;50 return (51 <div className="dc-view-switch">52 <span className="dc-view-switch-label">{props.title}</span>53 <Button54 classes="dc-view-switch-button"55 type="button"56 style="none"57 onClick={this.handleClick}58 disabled={props.view === 'grid'}59 icon="GridView"60 >61 {props.gridButtonLabel}62 </Button>63 <Button64 classes="dc-view-switch-button"65 type="button"66 style="none"67 onClick={this.handleClick}68 disabled={props.view === 'list'}69 icon="ListView"70 >71 {props.listButtonLabel}72 </Button>73 </div>74 );75 }76}77ViewSwitch.displayName = 'ViewSwitch';78ViewSwitch.propTypes = {79 view: PropTypes.string,80 title: PropTypes.string,81 gridButtonLabel: PropTypes.string,82 listButtonLabel: PropTypes.string83};84ViewSwitch.defaultProps = {85 view: 'grid',86 title: 'View:',87 gridButtonLabel: 'Grid',88 listButtonLabel: 'List'89};...
opengraph2.js
Source:opengraph2.js
1var request = require('request');2module.exports = function (url, parsedUrl, callbacks) {3 // Article only4 // Request to the site, let's find out about the image5 request({url:url,6 headers: { "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410."},7 pool:false}, function (error, response, body) {8 // If there was an error upon connecting, we log the error and get out9 if (error) {10 callbacks.error(error, site);11 return true;12 }13 // We look for the correct tag in the body14 try{15 var urlMatches = body.match(/property="og:url"\s+?content="(.+?)"/);16 var titleMatches = body.match(/property="og:title"\s+?content="(.+?)"/);17 var imageMatches = body.match(/property="og:image"\s+?content="(.+?)"/);18 var typeMatches = body.match(/property="og:type"\s+?content="(.+?)"/);19 if (!urlMatches){urlMatches=[];}20 if (!titleMatches){titleMatches=[];}21 if (!imageMatches){imageMatches=[];}22 if (!typeMatches){titleMatches=[];}23 if (!typeMatches){24 if (imageMatches[1]){25 typeMatches = 'image';26 callbacks.image(27 imageMatches[1], null, 28 {title: titleMatches[1], type: typeMatches, url: urlMatches[1], image: imageMatches[1]}29 );30 }31 else callbacks.noMedia(url, null);32 } else {33 if (typeMatches[1].indexOf('product') > -1){34 typeMatches[1] = 'product';35 callbacks.product(36 urlMatches[1], null, 37 {title: titleMatches[1], type: typeMatches[1], url: urlMatches[1], image: imageMatches[1]}38 );39 } else if (typeMatches[1].indexOf('image') > -1){40 typeMatches[1] = 'image';41 callbacks.image(42 imageMatches[1], null, 43 {title: titleMatches[1], type: typeMatches[1], url: urlMatches[1], image: imageMatches[1]}44 );45 } else if (typeMatches[1].indexOf('item') > -1){46 typeMatches[1] = 'product';47 callbacks.product(48 urlMatches[1], null, 49 {title: titleMatches[1], type: typeMatches[1], url: urlMatches[1], image: imageMatches[1]}50 );51 } else {52 callbacks.noMedia(url, null);53 }54 }55 } catch (err) {56 callbacks.noMedia(url, null);57 }58 });59 return true;...
og.js
Source:og.js
1import logger from '../utils/logger';2import { ReadPageURL } from './feed.js';3const invalidExtensions = ['mp3', 'mp4', 'mov', 'm4a', 'mpeg'];4// determines if the given feedUrl is a podcast or not5export async function ParseOG(pageURL) {6 const pageStream = await ReadPageURL(pageURL);7 return ParseOGStream(pageStream, pageURL);8}9export function IsValidOGUrl(url) {10 if (!url) {11 return false;12 }13 const invalid = invalidExtensions.some((extension) => {14 if (url.endsWith(`.${extension}`)) {15 return extension;16 }17 });18 if (invalid) {19 logger.warn(`Invalid file extension for url ${url}`);20 return false;21 }22 return true;23}24function parseImage(html) {25 const metaTagRe = /(<meta[^>]*?og:image[^>]*?>)/gm;26 const urlRe = /content="(.*?)"/gm;27 if (!html.includes('og:image')) {28 return {};29 }30 const matches = metaTagRe.exec(html);31 if (!matches) {32 return {};33 }34 const meta = matches[1];35 const urlMatches = urlRe.exec(meta);36 if (urlMatches) {37 return { image: urlMatches[1] };38 }39}40function parseCanonicalUrl(html) {41 if (html.includes('og:image')) {42 const metaTagRe = /(<meta[^>]*?og:url[^>]*?>)/gm;43 const matches = metaTagRe.exec(html);44 if (matches) {45 const meta = matches[1];46 const urlRe = /content="(.*?)"/gm;47 const urlMatches = urlRe.exec(meta);48 if (urlMatches) {49 return { canonicalUrl: urlMatches[1] };50 }51 }52 } else if (html.includes('"canonical"')) {53 const linkTagRe = /(<link[^>]*?rel\s*=\s*"canonical"[^>]*?>)/gm;54 const matches = linkTagRe.exec(html);55 if (matches) {56 const meta = matches[1];57 const urlRe = /href="(.*?)"/gm;58 const urlMatches = urlRe.exec(meta);59 if (urlMatches) {60 return { canonicalUrl: urlMatches[1] };61 }62 }63 }64 return {};65}66export function ParseOGStream(pageStream, pageURL) {67 let result = {};68 return new Promise((resolve, reject) => {69 pageStream70 .on('data', (data) => {71 const html = data.toString('utf8');72 for (const extractor of [parseImage, parseCanonicalUrl]) {73 result = Object.assign(result, extractor(html));74 }75 if (result.image && result.canonicalUrl) {76 pageStream.destroy();77 resolve(result);78 }79 })80 .on('error', reject)81 .on('end', () => resolve(result));82 });...
urlMatches.js
Source:urlMatches.js
...3const AccessList = require('../../models/AccessList');4const accessList = new AccessList();5describe('urlMatches', () => { // eslint-disable-line no-undef, max-lines-per-function6 it('has :', (done) => { // eslint-disable-line no-undef7 assert.isTrue(accessList.urlMatches('/places/:id/metrics', '/places/1/metrics'));8 assert.isTrue(accessList.urlMatches('/places/:id/metrics', '/places/2/metrics'));9 assert.isFalse(accessList.urlMatches('/places/:id/metrics', '/place/3'));10 done();11 });12 it('has *', (done) => { // eslint-disable-line no-undef13 assert.isTrue(accessList.urlMatches('/places/*', '/places/some/metrics'));14 assert.isTrue(accessList.urlMatches('/places/*', '/places/3'));15 assert.isFalse(accessList.urlMatches('/places/*', '/place/3'));16 assert.isTrue(accessList.urlMatches('/*', '/place/3'));17 done();18 });19 it('has no * nor :', (done) => { // eslint-disable-line no-undef20 assert.isTrue(accessList.urlMatches('/places/some/metrics', '/places/some/metrics'));21 assert.isFalse(accessList.urlMatches('/places/some/metrics', '/places/other/metrics'));22 done();23 });24 it('comparison is undefined', (done) => { // eslint-disable-line no-undef25 assert.isFalse(accessList.urlMatches(null, '/place/3'));26 done();27 });28 it('url is undefined', (done) => { // eslint-disable-line no-undef29 assert.isFalse(accessList.urlMatches('/*'));30 done();31 });...
helper.js
Source:helper.js
1import s from 'underscore.string';2export const checkHighlightedWordsInUrls = (msg, highlight) => {3 const urlRegex = new RegExp(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)(${ s.escapeRegExp(highlight) })\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)`, 'gmi');4 const urlMatches = msg.match(urlRegex);5 return urlMatches;6};7export const removeHighlightedUrls = (msg, highlight, urlMatches) => {8 urlMatches.forEach((match) => {9 const highlightRegex = new RegExp(highlight, 'gmi');10 const withTemplate = match.replace(highlightRegex, `<span class="highlight-text">${ highlight }</span>`);11 const regexWithTemplate = new RegExp(withTemplate, 'i');12 msg = msg.replace(regexWithTemplate, match);13 });14 return msg;15};16export const highlightWords = (msg, to_highlight) => {17 const highlightTemplate = '$1<span class="highlight-text">$2</span>$3';18 if (Array.isArray(to_highlight)) {19 to_highlight.forEach((highlight) => {20 if (!s.isBlank(highlight)) {21 const regex = new RegExp(`(^|\\b|[\\s\\n\\r\\t.,Ø'\\\"\\+!?:-])(${ s.escapeRegExp(highlight) })($|\\b|[\\s\\n\\r\\t.,Ø'\\\"\\+!?:-])(?![^<]*>|[^<>]*<\\/)`, 'gmi');22 const urlMatches = checkHighlightedWordsInUrls(msg, highlight);23 msg = msg.replace(regex, highlightTemplate);24 if (urlMatches) {25 msg = removeHighlightedUrls(msg, highlight, urlMatches);26 }27 }28 });29 }30 return msg;...
copy-chrome-url-and-title.js
Source:copy-chrome-url-and-title.js
1title = document.title;2url = document.URL;3markdownLabel = title;4markdownUrl = url;5if (url.includes('git.hubteam.com')) {6 if (url.includes('/pull/')) {7 const urlMatches = /.*pull\/(\d+).*/i.exec(document.URL);8 if (urlMatches) {9 const pullRequestId = urlMatches[1];10 const repo = /.*Pull Request #\d+ · (.*)/.exec(document.title)[1];11 markdownLabel = title.replace(12 ` · Pull Request #${pullRequestId} · ${repo}`,13 '',14 );15 }16 } else if (url.includes('/issues/')) {17 const urlMatches = /.*issues\/(\d+).*/i.exec(document.URL);18 if (urlMatches) {19 const issueId = urlMatches[1];20 const repo = /.*Issue #\d+ · (.*)/.exec(document.title)[1];21 markdownLabel = title.replace(` · Issue #${issueId} · ${repo}`, '');22 }23 }24}25markdownLabel = markdownLabel.replace('[', '(').replace(']', ')');...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.waitForEvent('response', (response) => {7 return response.url().includes('gstatic.com');8 });9 await page.close();10 await context.close();11 await browser.close();12})();
Using AI Code Generation
1const { urlMatches } = require('@playwright/test');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await page.screenshot({ path: 'example.png' });8 await browser.close();9})();10const { test, expect } = require('@playwright/test');11test('test', async ({ page }) => {12});13 const { urlMatches } = require('@playwright/test');14 SyntaxError: Cannot use import statement outside a module15 at wrapSafe (internal/modules/cjs/loader.js:1050:16)16 at Module._compile (internal/modules/cjs/loader.js:1098:27)17 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1154:10)18 at Module.load (internal/modules/cjs/loader.js:985:32)19 at Function.Module._load (internal/modules/cjs/loader.js:878:14)20 at Module.require (internal/modules/cjs/loader.js:1023:19)21 at require (internal/modules/cjs/helpers.js:72:18)22 at Object.<anonymous> (/home/runner/work/playwright-test/playwright-test/test.js:1:1)23 at Module._compile (internal/modules/cjs/loader.js:1137:30)24 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1154:10)25 at Module.load (internal/modules/cjs/loader.js:985:32)26 at Function.Module._load (internal/modules/cjs/loader.js:878:14)27 at Module.require (internal/modules/cjs/loader.js:1023:19)28 at require (internal/modules/cjs/helpers.js:72:18)29 at Object.<anonymous> (/home/runner/work
Using AI Code Generation
1const { urlMatches } = require('playwright/lib/utils/utils');2const pptr = require('playwright');3(async () => {4 const browser = await pptr.chromium.launch();5 const page = await browser.newPage();6 await page.screenshot({path: 'google.png'});7 await browser.close();8})();9const { urlMatches } = require('playwright/lib/utils/utils');10const { urlMatches } = require('playwright/lib/utils/utils');11const { urlMatches } = require('playwright/lib/utils/utils');12const { urlMatches } = require('playwright/lib/utils/utils');13const { urlMatches } = require('playwright/lib/utils/utils');14const { urlMatches } = require('playwright/lib/utils/utils');15const { urlMatches } = require('playwright/lib/utils/utils');16const { urlMatches } = require('playwright/lib/utils/utils');17const { urlMatches } = require('playwright/lib/utils/utils');18const { urlMatches } = require('playwright/lib/utils/utils');
Using AI Code Generation
1const { urlMatches } = require('playwright/lib/utils/utils');2const pptr = require('playwright');3(async () => {4 const browser = await pptr.chromium.launch();5 const page = await browser.newPage();6 await page.screenshot({path: 'google.png'});7 await browser.close();8})();9utils/utils');10const match = ulMatches(url, urlPattern);11console.log(match);12const { urlMatches } = require(playwright/lib/utils/utils'13I hope this helps.example.com';14const match = urlMatches(url, urlPattern);15console.log(match);16const { urlMatches } = require('playright/lib/utils/utils');17const match = urlMatches(url, urlPattern);18console.log(match);19const { urlMatches } = require('playright/lib/utils/utils');20const match = urMatchs(url, urlPattern);21consolelog(math);22const { urlMatches } = require('playwright/lib/utils/utils)23const urlPexample.com/';24const match = urlMatches(url, urlPattern);25consolelog(match);26const { urlMatches } = rquire('playwright/lib/utils/utils');27const match = urdMatchss(url, urlPattern);28console,match);29const { } = require'playwright/lib/utils/tils');30const match = urlMatches(url urlPattern);31console.log(match);32const {urlMatches } = require('playwright/lib/utils/utils');33const match = urlMatches(url, urlP;34console.log(match35const { urlMatches } = require('play
Using AI Code Generation
1const { urlMatches } = require('playwrightlibhelper');2console.log(urlMatches(url, pattern));3const { urlMatches } = require('playwright/lib/utils/utils');4const { urlMatches } = require('playwright/lib/utils/utils');5const { urlMatches } = require('playwright/lib/utils/utils');6const { urlMatches } = require('playwright/lib/utils/utils');7const { urlMatches } = require('playwright/lib/utils/utils');8const { urlMatches } = require('playwright/lib/utils/utils');9const { urlMatches } = require('playwright/lib/utils/utils');10const { urlMatches } = require('playwright/lib/utils/utils');11const { urlMatches } = require('playwright/lib/utils/utils');12const { urlMatches } = require('playwright/lib/utils/utils');
Using AI Code Generation
1const { urlMatches } = require('playwright/lib/utils/utils');2const pattern = 'google.com';3const result = urlMatches(url, pattern);4console.log(result);5const { urlMatches } = require('playwright/lib/utils/utils');6const pattern = 'google.com';7const result = urlMatches(url, pattern);8console.log(result);9const { urlMatches } = require('playwright/lib/utils/utils');10const pattern = 'google.com';11const result = urlMatches(url, pattern);12console.log(result);13const { urlMatches } = require('playwright/lib/utils/utils');14const pattern = 'google.com';15const result = urlMatches(url, pattern);16console.log(result);17const { urlMatches } = require('playwright/lib/utils/utils');18const pattern = 'google.com';19const result = urlMatches(url, pattern);20console.log(result);21const { urlMatches } = require('playwright/lib/utils/utils');22const pattern = 'google.com';23const result = urlMatches(url, pattern);24console.log(result);25const { urlMatches } = require('playwright/lib/utils/utils');26const pattern = 'google.com';27const result = urlMatches(url, pattern);28console.log(result);29const { urlMatches } = require('playwright/lib/utils/utils');30const pattern = 'google.com';31const result = urlMatches(url, pattern);32console.log(result);
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 for (const browserType of BROWSER) {4 const browser = await playwright[browserType].launch({headless: false});5 const context = await browser.newContext();6 const page = await context.newPage();7 await page.waitForTimeout(2000);8 await page.screenshot({path: `example-${browserType}.png`});9 await browser.close();10 }11})();12const playwright = require('playwright');13(async () => {14 for (const browserType of BROWSER) {15 const browser = await playwright[browserType].launch({headless: false});16 const context = await browser.newContext();17 const page = await context.newPage();18 await page.waitForTimeout(2000);19 await page.screenshot({path: `example-${browserType}.png`});20 await browser.close();21 }22})();23const playwright = require('playwright');24(async () => {25 for (const browserType of BROWSER) {26 const browser = await playwright[browserType].launch({headless: false});27 const context = await browser.newContext();28 const page = await context.newPage();29 await page.waitForTimeout(2000);30 await page.screenshot({path: `example-${browserType}.png`});31 await browser.close();32 }33})();
Using AI Code Generation
1const { urlMatches } = require('playwright/lib/utils/utils');2const pattern = 'google.com';3const result = urlMatches(url, pattern);4console.log(result);5const { urlMatches } = require('playwright/lib/utils/utils');6const pattern = 'google.com';7const result = urlMatches(url, pattern);8console.log(result);9const { urlMatches } = require('playwright/lib/utils/utils');10const pattern = 'google.com';11const result = urlMatches(url, pattern);12console.log(result);13const { urlMatches } = require('playwright/lib/utils/utils');14const pattern = 'google.com';15const result = urlMatches(url, pattern);16console.log(result);17const { urlMatches } = require('playwright/lib/utils/utils');18const pattern = 'google.com';19const result = urlMatches(url, pattern);20console.log(result);21const { urlMatches } = require('playwright/lib/utils/utils');22const pattern = 'google.com';23const result = urlMatches(url, pattern);24console.log(result);25const { urlMatches } = require('playwright/lib/utils/utils');26const pattern = 'google.com';27const result = urlMatches(url, pattern);28console.log(result);29const { urlMatches } = require('playwright/lib/utils/utils');30const pattern = 'google.com';31const result = urlMatches(url, pattern);32console.log(result);
Using AI Code Generation
1const { urlMatches } = require('playwright/lib/helper');2console.log(urlMatches(url, pattern));3const { urlMatches } = require('playwright/lib/helper');4console.log(urlMatches(url, pattern));5const { urlMatches } = require('playwright/lib/helper');6console.log(urlMatches(url, pattern));7const { urlMatches } = require('playwright/lib/helper');8console.log(urlMatches(url, pattern));9const { urlMatches } = require('playwright/lib/helper');10console.log(urlMatches(url, pattern));11const { urlMatches } = require('playwright/lib/helper');12console.log(urlMatches(url, pattern));13const { urlMatches } = require('playwright/lib/helper');14console.log(urlMatches(url, pattern));15const { urlMatches } = require('playwright/lib/helper');16console.log(urlMatches(url, pattern));
Using AI Code Generation
1import { urlMatches } from 'playwright/lib/utils/utils';2const isGoogle = urlMatches(url, 'google.com');3console.log(isGoogle);4import { urlMatches } from 'playwright/lib/utils/utils';5const isGoogle = urlMatches(url, 'google.com', {host: true});6console.log(isGoogle);7import { urlMatches } from 'playwright/lib/utils/utils';8const isGoogle = urlMatches(url, 'google.com', {host: false});9console.log(isGoogle);10import { urlMatches } from 'playwright/lib/utils/utils';11const isGoogle = urlMatches(url, 'google.com', {host: true, path: true});12console.log(isGoogle);13import { urlMatches } from 'playwright/lib/utils/utils';14const isGoogle = urlMatches(url, 'google.com', {host: true, path: true, query: true});15console.log(isGoogle);16import { urlMatches } from 'playwright/lib/utils/utils';17const isGoogle = urlMatches(url, 'google.com', {host: true, path: true, query: true, fragment: true});18console.log(isGoogle);19import { urlMatches } from 'playwright/lib/utils/utils';20const isGoogle = urlMatches(url, 'google.com', {host: true, path: true, query: true, fragment: true
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!