Skip to content

Instantly share code, notes, and snippets.

@folarb
Last active April 20, 2018 17:08
Show Gist options
  • Save folarb/34678 to your computer and use it in GitHub Desktop.
Save folarb/34678 to your computer and use it in GitHub Desktop.
search Mozilla Add-ons for add-ons
makeSearchCommand({
name: "add-on",
icon: "https://addons.mozilla.org/favicon.ico",
author: {
name: "Robert B****",
email: "folarb@gmail.com"
},
description: "Searches Mozilla Add-ons for your words",
help: "Doesn't show experimental add-ons (like ubiquity) yet. Enter (part of) the name or the description of an add-on. The first 5 results will be displayed in the preview, hit return to go to the search page",
url: "https://addons.mozilla.org/en-US/firefox/search?q={QUERY}",
preview: function(pBlock, directObject) {
if (!directObject.text || directObject.text.length < 1) {
pBlock.innerHTML = "Search Mozilla Add-ons";
return;
}
if (/^\d+$/.test(directObject.summary)) {
var url = "https://services.addons.mozilla.org/en-US/firefox/api/1.1/addon/" + directObject.text;
var searchType = 'lookup';
}
else {
var url = "https://services.addons.mozilla.org/en-US/firefox/api/1.1/search/" + directObject.text;
var searchType = 'search';
}
jQuery.ajax({
type: "GET",
url: url,
dataType: "xml",
error: function() {
pBlock.innerHTML = "Error: can't connect to server";
},
success: function(responseData) {
responseData = jQuery(responseData);
var items = [];
responseData.find("addon").slice(0, 5).each(function(itemIndex) {
var itemDetails = jQuery(this);
var newItem = {
name: itemDetails.find("name:first").text(),
url: itemDetails.find("learnmore").text()
};
if (itemDetails.find("summary").length > 0) {
newItem.summary = itemDetails.find("summary").text();
}
if (itemDetails.find("thumbnail").length > 0) {
newItem.image = itemDetails.find("thumbnail:first").text();
}
items.push(newItem);
});
var pData = {
searchType: searchType,
query: directObject.summary,
totalresults: responseData.find("searchresults").attr("total_results"),
items: items,
numitems: (searchType == "lookup" && items[0].name == "") ? 0 : items.length
};
var template = '';
template += '{if searchType == "lookup"}';
template += '{if numitems > 0}';
template += 'Add-on <b>${query}</b> is shown below.';
template += '{else}';
template += '<b>Your search--did not match any add-on.</b>';
template += '{/if}';
template += '{else}';
template += '{if numitems > 0}';
template += 'Found <a href="https://addons.mozilla.org/en-US/firefox/search?q=${query}">${totalresults} add-ons</a> on Mozilla Add-ons matching <b>${query}</b>.';
template += '{else}';
template += '<b>Your search--did not match any add-on.</b>';
template += '{/if}';
template += '{/if}';
template += '{if numitems > 0}';
template += '{for item in items}';
template += '<div style="clear: both; padding: 10px 0px; max-height: 120px;">';
template += '<a href="${item.url}">';
template += '{if item.image}';
template += '<img src="${item.image}" style="float: left; margin-right: 10px; max-height: 100px; max-width: 100px;"/>';
template += '{/if}';
template += '<u>${item.name}</u>';
template += '</a>';
template += '<small>';
template += '{if item.summary}';
template += '{if item.summary.length < 230}';
template += '<br />${item.summary}';
template += '{else}';
template += '<br />${item.summary.substr(0, 230)}' + '..';
template += '{/if}';
template += '{/if}';
template += '</small>';
template += '</div>';
template += '{/for}';
template += '{/if}';
template += '<br /> ';
pBlock.innerHTML = CmdUtils.renderTemplate(template, pData);
return;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment