Skip to content

Commit

Permalink
Merge pull request #6 from corbindavenport/dev
Browse files Browse the repository at this point in the history
Wikipedia Search v9.0
  • Loading branch information
corbindavenport authored Nov 25, 2017
2 parents a54b32c + e20eddb commit 89f484e
Show file tree
Hide file tree
Showing 18 changed files with 1,039 additions and 1,003 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
Wikipedia Search
================
This extension allows you to type the word 'wiki' into your browser's search bar, press the space key, and search for an article on Wikipedia. You can also select any word or phrase on the web, right-click, and search it in Wikipedia. If you have [Awesome New Tab Page](https://antp.co) installed, there's even a widget for that built-in. Instant access to the millions of articles of articles on Wikipedia.

If English isn't your main language, it's easy to change the language Wikipedia Search uses. Just go to the Settings page to switch to any of the 50+ languages Wikipedia supports.
This extension integrates Wikipedia searching capabilities into Chrome and Opera. You can type the word 'wiki' into the address bar, press space (or tab), and start typing the article you want to find. Live results are pulled from Wikipedia as you type. The search language is automatically detected, but you can change it manually to any of the 50+ languages that Wikipedia supports.

__Wikipedia is a trademark of the Wikimedia Foundation.__
In addition to the address bar search, this extension also adds a 'Search in Wikipedia' option to the right-click menu when you select text on any page. If you have [Awesome New Tab Page](https://antp.co) installed, Wikipedia Search provides a widget you can use.

[Download for Chrome](https://chrome.google.com/webstore/detail/wikipedia-search/lipakennkogpodadpikgipnogamhklmk)

[Download for Opera](https://addons.opera.com/en/extensions/details/wikipedia-search/)

__Wikipedia is a trademark of the Wikimedia Foundation. Wikipedia Search is in no way affiliated with Wikipedia or the Wikimedia Foundation.__

---------------------------------------------------------
__New in Wikipedia Search 8.1:__
* Clicking toolbar icon now opens extension settings
* Minor bug fixes

__New in Wikipedia Search 9.0:__

* Redesigned settings and welcome pages
* Search language now defaults to system language, instead of English
* Updated widget for Awesome New Tab Page
* Added support for Tamiḻ and Turkce languages
* Improved support for Opera
* Other bug fixes and improvements

---------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions css/bootstrap-theme.min.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions css/bootstrap.min.css

Large diffs are not rendered by default.

79 changes: 32 additions & 47 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,46 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/

// Check for settings & version
// List of Wikipedia's supported language in an array, for the auto-detect functionality
var langArray= ["ar","az","bg","nan","be","ca","cs","da","de","et","el","en","es","eo","eu","fa","fr","gl","ko","hy","hi","hr","id","it","he","ka","la","lt","hu","ms","min","nl","ja","no","nn","ce","uz","pl","pt","kk","ro","ru","ceb","sk","sl","sr","sh","fi","sv","ta","th","tr","uk","ur","vi","vo","war","zh"];
var detailArray = ["العربية","Azərbaycanca","Български","Bân-lâm-gú / Hō-ló-oē","Беларуская (Акадэмічная)","Català","Čeština","Dansk","Deutsch","Eesti","Ελληνικά","English","Español","Esperanto","Euskara","فارسی","Français","Galego","한국어","Հայերեն","हिन्दी","Hrvatski","Bahasa Indonesia","Italiano","עברית","ქართული","Latina","Lietuvių","Magyar","Bahasa Melayu","Bahaso Minangkabau","Nederlands","日本語","Norsk (Bokmål)","Norsk (Nynorsk)","Нохчийн","Oʻzbekcha / Ўзбекча","Polski","Português","Қазақша / Qazaqşa / قازاقشا","Română","Русский","Sinugboanong Binisaya","Slovenčina","Slovenščina","Српски / Srpski","Srpskohrvatski / Српскохрватски","Suomi","Svenska","தமிழ்","ภาษาไทย","Türkçe","Українська","اردو","Tiếng Việt","Volapük","Winaray","中文"];

// Check for settings & version
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "update" || "install"){
// Transfer data from Wikipedia Search 7.0.2 or below
// Wikipedia Search 7.1+ use booleans for localStorage
if (localStorage.getItem("shortcut") === "on") {
if ((localStorage.getItem("shortcut") === "on") || (localStorage.getItem("shortcut") === null)) {
localStorage["shortcut"] = "true";
} else if (localStorage.getItem("shortcut") === "off") {
localStorage["shortcut"] = "false";
}
if (localStorage.getItem("contentscripts") === "on") {
localStorage["contentscripts"] = "true";
} else if (localStorage.getItem("contentscripts") === "off") {
localStorage["contentscripts"] = "false";
}
if (localStorage.getItem("hidesearch") === "on") {
localStorage["hidesearch"] = "true";
} else if (localStorage.getItem("hidesearch") === "off") {
localStorage["hidesearch"] = "false";
}

if (localStorage.getItem("language") === null) {
localStorage["language"] = "en";
// Detect the user's system language
var lang = navigator.languages[0];
// Cut off the localization part if it exists (e.g. en-US becomes en), to match with Wikipedia's format
var n = lang.indexOf('-');
lang = lang.substring(0, n != -1 ? n : lang.length);
// Check if the language has a Wikipedia
if (langArray.includes(lang)) {
console.log("Language auto-detected as '" + lang + "' (" + detailArray[langArray.indexOf(lang)] + ")");
localStorage["language"] = lang;
localStorage["full-language"] = detailArray[langArray.indexOf(lang)];
} else {
// Set it to English as default
console.log("Could not auto-detect language, defaulting to 'en' (English)")
localStorage["language"] = "en";
localStorage["full-language"] = detailArray[langArray.indexOf("en")];
}
}
if (localStorage.getItem("full-language") === null) {
localStorage["full-language"] = detailArray[langArray.indexOf(localStorage["language"])];
}
if (localStorage.getItem("protocol") === null) {
localStorage["protocol"] = "https://";
}
if (localStorage.getItem("shortcut") === null) {
localStorage["shortcut"] = "true";
}
if (localStorage.getItem("contentscripts") === null) {
localStorage["contentscripts"] = "true";
}
if (localStorage.getItem("hidesearch") === null) {
localStorage["hidesearch"] = "false";
if (localStorage.getItem("settings-modified") === null) {
localStorage["settings-modified"] = "false";
}
}
if(localStorage.getItem("version") != chrome.runtime.getManifest().version){
Expand All @@ -55,7 +59,6 @@ chrome.browserAction.onClicked.addListener(function() {
});

// Awesome New Tab Page Widget

var info = {
poke: 3,
width: 1,
Expand Down Expand Up @@ -84,9 +87,8 @@ chrome.extension.onMessageExternal.addListener(function (request, sender, sendRe
});

// Context Menu Search

chrome.contextMenus.create({
title: "Search \"%s\" on Wikipedia",
title: "Search Wikipedia for \"%s\"",
contexts: ["selection"],
onclick: function searchText(info){
var url = encodeURI(localStorage["protocol"] + localStorage["language"] + ".wikipedia.org/w/index.php?title=Special:Search&search=" +info.selectionText);
Expand All @@ -96,38 +98,24 @@ chrome.contextMenus.create({

// Omnibox Search
// Derived from OmniWiki (github.com/hamczu/OmniWiki)

var currentRequest = null;

chrome.omnibox.onInputChanged.addListener(function(text, suggest) {

if (currentRequest != null) {
currentRequest.onreadystatechange = null;
currentRequest.abort();
currentRequest = null;
}

updateDefaultSuggestion(text);

if (text.length > 0) {
currentRequest = suggests(text, function(data) {
var results = [];
// Opera only supports showing four search results at a time, while Chrome can show five
// When the settings shortcut is enabled, it takes up one of the search results slots
// If shortcut is disabled = Five search results in Chrome, four search results in Opera
// If shortcut is enabled = Four search results in Chrome, three search results in Opera
// If shortcut is disabled = Show five search results
// If shortcut is enabled = Show four search results + shortcut
if (localStorage.getItem("shortcut") === "true") {
if (window.navigator.userAgent.indexOf("OPR") > -1) {
num = 3;
} else {
num = 4;
}
num = 4;
} else {
if (window.navigator.userAgent.indexOf("OPR") > -1) {
num = 4;
} else {
num = 5;
}
num = 5;
}
for(var i = 0; i < num; i++){
results.push({
Expand All @@ -138,13 +126,12 @@ chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
if (localStorage.getItem("shortcut") === "true") {
results.push({
content: "settings",
description: "<dim>Open settings for Wikipedia Search</dim>"
description: "<dim>Change search language (currently set to " + localStorage["full-language"] + ")</dim>"
});
}
suggest(results);
});
}

});

function resetDefaultSuggestion() {
Expand All @@ -159,7 +146,6 @@ function updateDefaultSuggestion(text) {
chrome.omnibox.setDefaultSuggestion({
description: searchLabel + 'Search on Wikipedia: %s'
});

};

chrome.omnibox.onInputStarted.addListener(function() {
Expand All @@ -170,7 +156,6 @@ chrome.omnibox.onInputCancelled.addListener(function() {
resetDefaultSuggestion();
});


function suggests(query, callback) {
var language = localStorage["language"];
var protocol = localStorage["protocol"];
Expand Down
7 changes: 7 additions & 0 deletions js/bootstrap.min.js

Large diffs are not rendered by default.

54 changes: 4 additions & 50 deletions js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,8 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/

// Wikipedia integration

chrome.runtime.sendMessage({method: "getLocalStorage", key: "contentscripts"}, function(response) {
if (response.data === "true") {
if (window.location.href.indexOf("/wiki/") > -1) {
// Add extra button for Wikipedia Search on left panel
var wikipanel = document.createElement("div");
wikipanel.setAttribute("class", "portal");
wikipanel.setAttribute("id", "wikipediasearchpanel");
wikipanel.innerHTML = '<h3>Wikipedia Search ' + chrome.runtime.getManifest().version + '</h3><div class="body"><ul><li><a href="' + chrome.extension.getURL('settings.html') + '" title="Open Wikipedia Search extension settings in new tab" target="_blank">Extension Settings</a></li><li><a href="http://github.com/corbindavenport/wikipedia-search" title="Open Wikipedia Search extenion project on GitHub in a new tab" target="_blank">GitHub Project</a></li><li><a href="http://github.com/corbindavenport/wikipedia-search/issues/new" title="Submit bug report through GitHub" target="_blank">Report bug</a></li></ul></div>';
var panelparent = document.getElementById("mw-panel");
panelparent.insertBefore(wikipanel, document.getElementById("mw-panel").getElementsByTagName("div")[3]);
}
} else {
if (window.location.href.indexOf("/wiki/") > -1) {
var info = document.createElement("li");
info.setAttribute("id", "wikipediasearchinfo");
info.innerHTML = '<span style="color: gray;">Wikipedia Search ' + chrome.runtime.getManifest().version + '</span> (<a href="' + chrome.extension.getURL('settings.html') + '" title="Open Wikipedia Search extension settings in new tab" target="_blank">Settings</a>)';
var headerparent = document.getElementById("p-personal").getElementsByTagName("ul")[0];
console.log(headerparent);
headerparent.insertBefore(info, document.getElementById("pt-userpage"));
}
}
});

// Hide search on Wikipedia article pages

chrome.runtime.sendMessage({method: "getLocalStorage", key: "hidesearch"}, function(response) {
if (response.data === "true" && window.location.href.indexOf("/wiki/") > -1) {
document.getElementById("searchform").style.display = "none";
document.getElementById("p-search").style.marginRight = "0";
}
});

// Remind users of Wikipedia Search on Wikipedia home page

if ((window.location.href.indexOf("www.wikipedia.org")) > -1 && (document.body.querySelector(".search-form"))) {
var searchcontainer = document.body.querySelector(".search-form").getElementsByTagName("fieldset")[0];
searchcontainer.innerHTML = searchcontainer.innerHTML + "<br /><br />Since you have the Wikipedia Search extension installed, you can search Wikipedia from your browser's search bar. Just type 'wiki' in the search bar, then a space, then the name of the article you want.";
}

// The easter egg can be enabled by running this command in the JavaScript console, with the Wikipedia Search settings open:
// localStorage['easteregg'] = "on";
// and turned off with:
// localStorage['easteregg'] = "off";

chrome.runtime.sendMessage({method: "getLocalStorage", key: "easteregg"}, function(response) {
if (response.data === "on" && window.location.href.indexOf("/wiki/") > -1) {
document.querySelector(".mw-wiki-logo").style.backgroundImage = "url('http://upload.wikimedia.org/wikipedia/commons/5/5f/Wiki_logo_The_Cunctator.png')";
}
});
if ((window.location.href.indexOf("www.wikipedia.org")) > -1 && (document.body.querySelector(".search-container"))) {
var searchcontainer = document.body.querySelector(".search-container");
searchcontainer.innerHTML = searchcontainer.innerHTML + "<i>Since you have the Wikipedia Search extension installed, you can search Wikipedia from your browser's search bar. Just type 'wiki' in the search bar, then a space, then the name of the article you want.</i>";
}
4 changes: 0 additions & 4 deletions js/jquery-3.1.1.min.js

This file was deleted.

4 changes: 4 additions & 0 deletions js/jquery-3.2.1.slim.min.js

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions js/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/

// List of Wikipedia's supported language in an array
var langArray= ["ar","az","bg","nan","be","ca","cs","da","de","et","el","en","simple","es","eo","eu","fa","fr","gl","ko","hy","hi","hr","id","it","he","ka","la","lt","hu","ms","min","nl","ja","no","nn","ce","uz","pl","pt","kk","ro","ru","ceb","sk","sl","sr","sh","fi","sv","ta","th","tr","uk","ur","vi","vo","war","zh"];
var detailArray = ["العربية","Azərbaycanca","Български","Bân-lâm-gú / Hō-ló-oē","Беларуская (Акадэмічная)","Català","Čeština","Dansk","Deutsch","Eesti","Ελληνικά","English","Simple English","Español","Esperanto","Euskara","فارسی","Français","Galego","한국어","Հայերեն","हिन्दी","Hrvatski","Bahasa Indonesia","Italiano","עברית","ქართული","Latina","Lietuvių","Magyar","Bahasa Melayu","Bahaso Minangkabau","Nederlands","日本語","Norsk (Bokmål)","Norsk (Nynorsk)","Нохчийн","Oʻzbekcha / Ўзбекча","Polski","Português","Қазақша / Qazaqşa / قازاقشا","Română","Русский","Sinugboanong Binisaya","Slovenčina","Slovenščina","Српски / Srpski","Srpskohrvatski / Српскохрватски","Suomi","Svenska","தமிழ்","ภาษาไทย","Türkçe","Українська","اردو","Tiếng Việt","Volapük","Winaray","中文"];

// Load settings
$(window).on('load', function() {
// Populate languages menu
langArray.forEach(function callback(currentValue, index, array) {
$('#language').append($('<option>', {
value: currentValue,
text: detailArray[langArray.indexOf(currentValue)]
}));
});
// Remove loading option
$("#language option[value='loading'").remove();
// Set to current language
$("#language").val(localStorage["language"]);
if (localStorage.getItem("protocol") === "https://") {
$("input[name='protocol']").prop("checked", true);
} else {
$("input[name='protocol']").prop("checked", true);
}
$("input[name='shortcut']").prop("checked", $.parseJSON(localStorage.getItem("shortcut")));
});

// Reset language button
$(document).on('click', ".reset-language", function() {
// Detect the user's system language
var lang = navigator.languages[0];
// Cut off the localization part if it exists (e.g. en-US becomes en), to match with Wikipedia's format
var n = lang.indexOf('-');
lang = lang.substring(0, n != -1 ? n : lang.length);
console.log(lang)
// Check if the language has a Wikipedia
if (langArray.includes(lang)) {
$("#language").val(lang);
localStorage["language"] = lang;
localStorage["full-language"] = detailArray[langArray.indexOf(lang)];
} else {
alert("Sorry, Wikipedia Search could not auto-detect your system language.")
}
});

// Save settings
$(document).on('change', "input,select", function() {
localStorage["settings-modified"] = "true";
localStorage["language"] = $("#language").val();
localStorage["full-language"] = detailArray[langArray.indexOf($("#language").val())];
if ($("input[name='protocol']").is(":checked")) {
localStorage["protocol"] = "https://";
} else {
localStorage["protocol"] = "http://";
}
localStorage.setItem("shortcut", $("input[name='shortcut']").is(":checked"));
});
31 changes: 31 additions & 0 deletions js/welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/

$(document).ready(function() {

// Add version number to welcome page
$(".version").html(" " + chrome.runtime.getManifest().version);

// Add language to welcome page
if ($.parseJSON(localStorage.getItem("settings-modified")) == false) {
$(".language-warning").html('The search language has been auto-detected as <b>' + localStorage["full-language"] + ' (' + localStorage["language"] + ')</b>.<a class="btn btn-primary" href="settings.html" role="button">Change language</a>');
} else {
$(".language-warning").html('The search language is currently set to <b>' + localStorage["full-language"] + ' (' + localStorage["language"] + ')</b>.<a class="btn btn-primary" href="settings.html" role="button">Change language</a>');
}

//Show instructions for leaving a review based on the browser being used
var useragent = navigator.userAgent;

// Opera has to be checked before Chrome, because Opera has both "Chrome" and "OPR" in the user agent string
if (useragent.includes("OPR")) {
$('.review-info').html('Also please leave a review on the <a href="https://addons.opera.com/en/extensions/details/wikipedia-search/" target="_blank">Opera add-ons site</a> if you can.');
} else if (useragent.includes("Chrome")) {
$('.review-info').html('Also please leave a review on the <a href="https://chrome.google.com/webstore/detail/wikipedia-search/lipakennkogpodadpikgipnogamhklmk" target="_blank">Chrome Web Store</a> if you can.');
}

});
Loading

0 comments on commit 89f484e

Please sign in to comment.