Last active
October 14, 2019 17:49
-
-
Save Fordi/a46b9437859f5b613d863527abb8719f to your computer and use it in GitHub Desktop.
Politifact "Truthfulness" score
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
// ==UserScript== | |
// @name PolitiFact "Truthfulness" | |
// @match https://www.politifact.com/personalities/* | |
// @updateUrl https://gist.githubusercontent.com/Fordi/a46b9437859f5b613d863527abb8719f/raw/Politifact%2520Truthfulness.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const weights = [1, 0.75, 0.5, 0.25, 0, -0.5]; | |
const template = ({ sum, total }) => { | |
const truth = Math.round(100 * sum / total); | |
return ` | |
<li class="chartlist__item"> | |
<span class="chartlist__label">Truthfulness</span> | |
<span class="chartlist__count"> | |
${sum} / ${total} (${truth}%) | |
</span> | |
<span class="chartlist__index" style="width:${truth}%;"> | |
(${truth}%) | |
</span> | |
</li> | |
`.trim(); | |
}; | |
const processChart = (chart) => { | |
const elements = [].slice.call(chart.querySelectorAll('.chartlist__count')); | |
if (elements.length === 7) { return; } | |
const data = elements.reduce(({ sum, total }, el, index) => { | |
const score = parseInt(el.textContent); | |
console.log(score); | |
return { | |
sum: sum + score * weights[index], | |
total: total + score, | |
}; | |
}, { sum: 0, total: 0 }); | |
const div = document.createElement('div'); | |
div.innerHTML = template(data); | |
div.querySelector('.chartlist__label').setAttribute('title', ` | |
Aggregated score based on the weighted mean of evaulated statements. | |
Weights are: | |
True: 1.0 | |
Mostly true: 0.75 | |
Half-true: 0.5 | |
Mostly false: 0.25 | |
False: 0 | |
Pants on Fire: -0.5 | |
`.trim()); | |
chart.appendChild(div.firstElementChild); | |
} | |
[].forEach.call(unsafeWindow.document.querySelectorAll('.chartlist'), processChart); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment