Skip to content

Commit

Permalink
Merge pull request opt-out-tools#75 from opt-out-tools/ribicn-softerr…
Browse files Browse the repository at this point in the history
…ific-update_to_tests_and_code

Taking out selenium tests so we can merge the rest of the PR
  • Loading branch information
malteserteresa authored Dec 9, 2019
2 parents 79bbe78 + a0d5f18 commit 60908ee
Show file tree
Hide file tree
Showing 21 changed files with 1,040 additions and 192 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ env:
webextensions: true
jest: true
extends:
- airbnb-base
- plugin:jest/recommended
plugins:
- jest
globals:
Atomics: readonly
SharedArrayBuffer: readonly
browser: readonly
parserOptions:
ecmaVersion: 2018
sourceType: module
rules: {}
rules: {
no-console: off
}

116 changes: 72 additions & 44 deletions extension/content/opt-out-ext.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
let selector;
let option = 'text_crossed';
let slider = '1';

function updateOption(result) {
option = result.optOut.selector;
slider = result.optOut.slider;
}

function onError(error) {
console.log(`Error: ${error}`);
}
browser.storage.sync.get('optOut').then(updateOption, onError);

const bodyColor = window.getComputedStyle(document.body, null).getPropertyValue('background-color');
document.documentElement.style
.setProperty('--color', bodyColor);

const root = document.getElementById('doc') || document.getElementById('react-root');

Expand All @@ -22,31 +14,56 @@ if (document.querySelector('body').classList.contains('logged-out')) {
console.log('online');
selector = '[data-testid="tweet"]';
}
/**
* @description Updates `option` and `slider` depending on the given result
* @param result
*/
const updateOption = (result) => {
option = result.optOut.selector;
slider = result.optOut.slider;
};

/**
* @description Function handles errors.
* @param error
*/
const onError = (error) => {
console.error(`Error: ${error}`);
};

/*
Depending on `option` sets classes to tweet nodes
/**
* @description Depending on `option` sets classes to tweet nodes
*/
const styleTweet = function (element, selectedOption, sliderValue) {
if ((selectedOption === 'text_white') && (sliderValue === '1')) element.classList.add('opt-out-tw');
else element.classList.remove('opt-out-tw');
if ((selectedOption === 'text_crossed') && (sliderValue === '1')) element.classList.add('opt-out-tc');
else element.classList.remove('opt-out-tc');
if ((selectedOption === 'text_removed') && (sliderValue === '1')) element.classList.add('opt-out-trem');
else element.classList.remove('opt-out-trem');
const styleTweet = (element, selectedOption, sliderValue) => {
element.classList.remove('opt-out-tw', 'opt-out-tc', 'opt-out-trem');
if (sliderValue === '1') {
switch (selectedOption) {
case 'text_white':
element.classList.add('opt-out-tw');
break;
case 'text_crossed':
element.classList.add('opt-out-tc');
break;
case 'text_removed':
element.classList.add('opt-out-trem');
break;
}
}
};

/*
function which calls server for given node, and depending on the response,
applies pre-defined action
/**
* @description function which calls server for given node, and depending on the response,
* applies pre-defined action
* @param node
*/
const checkText = function (node) {
const checkText = (node) => {
console.log('Sending Request');
const link = 'https://api.optoutools.com/predict';
const xhr = new XMLHttpRequest();
xhr.open('POST', link, true);
xhr.setRequestHeader('Content-type', 'application/json;charset=UTF-8');
xhr.withCredentials = true;
xhr.onreadystatechange = function (e) {
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return;
}
Expand Down Expand Up @@ -75,25 +92,10 @@ const checkText = function (node) {
}),
);
};

/*
* Predefines action and changes it depending on user action
/**
* @description get every tweet and process unprocessed ones.
*/
browser.runtime.onMessage.addListener((message) => {
if ((option !== message.selector) || (slider !== message.slider)) {
option = message.selector;
slider = message.slider;
const posts = document.querySelectorAll('.processed-true');
posts.forEach((post) => {
const tweetText = post.querySelector(
`${selector} > div ~ div > div ~ div`,
); // selecting text inside tweet
styleTweet(tweetText, option, slider);
});
}
});

const processTweets = function () {
const processTweets = () => {
const posts = document.querySelectorAll(selector); // selecting tweet object
posts.forEach((post) => {
if (post.classList.contains('processed-true')) return;
Expand All @@ -102,13 +104,39 @@ const processTweets = function () {
});
};

const checkTweetList = function (mutationsList) {
/**
* @description for every change in DOM run processTweets
* @param mutationsList
*/
const checkTweetList = (mutationsList) => {
mutationsList.forEach((mutation) => {
if (mutation.type === 'childList') {
processTweets();
}
});
};


const checkTweetListObserver = new MutationObserver(checkTweetList);

// MAIN FUNCTION

browser.storage.sync.get('optOut').then(updateOption, onError);
/**
* Adds listener which on new message received from popup goes over tweets and applies new style
*/
browser.runtime.onMessage.addListener((message) => {
if ((option !== message.selector) || (slider !== message.slider)) {
option = message.selector;
slider = message.slider;
const posts = document.querySelectorAll('.processed-true');
posts.forEach((post) => {
const tweetText = post.querySelector(
`${selector} > div ~ div > div ~ div`,
); // selecting text inside tweet
styleTweet(tweetText, option, slider);
});
}
});

checkTweetListObserver.observe(root, { childList: true, subtree: true });
8 changes: 7 additions & 1 deletion extension/content/opt-out.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
:root {
--color: rgb(255, 255, 255);
}
.opt-out-tw {
color: white;
color: var(--color, white);
}
.opt-out-tw a {
color: var(--color, white);
}

.opt-out-tc {
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"browser_action": {
"default_icon": "icons/icon-48.png",
"default_title": "Opt-Out-Ext",
"default_popup": "popup/set_view_mod.html"
"default_popup": "popup/popup.html"
},
"icons": {
"48": "icons/icon-48.png",
Expand Down
File renamed without changes
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ h2 {

#slider {
width: 100%;
background: #d3d3d3;
background: #d3d3d3;
height: 10px;
border-radius: 5px;
margin-bottom: 24px;
}

#slider::-moz-range-thumb {
width: 50px;
width: 50px;
height: 50px;
cursor: pointer;
border: none;
Expand All @@ -37,7 +37,7 @@ h2 {
}

#slider::-webkit-slider-thumb {
width: 50px;
width: 50px;
height: 50px;
cursor: pointer;
border: none;
Expand Down
10 changes: 10 additions & 0 deletions extension/popup/functions/onError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description Display the popup's error message, and hide the normal UI.
*
* @param error - contains error message
*/
export default function (error) {
document.querySelector('#popup-content').classList.add('hidden');
document.querySelector('#error-content').classList.remove('hidden');
console.error(`Failed to execute opt-out content script: ${error.message}`);
}
12 changes: 12 additions & 0 deletions extension/popup/functions/updateOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import updateSliderKnob from './updateSliderKnob.js';

/**
* @description Updates slider and option states, from values provided by result, and sets them in popup
* @param result {object}
*/
export default function (result) {
document.querySelector(`#${result.optOut.selector}`).checked = true;
const slider = document.querySelector('#slider');
slider.value = result.optOut.slider;
updateSliderKnob(slider);
}
8 changes: 8 additions & 0 deletions extension/popup/functions/updateSliderKnob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @description Updates slider's knob css image depending on the current value
*
* @param slider
*/
export default function (slider) {
(slider.value === '1') ? slider.classList.remove('slider-angry') : slider.classList.add('slider-angry');
}
37 changes: 37 additions & 0 deletions extension/popup/popup-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import updateOptions from './functions/updateOptions.js';
import onError from './functions/onError.js';
import updateSliderKnob from './functions/updateSliderKnob.js';

/**
* @description get input values, send them to storage and send message for update to background
* script
* @param tabs
*/
function setChecks(tabs) {
const slider = document.getElementById('slider');
const optionValue = document.querySelector('input[name="text_options"]:checked').value || 'tc';
const popupSettings = {
slider: slider.value,
selector: optionValue,
};

updateSliderKnob(slider);
browser.storage.sync.set({ optOut: popupSettings });
browser.tabs.sendMessage(tabs[0].id, popupSettings);
}

/**
* When the popup loads, inject a content script into the active tab,
* and add a click handler.
* If we couldn't inject the script, handle the error.
*/
document.addEventListener('DOMContentLoaded', browser.storage.sync.get('optOut').then(updateOptions, onError));
document.addEventListener('input', () => {
browser.tabs
.query({
active: true,
url: 'https://twitter.com/*',
currentWindow: true,
})
.then(setChecks, onError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>Title</title>
<link rel="stylesheet" href="set_view_mod.css"/>
<link rel="stylesheet" href="assets/styles.css"/>
</head>

<body>
Expand All @@ -26,7 +26,7 @@ <h2>How should it be hidden?</h2>
</label>
<label for="text_white">
<input type="radio" id="text_white" name="text_options" value="text_white">
<span>White text</span>
<span>Make text invisible</span>
</label>
<label for="text_removed">
<input type="radio" id="text_removed" name="text_options" value="text_removed">
Expand All @@ -42,7 +42,7 @@ <h2>How should it be hidden?</h2>
<p>Check your internet connection</p>
</div>

<script src="set_view_mod.js"></script>
<script type="module" src="popup-main.js"></script>


</body>
Expand Down
19 changes: 0 additions & 19 deletions extension/popup/reset.js

This file was deleted.

Loading

0 comments on commit 60908ee

Please sign in to comment.