-
- Version: MPL 1.1/GPL 2.0/LGPL 2.1 - - The contents of this file are subject to the Mozilla Public License Version - 1.1 (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - http://www.mozilla.org/MPL/ - - Software distributed under the License is distributed on an "AS IS" basis, - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - for the specific language governing rights and limitations under the - License. - - The Original Code is the Copy URL+. - - The Initial Developer of the Original Code is Philip Mateescu. - Portions created by the Initial Developer are Copyright (C) 2002-2003 - the Initial Developer. All Rights Reserved. - - Contributor(s): Philip Mateescu <xbiker @ yahoo.com> - Thanks to IMGTAG (Ted Mielczarek, Jedrek Kostecki) for unknowingly - allowing me to shamelessly borrow their clipboard copy code. - Contributor(s): Marc Meurrens <marc @ meurrens.com> http://www.meurrens.org/
- Modification by Raymond Yee to add selection of HTML.
- Alternatively, the contents of this file may be used under the terms of - either the GNU General Public License Version 2 or later (the "GPL"), or - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - in which case the provisions of the GPL or the LGPL are applicable instead - of those above. If you wish to allow use of your version of this file only - under the terms of either the GPL or the LGPL, and not to allow others to - use your version of this file under the terms of the MPL, indicate your - decision by deleting the provisions above and replace them with the notice - and other provisions required by the LGPL or the GPL. If you do not delete - the provisions above, a recipient may use your version of this file under - the terms of any one of the MPL, the GPL or the LGPL. - - ***** END LICENSE BLOCK *****/
/**
-
Reads Copy URL+ preferences
-
Code taken from: http://www.xulplanet.com/tutorials/xulqa/q_prefs.html
-
/
function cupGetPrefs(prefName) {
-
var prefs = Components.classes@mozilla.org/preferences-service;1.
-
getService(Components.interfaces.nsIPrefBranch);
-
// get the preferences based on language settings const nsISupportsString = Components.interfaces.nsISupportsString; prefVal = prefs.getComplexValue(prefName, nsISupportsString).data;
-
prefVal = null;
return prefVal;
}
/**
-
Writes Copy URL+ preferences
-
Code taken from: http://www.xulplanet.com/tutorials/xulqa/q_prefs.html
-
Contributed by motohiko - now this function always sets localized string.
-
/
function cupSetPrefs(prefName, prefVal) {
-
var prefs = Components.classes@mozilla.org/preferences-service;1.
-
getService(Components.interfaces.nsIPrefBranch);
-
createInstance(Components.interfaces.nsISupportsString);
}
/**
-
Returns the end of line char based on platform
-
/
function eol() {
-
platform = navigator.platform.toLowerCase(); if (platform.indexOf('win') != -1)
-
return "\r\n"; // the bor(g)ing platform
-
return "\r"; // apple
-
|| platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
-
return "\n"; // good boys
}
function copyToClipboard(copyThis) {
-
// testing prefs //~ { //~ s = ; //~ s += "\npref.1=" + cupGetPrefs('copyurlplus.menus.1.label'); //~ s += "\npref.5=" + cupGetPrefs('copyurlplus.menus.5.label'); //~ var menu = document.getElementById('copyurlplus-menu'); //~ var items = menu.firstChild.childNodes; //~ for (var j=0; j < items.length; j++) {
-
//~ s += "\nmenu." + j + "=" + items[j].label;
var str = Components.classes@mozilla.org/supports-string;1.createInstance(Components.interfaces.nsISupportsString); if (!str) return false; // couldn't get thingamajig str.data = copyThis; // unicode string?
var trans = Components.classes@mozilla.org/widget/transferable;1.createInstance(Components.interfaces.nsITransferable); if (!trans) return false; //no transferable widget found
trans.addDataFlavor("text/unicode"); trans.setTransferData("text/unicode", str, copyThis.length*2); // *2 cuz it's unicode
var clipid=Components.interfaces.nsIClipboard; var clip = Components.classes@mozilla.org/widget/clipboard;1.getService(clipid); if (!clip) return false; // couldn't get the clipboard
clip.setData(trans, null, clipid.kGlobalClipboard); return true;
}
function getCurrentBrowser() {
-
var latestBrowser = null; var windowMediator = Components.classes@mozilla.org/appshell/window-mediator;1
-
.getService(Components.interfaces.nsIWindowMediator);
if (browserWindow) {
-
latestBrowser = browserWindow.getBrowser();
}
function getCurrentSelection() {
-
var focusedWindow = document.commandDispatcher.focusedWindow; var selection = focusedWindow.getSelection.call(focusedWindow).toString();
return selection;
}
function getCurrentHTMLSelection() {
-
// borrowed from http://www.squarefree.com/bookmarklets/webdevel.html#partial_source
var sel = ""; try {
-
var x = document.createElement("div"); var focusedWindow = document.commandDispatcher.focusedWindow; var selection = focusedWindow.getSelection.call(focusedWindow); x.appendChild(selection.getRangeAt(0).cloneContents()); sel = x.innerHTML;
-
sel = "An exception occurred in the script. Error name: " + e.name + "Error message: " + e.message;
return sel
}
function getCurrentHTMLSelection2() {
-
// borrowed from http://www.squarefree.com/bookmarklets/webdevel.html#partial_source
var sel = ""; try {
-
var x = document.createElement("div"); x.appendChild(window.getSelection().getRangeAt(0).cloneContents()); sel = x.innerHTML;
-
sel = "An exception occurred in the script. Error name: " + e.name + "Error message: " + e.message;
return sel
}
function bulkReplace(inString, regexArray) {
-
var re; var ret = inString; for(var key in regexArray) {
-
re = new RegExp(key, 'g'); ret = ret.replace(re, regexArray[key]);
return ret;
}
/**
-
Returns the URL of the link the user right-clicked on or empty if no link
-
/
function getCurrentLink() {
-
if (gContextMenu && gContextMenu.onLink) {
-
return gContextMenu.linkURL();
-
return ;
}
function doCopyURLPlus(mode) {
-
var focusedWindow = document.commandDispatcher.focusedWindow;
var KEYS = new Array();
KEYS['%URL%'] = focusedWindow.location.href; KEYS['%TITLE%'] = focusedWindow.document.title; KEYS['%SEL%'] = getCurrentSelection(); KEYS['%RLINK%'] = getCurrentLink(); KEYS['%HTMLSEL%'] = getCurrentHTMLSelection2();
////// added by Meurrens /////// var ti = new Date() ; KEYS['%UTC_TIME%'] = ti.toUTCString() ; KEYS['%LOCAL_TIME%'] = ti.toLocaleString() ;
KEYS['%EOL%'] = eol() ;
/// too confusing to the user //~ KEYS['%TITLE_ESCAPED%'] = escape(title) ; //~ KEYS['%URL_ESCAPED%'] = escape(url) ; //~ KEYS['%RLINK_ESCAPED%'] = escape(link) ;
// don't really need it - use the htmlified version instead //~ KEYS['%TITLE_DOUBLEQUOTED%'] = '"' + title.replace(/"/g, '\\"' ) + '"' ; //~ KEYS['%URL_DOUBLEQUOTED%'] = '"' + url.replace(/"/g, '\\"' ) + '"' ; //~ KEYS['%RLINK_DOUBLEQUOTED%'] = '"' + link.replace(/"/g, '\\"' ) + '"' ;
var re_htmlify = new Array(); re_htmlify\\&(?!(\\w+[;]|#[0-9]+[;])) = '&\\1'; re_htmlify['"'] = '"'; re_htmlify['<'] = '<';
// proper HTML text KEYS['%SEL_HTMLIFIED%'] = bulkReplace(KEYS['%SEL%'], re_htmlify); KEYS['%TITLE_HTMLIFIED%'] = bulkReplace(KEYS['%TITLE%'], re_htmlify); KEYS['%RLINK_HTMLIFIED%'] = bulkReplace(KEYS['%RLINK%'], re_htmlify); KEYS['%URL_HTMLIFIED%'] = bulkReplace(KEYS['%URL%'], re_htmlify);
var copytext = ; switch (mode) {
-
case 'title':
-
copytext = KEYS['%TITLE%'] + KEYS['%EOL%'] + KEYS['%URL%']; break;
-
copytext = KEYS['%TITLE%'] + KEYS['%EOL%'] + '"' + KEYS['%SEL%'] + '"' + KEYS['%EOL%'] + KEYS['%URL%']; break;
-
copytext = '"' + KEYS['%SEL%'] + '"' + KEYS['%EOL%'] + KEYS['%URL%']; break;
-
if (mode.indexOf('user') == 0) {
-
var j = mode.substr(mode.indexOf('-') + 1); if (!(action = cupGetPrefs('copyurlplus.menus.' + j + '.copy'))) {
-
// kaboom! alert('Entry "copyurlplus.menus.' + j + '.copy" not found!' +
-
"\nCheck your user.js settings");
-
// it's alright now, baby, it is... al-right now...
-
alert('Unknown copy mode requested:'
-
+ "\n mode=" + mode + + "\n title=" + title + "\n url=" + url + "\n sel=" + sel
-
//~ + "\n
"
-
//~ + "\n mode=" + mode //~ + "\n title=" + title //~ + "\n url=" + url //~ + "\n sel=" + sel
-
//~ );
}
/**
-
startup functions
-
/
var copyurlplusMenuItems = new Array('copyurlplus-sel', 'copyurlplus-titlesel');
function copyurlplusContext() {
-
if (gContextMenu) {
-
//~ show = (gContextMenu.onLink && (fetchLink())); show = gContextMenu.isTextSelected; for (var i=0; i<copyurlplusMenuItems.length; i++) {
-
var menuitem = document.getElementById(copyurlplusMenuItems[i]); if (menuitem)
-
menuitem.hidden = !show;
}
function copyurlplusAddMenu(menu, prefPos, label) {
-
var pop = menu.firstChild;
var newMenu = document.createElement('menuitem'); newMenu.setAttribute('label', label); newMenu.setAttribute('id', 'copyurlplus-' + prefPos); newMenu.setAttribute('oncommand', "doCopyURLPlus('user-" + prefPos + "')"); pop.appendChild(newMenu);
}
/**
-
Creates all custom menus from user.js.
-
menu entries in user.js will look like this:
-
user_pref('copyurlplus.menus.1.label', 'Copy URL + Title (Wiki)');
-
user_pref('copyurlplus.menus.1.copy', '[[%URL% %TITLE%]]');
-
where 1 is an incremental value, .label is the label of the menu
-
and .copy is the format. with %URL% being the current document's URL,
-
%TITLE% its title and %SEL% the current selection
-
/
function copyurlplusCreateMenus() {
-
var menu1 = document.getElementById('copyurlplus-menu'); var menu2 = document.getElementById('copyurlplus-menu-edit');
var j = 1; while(1) {
-
label = cupGetPrefs('copyurlplus.menus.' + j + '.label'); if (!label) break;
if (menu1) copyurlplusAddMenu(menu1, j, label); if (menu2) copyurlplusAddMenu(menu2, j, label); j++;
}
function init() {
-
var menu = document.getElementById("contentAreaContextMenu"); menu.addEventListener("popupshowing", copyurlplusContext, false); copyurlplusCreateMenus();
}
window.addEventListener("load", init, false);
