Subversion Repositories general

Compare Revisions

No changes between revisions

Ignore whitespace Rev 1323 → Rev 1324

/mozilla/selsearch/trunk/src/chrome/content/selsearch.js
0,0 → 1,162
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 ss = SelSearch.getSearchService();
var engines = ss.getVisibleEngines({ });
 
if(num < 0 || num >= engines.length) return;
 
var sel = getBrowserSelection();
if(!sel) { // nothing selected in html - try to find selected text in input box
sel = SelSearch.getTextBoxSelection(16);
}
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);
/mozilla/selsearch/trunk/src/chrome/content/selsearch.xul
0,0 → 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>
/mozilla/selsearch/trunk/src/chrome/.
Property changes:
Added: svn:mergeinfo