-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(legacy-javascript): use third-party-web for scoring (#10849)
- Loading branch information
1 parent
210ff35
commit 03f00c7
Showing
5 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const thirdPartyWeb = require('third-party-web/httparchive-nostats-subset'); | ||
|
||
/** @typedef {import("third-party-web").IEntity} ThirdPartyEntity */ | ||
|
||
/** | ||
* `third-party-web` throws when the passed in string doesn't appear to have any domain whatsoever. | ||
* We pass in some not-so-url-like things, so make the dependent-code simpler by making this call safe. | ||
* @param {string} url | ||
* @return {ThirdPartyEntity|undefined} | ||
*/ | ||
function getEntity(url) { | ||
try { | ||
return thirdPartyWeb.getEntity(url); | ||
} catch (_) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} url | ||
* @param {ThirdPartyEntity | undefined} mainDocumentEntity | ||
*/ | ||
function isThirdParty(url, mainDocumentEntity) { | ||
const entity = getEntity(url); | ||
if (!entity) return false; | ||
if (entity === mainDocumentEntity) return false; | ||
return true; | ||
} | ||
|
||
/** | ||
* @param {string} url | ||
* @param {ThirdPartyEntity | undefined} mainDocumentEntity | ||
*/ | ||
function isFirstParty(url, mainDocumentEntity) { | ||
return !isThirdParty(url, mainDocumentEntity); | ||
} | ||
|
||
module.exports = { | ||
getEntity, | ||
isThirdParty, | ||
isFirstParty, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env jest */ | ||
|
||
const thirdPartyWeb = require('../../lib/third-party-web.js'); | ||
|
||
describe('third party web', () => { | ||
it('basic', () => { | ||
expect(thirdPartyWeb.isThirdParty('https://www.example.com', undefined)).toBe(false); | ||
expect(thirdPartyWeb.isFirstParty('https://www.example.com', undefined)).toBe(true); | ||
|
||
expect(thirdPartyWeb.isThirdParty('https://www.googletagmanager.com', undefined)).toBe(true); | ||
expect(thirdPartyWeb.isFirstParty('https://www.googletagmanager.com', undefined)).toBe(false); | ||
}); | ||
|
||
it('not third party if main document is same entity', () => { | ||
const mainDocumentEntity = thirdPartyWeb.getEntity('https://www.googletagmanager.com'); | ||
expect(thirdPartyWeb.isThirdParty('https://www.googletagmanager.com/a.js', mainDocumentEntity)).toBe(false); | ||
expect(thirdPartyWeb.isThirdParty('https://blah.atdmt.com', mainDocumentEntity)).toBe(true); | ||
expect(thirdPartyWeb.isThirdParty('https://www.example.com', mainDocumentEntity)).toBe(false); | ||
}); | ||
}); |