-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorry about this.
- Loading branch information
Danny Walker
committed
Dec 26, 2017
1 parent
24893b6
commit 0abb76c
Showing
8 changed files
with
664 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# JuicyInfeaux | ||
|
||
Browser extension moves all the good stuff to the top of Wikipedia. | ||
|
||
## Why | ||
|
||
If like me, you go on Wikipedia all the time to find out what the most scandalous fact about a specific celebrity is, you might have been frustrated by how the juicy stuff is buried somewhere near the end of the page. | ||
|
||
JuicyInfeaux tries to fix that. | ||
|
||
It uses a list of keywords, and if there's a heading that includes one of these keywords, that section gets moved to the beginning of the article, getting you the juicy info you crave, faster. | ||
|
||
## Installation | ||
|
||
If this actually gets any users, I'll add it to the Chrome webstore. | ||
|
||
In the meantime: download the source of this and add it to Chrome in developer mode as an unpacked extension. See [here](https://developer.chrome.com/extensions/getstarted#unpacked). | ||
|
||
## Also | ||
- Follow [me on twitter](https://twitter.com/walkerdanny). | ||
- [Donate to Wikipedia](https://donate.wikimedia.org/) because they're good guys. | ||
|
||
|
||
## Thanks | ||
Thanks to Chris from [gomakethings](http://gomakethings.com) for writing the function that makes this whole thing. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// JuicyInfeaux by Danny Walker, 2017 | ||
// Licensed as CC BY-NC-SA 4.0 | ||
|
||
// I apologise for making this. | ||
|
||
// Here's the good shit we're looking for, feel free to edit this as you want | ||
var theJuicyKeyWords = ["Legal", "issues", "Arrest", "conviction", "personal", "lawsuit", "Controversy", "politics", "education", "drug", "views"]; | ||
|
||
// Probably not a great idea to edit from here onwards if you don't know what you're doing. | ||
|
||
var nextUntil = function (elem, selector, filter) { | ||
// Good-ass function courtesy of https://gomakethings.com/how-to-get-all-sibling-elements-until-a-match-is-found-with-vanilla-javascript/ | ||
// matches() polyfill | ||
if (!Element.prototype.matches) { | ||
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; | ||
} | ||
|
||
// Setup siblings array | ||
var siblings = []; | ||
|
||
siblings.push(elem); | ||
|
||
// Get the next sibling element | ||
elem = elem.nextElementSibling; | ||
|
||
// As long as a sibling exists | ||
while (elem) { | ||
|
||
// If we've reached our match, bail | ||
if (elem.matches(selector)) break; | ||
|
||
// If filtering by a selector, check if the sibling matches | ||
if (filter && !elem.matches(filter)) { | ||
elem = elem.nextElementSibling; | ||
continue; | ||
} | ||
|
||
// Otherwise, push it to the siblings array | ||
siblings.push(elem); | ||
|
||
// Get the next sibling element | ||
elem = elem.nextElementSibling; | ||
|
||
} | ||
return siblings; | ||
}; | ||
|
||
|
||
var prevH2 = function (elem) { | ||
// Basically the same as above but it only looks upwards for a h2 object | ||
// matches() polyfill | ||
if (!Element.prototype.matches) { | ||
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; | ||
} | ||
|
||
// Get the preious sibling element | ||
elem = elem.previousElementSibling; | ||
|
||
var foundH2; | ||
|
||
// As long as a sibling exists | ||
while (elem) { | ||
|
||
// If we've found a h2, bail | ||
if (elem.matches("h2")){ | ||
foundH2 = elem; | ||
break; | ||
} | ||
// If not... | ||
// Get the previous sibling element | ||
elem = elem.previousElementSibling; | ||
|
||
} | ||
return foundH2; | ||
}; | ||
|
||
// Formats a given string in the same way wikipedia formats their headings so we can get matches | ||
var formatHeading = function (juicyString){ | ||
var stringz = juicyString.split(" "); | ||
for(var w = 0; w<stringz.length;w++){ | ||
stringz[w] = stringz[w].toLowerCase(); | ||
} | ||
if(stringz[0].length>1){ | ||
var firstWord = stringz[0].substring(0,1).toUpperCase() + stringz[0].substring(1,stringz[0].length); | ||
stringz[0] = firstWord; | ||
} else { | ||
var firstWord = stringz[0].toUpperCase(); | ||
stringz[0] = firstWord; | ||
} | ||
var headingCased = ""; | ||
for(var w = 0; w<stringz.length;w++){ | ||
headingCased += stringz[w]; | ||
if(w != stringz.length-1){ | ||
headingCased += "_"; | ||
} | ||
} | ||
return headingCased; | ||
} | ||
|
||
// Arrays to store mathed h2 and h3 strings | ||
var theJuicyH2s = []; | ||
var theJuicyH3s = []; | ||
|
||
// Fun way of converting a HTMLCollection to an Array, which is brexit tbh | ||
var h2 = Array.prototype.slice.call(document.getElementsByTagName("h2"), 0); | ||
var h3 = Array.prototype.slice.call(document.getElementsByTagName("h3"), 0); | ||
|
||
// Grab the first h2 node (that isn't the word "Contents", so actually the second node) so we can put stuff after it | ||
h2First = h2[1]; | ||
|
||
// Search for h2 elements that match first, and then search for h3 elements that match separately | ||
for(var i=0;i<h2.length;i++){ | ||
for (var j=0;j<theJuicyKeyWords.length;j++){ | ||
var headingText = h2[i].firstChild.innerHTML; | ||
if(headingText){ // If there is some inner HTML | ||
var searchTerms = theJuicyKeyWords[j].split(" "); // Split the title up into words | ||
for(var k = 0; k<searchTerms.length; k++){ // Loop over the array returned | ||
if(headingText.toLowerCase().includes(searchTerms[k].toLowerCase())){ // Do the actual checking for matches with the juicy keywords | ||
theJuicyH2s.push(formatHeading(headingText)); // If we find a match, add it to the array | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Do the h3 search, the same as above | ||
for(var i=0;i<h3.length;i++){ | ||
for (var j=0;j<theJuicyKeyWords.length;j++){ | ||
var headingText = h3[i].firstChild.innerHTML; | ||
if(headingText){ | ||
var searchTerms = theJuicyKeyWords[j].split(" "); | ||
for(var k = 0; k<searchTerms.length; k++){ | ||
if(headingText.toLowerCase().includes(searchTerms[k].toLowerCase())){ | ||
theJuicyH3s.push(formatHeading(headingText)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
console.log("Moving these Sections:"); | ||
|
||
// Grab the matched elements and move them to in front of the first h2 | ||
for(var t = 0; t<theJuicyH2s.length;t++){ | ||
var f = document.getElementById(theJuicyH2s[t]); | ||
if(f){ | ||
console.log(theJuicyH2s[t]) | ||
f = f.parentElement; | ||
var g = nextUntil(f,"h2"); | ||
g.forEach(function(el){ | ||
h2First.insertAdjacentElement('beforebegin', el) | ||
}); | ||
} | ||
} | ||
|
||
// Kinda brexit way of not duplicating h2 nodes repeatedly given lots of h3 matches | ||
var copiedH2s = []; | ||
|
||
// Same as above, but with the check for duplicate h2s if there's multiple matches in one section | ||
for(var t = 0; t<theJuicyH3s.length;t++){ | ||
var f = document.getElementById(theJuicyH3s[t]); | ||
if(f){ | ||
console.log(theJuicyH3s[t]) | ||
f = f.parentElement; | ||
var h2above = prevH2(f); | ||
if(copiedH2s.includes(h2above)){ | ||
var h2copy = h2above.cloneNode(true); | ||
h2First.insertAdjacentElement('beforebegin', h2copy); | ||
copiedH2s.push(h2above); | ||
} | ||
var g = nextUntil(f,"h2,h3"); | ||
g.forEach(function(el){ | ||
h2First.insertAdjacentElement('beforebegin', el) | ||
}); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"manifest_version": 2, | ||
|
||
"name": "JuicyInfeaux", | ||
"short_name": "JuicyInfeaux", | ||
"description": "Makes Wikipedia good", | ||
"author": "Danny Walker", | ||
"version": "0.1", | ||
|
||
"icons": { | ||
"16": "icon-16.png", | ||
"32": "icon-32.png", | ||
"48": "icon-48.png", | ||
"128": "icon-128.png" | ||
}, | ||
|
||
"content_scripts": | ||
[ | ||
{ | ||
"matches": ["*://*.wikipedia.org/*"], | ||
"all_frames": true, | ||
"js": ["content_script.js"], | ||
"run_at": "document_end" | ||
} | ||
] | ||
} |