Skip to content
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

Fix connection removal tool #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
96 changes: 49 additions & 47 deletions Leave Me Alone/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const wait = time => new Promise(resolve => setTimeout(resolve, 1000*time));

function range(start, stop, step) {
if (typeof stop == 'undefined') {
stop = start;
start = 0;
stop = start;
start = 0;
}

if (typeof step == 'undefined') {
step = 1;
step = 1;
}

if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
return [];
}

var result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
result.push(i);
}

return result;
Expand Down Expand Up @@ -67,7 +67,7 @@ const findAllCards = () => {


const selectCardOrNot = (card, headlines) => {
let position = card.querySelector('.invitation-card__occupation');
let position = card.querySelector('.invitation-card__subtitle');
position = position.innerText;
let checkBox = card.querySelector('input[type="checkbox"]');
let checkBool = false;
Expand Down Expand Up @@ -103,8 +103,8 @@ const extractInfos = connection => {
headline = headline.innerText;
let name = connection.querySelector('.mn-connection-card__name');
name = name.innerText;
let profileImage = connection.querySelector('div.presence-entity__image');
profileImage = profileImage.style.backgroundImage.slice(5, -2);
let profileImage = connection.querySelector('img.presence-entity__image');
profileImage = profileImage.src;
return { headline, name, profileImage }
}

Expand All @@ -129,15 +129,15 @@ function deleteConnection (connection) {

let trigger = connection.querySelector('button[data-control-name=ellipsis]');
trigger.click();
let removeButton = connection.querySelector("button[data-control-name=remove]");
let removeButton = connection.querySelector(".js-mn-connection-card__dropdown-delete-btn button");

let options = {
fireOnAttributesModification: false,
onceOnly: true,
existing: false,
};
let modalContainer = document.querySelector('div#li-modal-container');
modalContainer.arrive('div.modal-content-wrapper', options, handleConnection);
let modalContainer = document.querySelector('#artdeco-modal-outlet');
modalContainer.arrive('div.mn-delete-connection-modal', options, handleConnection);

removeButton.click();
});
Expand All @@ -159,31 +159,31 @@ function removeConnectionOrNot (connection, headlinesRegex) {

const scrollToFindConnections = (currentConnections, scrollTop) => {
return Promise.resolve()
.then(() => {
let niterArray = range(window.pageYOffset, scrollTop, window.innerHeight);
return niterArray.reduce(
(promise_chain, value, index) =>
promise_chain
.then(() => {
window.scrollTo(0, value);
return Promise.resolve(value)
})
.then(() => wait(2)),
Promise.resolve()
);
})
.then(() => wait(2))
.then(() => {
let connections = findAllConnections();
if (connections.length == currentConnections) {
return Promise.reject(connections)
}
let position = connections[connections.length - 1].getBoundingClientRect();
return Promise.resolve({
'length': connections.length,
'top': position.top + window.pageYOffset})
})
.then(info => scrollToFindConnections(info.length, info.top))
.then(() => {
let niterArray = range(window.pageYOffset, scrollTop, window.innerHeight);
return niterArray.reduce(
(promise_chain, value, index) =>
promise_chain
.then(() => {
window.scrollTo(0, value);
return Promise.resolve(value)
})
.then(() => wait(2)),
Promise.resolve()
);
})
.then(() => wait(2))
.then(() => {
let connections = findAllConnections();
if (connections.length == currentConnections) {
return Promise.reject(connections)
}
let position = connections[connections.length - 1].getBoundingClientRect();
return Promise.resolve({
'length': connections.length,
'top': position.top + window.pageYOffset})
})
.then(info => scrollToFindConnections(info.length, info.top))
}


Expand All @@ -208,7 +208,7 @@ const displayModal = (connectionsFiltered, headlinesRegex) => {

let spanContent = document.createElement('span');
spanContent.style = 'font-size:90%'
spanContent.innerText = 'The following connections will be removed:'
spanContent.innerText = 'The following ' + connectionsFiltered.length + ' connections will be removed:'
modalContent.appendChild(spanContent);

let containerList = document.createElement('div');
Expand Down Expand Up @@ -245,8 +245,8 @@ const displayModal = (connectionsFiltered, headlinesRegex) => {
let actions = connectionsFiltered.reduce(
(promise_chain, connection) =>
promise_chain
.then(() => wait(1))
.then(() => deleteConnection(connection)),
.then(() => wait(1))
.then(() => deleteConnection(connection)),
Promise.resolve()
).then(() => document.querySelector('.comet-modal').remove());
});
Expand All @@ -270,13 +270,15 @@ const cleanExistingConnections = () => {
let headlinesRegex = constructHeadlinesRegex(headlines);
let connectionsDone = 0;
scrollToFindConnections(0, 0)
.catch(connections => {
let connectionsFiltered = connections
.filter(connection => removeConnectionOrNot(connection, headlinesRegex))
return Promise.resolve(connectionsFiltered)
})
.then(connectionsFiltered => displayModal(connectionsFiltered, headlinesRegex))
.then(connectionsFiltered => {console.log('connexions filtrées', connectionsFiltered)})
.catch(connections => {
let connectionsFiltered = connections
.filter(connection => removeConnectionOrNot(connection, headlinesRegex))
return Promise.resolve(connectionsFiltered)
})
.then(connectionsFiltered => {
displayModal(connectionsFiltered, headlinesRegex) ;
return connectionsFiltered}
).then(connectionsFiltered => {console.log('connexions filtrées', connectionsFiltered)})
}
});
}
Expand All @@ -291,4 +293,4 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'start-cleaning-existing') {
cleanExistingConnections();
}
});
});