-
Notifications
You must be signed in to change notification settings - Fork 1.1k
redirecting AMP to canonical pages #4950
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
components/brave_extension/extension/brave_extension/background/api/ampAPI.ts
This file contains hidden or 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,16 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| * You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import * as SettingsPrivate from '../../../../../common/settingsPrivate' | ||
|
|
||
| const ampSettingsKey = 'brave.shields.amp_neutralized' | ||
|
|
||
| export async function getAMPNeutralized (): Promise<boolean> { | ||
| const pref = await SettingsPrivate.getPreference(ampSettingsKey) | ||
| if (pref.type !== chrome.settingsPrivate.PrefType.BOOLEAN) { | ||
| throw new Error(`Unexpected settings type received for "${ampSettingsKey}.` + | ||
| `" Expected: ${chrome.settingsPrivate.PrefType.BOOLEAN}, Received: ${pref.type}`) | ||
| } | ||
| return pref.value | ||
| } |
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
components/brave_extension/extension/brave_extension/background/reducers/ampReducer.ts
This file contains hidden or 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,29 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| * You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import * as webNavigationTypes from '../../constants/webNavigationTypes' | ||
| import { Actions } from '../../types/actions/index' | ||
| import * as ampAPI from '../api/ampAPI' | ||
|
|
||
| export default function ampReducer (state = {}, action: Actions) { | ||
| switch (action.type) { | ||
| case webNavigationTypes.ON_COMMITTED: { | ||
| if (action.isMainFrame) { | ||
| ampAPI.getAMPNeutralized().then(ampNeutralized => { | ||
| if (ampNeutralized) { | ||
| chrome.tabs.executeScript(action.tabId, { | ||
| file: 'out/content_noamp.bundle.js', | ||
| allFrames: false, | ||
| runAt: 'document_start', | ||
| frameId: 0 | ||
| }) | ||
| } | ||
| }) | ||
| .catch(() => console.warn('Failed to read AMP neutralizer preference')) | ||
| } | ||
| break | ||
| } | ||
| } | ||
| return state | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
components/brave_extension/extension/brave_extension/content_noamp.ts
This file contains hidden or 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,19 @@ | ||
| const redirectAmp = () => { | ||
| // Check whether it's AMP html | ||
| let h = document.getElementsByTagName('html') | ||
| if (h[0].getAttribute('amp') !== null | ||
| || h[0].getAttribute('⚡') !== null | ||
| || document.location.href.substr(0, 29).toLowerCase() === 'https://www.google.com/amp/s/') { | ||
|
|
||
| // Try and find a canonical link | ||
| let canonicals = document.querySelectorAll('link[rel=canonical]') | ||
| for (let i = 0; i < canonicals.length; i++) { | ||
| let canonicalURL = canonicals[i].getAttribute('href') | ||
| if (canonicalURL !== null && canonicalURL !== window.location.href.split('#')[0]) { | ||
| window.location.replace(canonicalURL) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', redirectAmp, false) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can do the redirect in the native code, we really want to avoid using extensions for stuff like this. Check out brave_common_static_redirect_network_delegate_helper
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so actually in moving this to greaselion we will just conditionally enable this content script when the pref values changes so this won't be needed at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same with this https://github.com/brave/brave-core/pull/4950/files#diff-e5fa22746ae0f1e347e9e93cd7113627R9