Skip to content

Instantly share code, notes, and snippets.

@Fordi
Last active October 14, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fordi/a46b9437859f5b613d863527abb8719f to your computer and use it in GitHub Desktop.
Save Fordi/a46b9437859f5b613d863527abb8719f to your computer and use it in GitHub Desktop.
Politifact "Truthfulness" score
// ==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