Skip to content

Commit

Permalink
A Better Place 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonty committed Jun 28, 2014
0 parents commit d71b717
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
A Better Place
==============

Reading Twitter in the morning can be awful - an endless stream of bad news, anger, and depression. It sets the tone for your day.

Researchers at Facebook conducted unethical research proving the attitude of social media seriously affects mood for extended periods of time.

A Better Place is a positivity filter bubble. It uses terrible sentiment analysis to strip out negative Twitter posts from your timeline; hopefully leaving you with a few morsels that make your day tolerable.

It comes in both [Chrome extension](https://chrome.google.com/webstore/detail/a-better-place/ajihembjdmpchmpcocjlagkfcobmdaog) and bookmarklet flavours, however only the Chrome extension will automatically kick in between the hours of 7am and 11am every day. You can use the bookmarklet whenever you need it.

Have a nice day.

Bookmarklet
=========

Github is terrible and won't let you embed bookmarklets. Create a new bookmark and add the following as the link.
```
javascript:(function(){ document.body.appendChild(document.createElement('script')).src='https://raw.githack.com/Jonty/abetterplace/master/src/load.js'; })();
```
Alternatively there's a draggable link [over on the main site](http://jonty.co.uk/abetterplace).

Developers
========

This is using [AFIN-111](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010) sentiment analysis, which is awful, but still better than everything else when using twitter. A small amount of the code is taken from [sentiment](https://github.com/thisandagain/sentiment) by [thisandagain](https://github.com/thisandagain). Thanks.

If you want to play around with this you can see debug logging in the JS console, however Twitter disable it - you can re-enable it by issuing `delete console.log` in the console.
18 changes: 18 additions & 0 deletions chrome_extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "A Better Place",
"version": "0.0.1",
"manifest_version": 2,
"description": "Hides all negative tweets on twitter.com between the hours of 7am and 11am.",
"homepage_url": "http://jonty.co.uk/abetterplace",
"content_scripts": [
{
"matches": [
"https://twitter.com/*"
],
"js": [
"src/inject.js"
]
}
],
"web_accessible_resources": ["src/afinn.js", "src/abp.js", "src/run.js"]
}
1 change: 1 addition & 0 deletions chrome_extension/src/abp.js
1 change: 1 addition & 0 deletions chrome_extension/src/afinn.js
22 changes: 22 additions & 0 deletions chrome_extension/src/inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
chrome.extension.sendMessage({}, function(response) {
var readyStateCheckInterval = setInterval(function() {
if (document.readyState === "complete") {
clearInterval(readyStateCheckInterval);

var date = new Date();
var currentHour = date.getHours();

if (currentHour >= 7 && currentHour < 11) {
['afinn.js', 'abp.js', 'run.js'].map(function(script) {
var s = document.createElement('script');
s.src = chrome.extension.getURL('src/' + script);
s.onload = function() {
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
});
}

}
}, 10);
});
3 changes: 3 additions & 0 deletions chrome_extension/src/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log("Load complete, starting ABP...");
document.styleSheets[0].insertRule("[tweet-is-negative=true]{display:none}", 0);
setInterval(abp_process_tweets, 200);
37 changes: 37 additions & 0 deletions src/abp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function abp_tokenise (input) {
return input
.replace(/[^a-zA-Z ]+/g, '')
.replace('/ {2,}/',' ')
.toLowerCase()
.split(' ');
}

function abp_sentiment (phrase) {
var tokens = abp_tokenise(phrase),
score = 0;

var len = tokens.length;
while (len--) {
var obj = tokens[len];
var item = afinn[obj];
if (typeof item === 'undefined') continue;

score += item;
}

return score;
}

function abp_process_tweets() {
$('.tweet-text').not('.abp_processed').each(function(i, node) {
var score = abp_sentiment(node.textContent);
if (score <= 0) {
console.log('Removing: ' + node.textContent + '(Score: ' + score + ')');
node.parentNode.parentNode.setAttribute('tweet-is-negative', 'true');
} else {
console.log('Leaving: ' + node.textContent + '(Score: ' + score + ')');
}

node.className = node.className + 'abp_processed';
})
}
1 change: 1 addition & 0 deletions src/afinn.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function() {
var scripts = document.getElementsByTagName("script");
var CurrentScript = scripts[scripts.length-1].src;

var PathIndex = CurrentScript.lastIndexOf("/") + 1;
var Filepath = CurrentScript.substr(0, PathIndex);

var d = new Date();
var n = d.getTime();

$.getScript(Filepath + 'afinn.js?' + n).done(function(script, status) {
$.getScript(Filepath + 'abp.js?' + n).done(function(script, status) {
console.log("Load complete, starting ABP...");
document.styleSheets[0].insertRule("[tweet-is-negative=true]{display:none}", 0);
setInterval(abp_process_tweets, 200);
})
})

})();

0 comments on commit d71b717

Please sign in to comment.