Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1305 → Rev 1306

/firefox/selsearch/trunk/test/index.html
1,8 → 1,8
<h1>SelSearch</h1>
 
Select a search engine to query from the context menu
 
<form>
<input type=text value="some text field"><br>
<textarea>some text area</textarea>
<h1>SelSearch</h1>
 
Select a search engine to query from the context menu
 
<form>
<input type=text value="some text field"><br>
<textarea>some text area</textarea>
</form>
/firefox/selsearch/trunk/src/install.rdf
1,26 → 1,26
<?xml version="1.0"?>
 
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
 
<Description about="urn:mozilla:install-manifest">
<em:id>{58C3BCF4-A9A4-11DD-A2A9-4FD0196F0FEF}</em:id>
<em:version>1.2</em:version>
<em:type>2</em:type>
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>3</em:minVersion>
<em:maxVersion>3.0.*</em:maxVersion>
</Description>
</em:targetApplication>
<em:name>SelSearch</em:name>
<em:description>Select a search engine to query from the context menu, add the search menu for text boxes</em:description>
<em:creator>Anatoli Klassen</em:creator>
<em:homepageURL>http://www.26th.net/public/projects/selsearch</em:homepageURL>
<?xml version="1.0"?>
 
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
 
<Description about="urn:mozilla:install-manifest">
<em:id>{58C3BCF4-A9A4-11DD-A2A9-4FD0196F0FEF}</em:id>
<em:version>1.2</em:version>
<em:type>2</em:type>
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>3</em:minVersion>
<em:maxVersion>3.0.*</em:maxVersion>
</Description>
</em:targetApplication>
<em:name>SelSearch</em:name>
<em:description>Select a search engine to query from the context menu, add the search menu for text boxes</em:description>
<em:creator>Anatoli Klassen</em:creator>
<em:homepageURL>http://www.26th.net/public/projects/selsearch</em:homepageURL>
<em:updateURL>http://www.26th.net/public/projects/selsearch/update.rdf</em:updateURL>
<em:updateKey>MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0n3FJvEygIzLWtleG7RQGsW33KlWt2CEKm4cNdyYtos2PXb68ZwMIGDduDeAdv0kg90Y2vnHpEgJwE4O6Ce6bfjxQWNbCWj6UgjRaosuzqKq4+BBwT65hMcIs72K1x3zQde6oNBsnYiEP18Cy7aY26+QA7M/vcOY5Rut66xYcjQIDAQAB</em:updateKey>
</Description>
</RDF>
<em:updateKey>MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0n3FJvEygIzLWtleG7RQGsW33KlWt2CEKm4cNdyYtos2PXb68ZwMIGDduDeAdv0kg90Y2vnHpEgJwE4O6Ce6bfjxQWNbCWj6UgjRaosuzqKq4+BBwT65hMcIs72K1x3zQde6oNBsnYiEP18Cy7aY26+QA7M/vcOY5Rut66xYcjQIDAQAB</em:updateKey>
</Description>
</RDF>
/firefox/selsearch/trunk/src/chrome.manifest
1,2 → 1,2
content selsearch chrome/content/
overlay chrome://browser/content/browser.xul chrome://selsearch/content/selsearch.xul
content selsearch chrome/content/
overlay chrome://browser/content/browser.xul chrome://selsearch/content/selsearch.xul
/firefox/selsearch/trunk/src/chrome/content/selsearch.xul
1,14 → 1,14
<?xml version="1.0"?>
 
<overlay id="selsearch"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
<script type="application/x-javascript" src="chrome://selsearch/content/selsearch.js" />
<popup id="contentAreaContextMenu">
<menu id="context-selsearch-submenu" class="menuitem-iconic" insertafter="context-searchselect">
<menupopup id="context-selsearch-menupopup" />
</menu>
</popup>
</overlay>
<?xml version="1.0"?>
 
<overlay id="selsearch"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
<script type="application/x-javascript" src="chrome://selsearch/content/selsearch.js" />
<popup id="contentAreaContextMenu">
<menu id="context-selsearch-submenu" class="menuitem-iconic" insertafter="context-searchselect">
<menupopup id="context-selsearch-menupopup" />
</menu>
</popup>
</overlay>
/firefox/selsearch/trunk/src/chrome/content/selsearch.js
1,158 → 1,158
var SelSearch = {
init: function()
{
var menu = document.getElementById('contentAreaContextMenu');
menu.addEventListener('popupshowing', SelSearch.onPopupShowing, false);
},
 
onPopupShowing: function()
{
try {
var sel = getBrowserSelection(16);
if(!sel) { // nothing selected in html - try to find selected text in input box
sel = SelSearch.getTextBoxSelection(16);
}
var submenu = document.getElementById('context-selsearch-submenu');
var popup = document.getElementById('context-selsearch-menupopup');
if(submenu && popup) {
while(popup.hasChildNodes()){
popup.removeChild(popup.firstChild);
}
if(sel) {
if(sel.length > 15) {
sel = sel.substr(0,15) + "\u2026";
}
 
submenu.hidden = false;
submenu.label = 'Search "' + sel + '" in';
 
var ss = SelSearch.getSearchService();
var engines = ss.getVisibleEngines({ });
 
if(engines && engines.length > 0) {
var curEngine;
if(isElementVisible(BrowserSearch.searchBar)) {
curEngine = ss.currentEngine.name;
}
else {
curEngine = ss.defaultEngine.name;
}
// show the default engine
for(var i = 0, l = engines.length; i < l; ++i) {
if(engines[i].name == curEngine) {
SelSearch.addMenuItem(popup, i, engines[i].name, engines[i].iconURI, true);
popup.appendChild(document.createElementNS(
'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuseparator'));
break;
}
}
// show all
for(var i = 0, l = engines.length; i < l; ++i) {
SelSearch.addMenuItem(popup, i, engines[i].name, engines[i].iconURI, engines[i].name == curEngine);
}
}
}
else {
submenu.hidden = true;
submenu.label = 'Search in';
}
}
 
// we cannot just remove the 'context-searchselect' in the overlay because it could be
// some other items which use it as address point, so hide it here
var originItem = document.getElementById('context-searchselect');
if(originItem) {
originItem.hidden = true;
}
}
catch(ex) {
alert(ex);
}
},
 
getTextBoxSelection: function(aCharLen)
{
if(!gContextMenu) return null;
if(!gContextMenu.target) return null;
if(!gContextMenu.target.value) return null;
var start = gContextMenu.target.selectionStart;
var end = gContextMenu.target.selectionEnd;
if(start < 0 || end < 0) {
return null;
}
var selection = gContextMenu.target.value.toString().substring(start, end);
//
// the code from browser.js/getBrowserSelection
//
// selections of more than 150 characters aren't useful
const kMaxSelectionLen = 150;
const charLen = Math.min(aCharLen || kMaxSelectionLen, kMaxSelectionLen);
if (selection) {
if (selection.length > charLen) {
// only use the first charLen important chars. see bug 221361
var pattern = new RegExp("^(?:\\s*.){0," + charLen + "}");
pattern.test(selection);
selection = RegExp.lastMatch;
}
selection = selection.replace(/^\s+/, "")
.replace(/\s+$/, "")
.replace(/\s+/g, " ");
if (selection.length > charLen)
selection = selection.substr(0, charLen);
}
return selection;
},
getSearchService: function()
{
const nsIBSS = Components.interfaces.nsIBrowserSearchService;
return Components.classes["@mozilla.org/browser/search-service;1"].getService(nsIBSS);
},
addMenuItem: function(popup, num, label, iconURI, bold)
{
var e = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
e.setAttribute('label', label);
e.setAttribute('oncommand', 'SelSearch.search(' + num + ')');
e.setAttribute('class', 'menuitem-iconic');
if(iconURI) {
e.style.listStyleImage = "url('" + iconURI.spec + "')";
}
if(bold) {
e.style.fontWeight = 'bold';
}
popup.appendChild(e);
},
search: function(num) {
var sel = getBrowserSelection();
var ss = SelSearch.getSearchService();
var engines = ss.getVisibleEngines({ });
 
if(num < 0 || num >= engines.length) return;
if(!sel) return;
var submission = engines[num].getSubmission(sel, null); // HTML response
if(!submission) return;
getBrowser().loadOneTab(submission.uri.spec, null, null, submission.postData, null, false);
},
};
 
window.addEventListener('load', SelSearch.init, false);
var SelSearch = {
init: function()
{
var menu = document.getElementById('contentAreaContextMenu');
menu.addEventListener('popupshowing', SelSearch.onPopupShowing, false);
},
 
onPopupShowing: function()
{
try {
var sel = getBrowserSelection(16);
if(!sel) { // nothing selected in html - try to find selected text in input box
sel = SelSearch.getTextBoxSelection(16);
}
var submenu = document.getElementById('context-selsearch-submenu');
var popup = document.getElementById('context-selsearch-menupopup');
if(submenu && popup) {
while(popup.hasChildNodes()){
popup.removeChild(popup.firstChild);
}
if(sel) {
if(sel.length > 15) {
sel = sel.substr(0,15) + "\u2026";
}
 
submenu.hidden = false;
submenu.label = 'Search "' + sel + '" in';
 
var ss = SelSearch.getSearchService();
var engines = ss.getVisibleEngines({ });
 
if(engines && engines.length > 0) {
var curEngine;
if(isElementVisible(BrowserSearch.searchBar)) {
curEngine = ss.currentEngine.name;
}
else {
curEngine = ss.defaultEngine.name;
}
// show the default engine
for(var i = 0, l = engines.length; i < l; ++i) {
if(engines[i].name == curEngine) {
SelSearch.addMenuItem(popup, i, engines[i].name, engines[i].iconURI, true);
popup.appendChild(document.createElementNS(
'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuseparator'));
break;
}
}
// show all
for(var i = 0, l = engines.length; i < l; ++i) {
SelSearch.addMenuItem(popup, i, engines[i].name, engines[i].iconURI, engines[i].name == curEngine);
}
}
}
else {
submenu.hidden = true;
submenu.label = 'Search in';
}
}
 
// we cannot just remove the 'context-searchselect' in the overlay because it could be
// some other items which use it as address point, so hide it here
var originItem = document.getElementById('context-searchselect');
if(originItem) {
originItem.hidden = true;
}
}
catch(ex) {
alert(ex);
}
},
 
getTextBoxSelection: function(aCharLen)
{
if(!gContextMenu) return null;
if(!gContextMenu.target) return null;
if(!gContextMenu.target.value) return null;
var start = gContextMenu.target.selectionStart;
var end = gContextMenu.target.selectionEnd;
if(start < 0 || end < 0) {
return null;
}
var selection = gContextMenu.target.value.toString().substring(start, end);
//
// the code from browser.js/getBrowserSelection
//
// selections of more than 150 characters aren't useful
const kMaxSelectionLen = 150;
const charLen = Math.min(aCharLen || kMaxSelectionLen, kMaxSelectionLen);
if (selection) {
if (selection.length > charLen) {
// only use the first charLen important chars. see bug 221361
var pattern = new RegExp("^(?:\\s*.){0," + charLen + "}");
pattern.test(selection);
selection = RegExp.lastMatch;
}
selection = selection.replace(/^\s+/, "")
.replace(/\s+$/, "")
.replace(/\s+/g, " ");
if (selection.length > charLen)
selection = selection.substr(0, charLen);
}
return selection;
},
getSearchService: function()
{
const nsIBSS = Components.interfaces.nsIBrowserSearchService;
return Components.classes["@mozilla.org/browser/search-service;1"].getService(nsIBSS);
},
addMenuItem: function(popup, num, label, iconURI, bold)
{
var e = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
e.setAttribute('label', label);
e.setAttribute('oncommand', 'SelSearch.search(' + num + ')');
e.setAttribute('class', 'menuitem-iconic');
if(iconURI) {
e.style.listStyleImage = "url('" + iconURI.spec + "')";
}
if(bold) {
e.style.fontWeight = 'bold';
}
popup.appendChild(e);
},
search: function(num) {
var sel = getBrowserSelection();
var ss = SelSearch.getSearchService();
var engines = ss.getVisibleEngines({ });
 
if(num < 0 || num >= engines.length) return;
if(!sel) return;
var submission = engines[num].getSubmission(sel, null); // HTML response
if(!submission) return;
getBrowser().loadOneTab(submission.uri.spec, null, null, submission.postData, null, false);
},
};
 
window.addEventListener('load', SelSearch.init, false);