Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

remove mutation observers from brave content scripts #3773

Merged
merged 1 commit into from
Sep 7, 2016
Merged
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
40 changes: 17 additions & 23 deletions app/extensions/brave/content/scripts/flashListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ if (chrome.contentSettings.flashActive != 'allow' &&
}
})
}
setTimeout(() => {
var observer = new window.MutationObserver(function (mutations) {
replaceAdobeLinks()
})
replaceAdobeLinks()
observer.observe(document.documentElement, {
childList: true,
subtree: true
})
}, 1000)
replaceAdobeLinks()
let interval = setInterval(replaceAdobeLinks, 1000)
document.addEventListener('visibilitychange', () => {
clearInterval(interval)
if (document.visibilityState !== 'hidden') {
interval = setInterval(replaceAdobeLinks, 1000)
}
})
})()
}

Expand Down Expand Up @@ -131,7 +129,7 @@ function getFlashObjects (elem) {
* Inserts Flash placeholders.
* @param {Element} elem - HTML element to search
*/
function insertFlashPlaceholders (elem) {
function insertFlashPlaceholders (elem = document.documentElement) {
const minWidth = 200
const minHeight = 100
let flashObjects = getFlashObjects(elem)
Expand Down Expand Up @@ -161,16 +159,12 @@ function insertFlashPlaceholders (elem) {

if (chrome.contentSettings.flashActive != 'allow' ||
chrome.contentSettings.flashEnabled != 'allow') {
insertFlashPlaceholders(document.documentElement)
setTimeout(() => {
var observer = new window.MutationObserver(function (mutations) {
insertFlashPlaceholders(document.documentElement)
})

insertFlashPlaceholders(document.documentElement)
observer.observe(document.documentElement, {
childList: true,
subtree: true
})
}, 1000)
insertFlashPlaceholders()
let interval = setInterval(insertFlashPlaceholders, 1000)
document.addEventListener('visibilitychange', () => {
clearInterval(interval)
if (document.visibilityState !== 'hidden') {
interval = setInterval(insertFlashPlaceholders, 1000)
}
})
}
43 changes: 14 additions & 29 deletions app/extensions/brave/content/scripts/passwordManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

if (chrome.contentSettings.passwordManager == 'allow') {

let credentials = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryAutofillForm no longer needs credentials as an argument

function savePassword(username/*: ?string*/, pw/*: string*/, origin/*: string*/, action/*: string*/) {
chrome.ipc.send('save-password', username, pw, origin, action)
}
Expand Down Expand Up @@ -45,11 +45,10 @@ if (chrome.contentSettings.passwordManager == 'allow') {
}

if (credentials[action]) {
credentials[action].push([passwordElem, usernameElem])
} else {
credentials[action] = [[passwordElem, usernameElem]]
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are mutiple forms on a page with the same action, all of them should be autofilled. i believe this change causes only the first to be autofilled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just made a test case for this so i can fix

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I assumed each action had a single username/password element. The purpose of this change was to avoid adding another listener on each check so just watch out for that with the fix

}

credentials[action] = [[passwordElem, usernameElem]]
// Fill the password immediately if there's only one or if the username
// is already autofilled
chrome.ipc.send('get-passwords', formOrigin, action)
Expand Down Expand Up @@ -111,17 +110,15 @@ if (chrome.contentSettings.passwordManager == 'allow') {
function autofillPasswordListener () {
// Don't autofill on non-HTTP(S) sites for now
if (document.location.protocol !== 'http:' && document.location.protocol !== 'https:') {
return false
return
}

if (document.querySelectorAll('input[type=password]').length === 0) {
// No password fields; abort
return false
// No password fields;
return
}

// Map of action origin to [[password element, username element]]
var credentials = {}

var formOrigin = [document.location.protocol, document.location.host].join('//')
var formNodes = document.querySelectorAll('form')

Expand All @@ -148,7 +145,6 @@ if (chrome.contentSettings.passwordManager == 'allow') {
})
}
})
return true
}

/**
Expand Down Expand Up @@ -262,24 +258,13 @@ if (chrome.contentSettings.passwordManager == 'allow') {
return passwordNodes
}


if (autofillPasswordListener() !== true) {
setTimeout(() => {
// Some pages insert the password form into the DOM after it's loaded
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
if (autofillPasswordListener() === true) {
observer.disconnect()
}
}
})
})
observer.observe(document.documentElement, {
childList: true,
subtree: true
})
}, 1000)
}
autofillPasswordListener()
let interval = setInterval(autofillPasswordListener, 1000)
document.addEventListener('visibilitychange', () => {
clearInterval(interval)
if (document.visibilityState !== 'hidden') {
interval = setInterval(autofillPasswordListener, 1000)
}
})
}