Permalink
Browse files

Added context menu

  • Loading branch information...
1 parent a09dc37 commit 39ed1cdbe558cd6002200ac7ee6c4b6d59466f7e @hnakamur hnakamur committed Aug 20, 2011
Showing with 70 additions and 40 deletions.
  1. +1 −0 LICENCE
  2. +65 −8 extension/background.html
  3. +2 −2 extension/manifest.json
  4. +2 −30 extension/popup.html
View
@@ -1,4 +1,5 @@
Copyright 2009 ku ku0522a*gmail.com. All rights reserved.
+Copyright 2011 Hiroaki Nakamura <hnakamur@gmail.com>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@@ -8,17 +8,21 @@
// <![CDATA[
var useNPAPI = false;
+ function copyToClipboard(text) {
+ var proxy = document.getElementById('clipboard_object');
+ if ( useNPAPI )
+ proxy.set(text);
+ else {
+ proxy.value = text;
+ proxy.select();
+ document.execCommand("copy");
+ }
+ }
+
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if ( request.command == 'setClipboard' ) {
- var proxy = document.getElementById('clipboard_object');
- if ( useNPAPI )
- proxy.set(request.data);
- else {
- proxy.value = request.data;
- proxy.select();
- document.execCommand("copy");
- }
+ copyToClipboard(request.data);
}
}
);
@@ -43,6 +47,36 @@
}
return formats;
}
+ function escapeHTML(text) {
+ return text ? text.replace(/[&<>'"]/g, convertHTMLChar) : text;
+ }
+ function convertHTMLChar(c) { return charMap[c]; }
+ var charMap = {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ "'": '&apos;',
+ '"': '&quot;'
+ };
+ function showPrompt(text, pos, subject) {
+ var msg = "Please enter the input text for \n" + subject;
+ var s = window.prompt(msg);
+ return (s === null) ? "" : s;
+ }
+ CreateLink.prototype.formatLinkText = function (formatId, url, text, title) {
+ return this.formats[formatId].format.
+ replace(/%url%/g, url).
+ replace(/%text%/g, text.replace(/\n/g, ' ')).
+ replace(/%text_n%/g, text).
+ replace(/%text_br%/g, text.replace(/\n/g, '<br />\n')).
+ replace(/%title%/g, title).
+ replace(/%newline%/g, '\n').
+ replace(/%htmlEscapedText%/g, escapeHTML(text)).
+ // TODO: window.prompt does not respond in popup window.
+ //replace(/%input%/g, showPrompt).
+ replace(/\\t/g, '\t').
+ replace(/\\n/g, '\n');
+ }
function instance() {
if ( !window.__instance ) {
@@ -51,6 +85,29 @@
return window.__instance;
}
+function onMenuItemClick(info, tab) {
+ /*alert("info: " + JSON.stringify(info) + "\n" +
+ "tab: " + JSON.stringify(tab));*/
+ var formatId = parseInt(info.menuItemId) - 1;
+ var url = info.linkUrl ? info.linkUrl : info.pageUrl;
+ var text = info.selectionText ? info.selectionText : tab.title;
+ var title = tab.title;
+ var linkText = instance().formatLinkText(formatId, url, text, title);
+ copyToClipboard(linkText);
+}
+var formats = instance().formats;
+if (formats.length == 1) {
+ chrome.contextMenus.create({"title": "Copy Link as " + formats[0].label,
+ "contexts": ["all"],
+ "onclick": onMenuItemClick});
+} else {
+ for (var i = 0; i < formats.length; ++i) {
+ chrome.contextMenus.create({"title": formats[i].label,
+ "contexts": ["all"],
+ "onclick": onMenuItemClick});
+ }
+}
+
// ]]>
</script>
<body>
@@ -21,6 +21,6 @@
"run_at" : "document_end",
"all_frames" : true
}],
- "permissions": [ "tabs" ],
- "version": "0.0.7"
+ "permissions": [ "tabs", "contextMenus" ],
+ "version": "0.0.13"
}
View
@@ -52,18 +52,8 @@
chrome.tabs.sendRequest(tab.id, "getSelection", function(response) {
// see chrome://makelink/content/help/defininglinktypes.html
var text = response.length > 0 ? response : tab.title;
- var data = def.format.
- replace(/%url%/g, tab.url).
- replace(/%text%/g, text.replace(/\n/g, ' ')).
- replace(/%text_n%/g, text).
- replace(/%text_br%/g, text.replace(/\n/g, '<br />\n')).
- replace(/%title%/g, tab.title).
- replace(/%newline%/g, '\n').
- replace(/%htmlEscapedText%/g, escapeHTML(text)).
- // TODO: window.prompt does not respond in popup window.
- //replace(/%input%/g, showPrompt).
- replace(/\\t/g, '\t').
- replace(/\\n/g, '\n');
+ var data = chrome.self.getBackgroundPage().instance().formatLinkText(
+ n, tab.url, text, tab.title);
chrome.extension.sendRequest({
command: 'setClipboard',
@@ -74,24 +64,6 @@
}
}
- function escapeHTML(text) {
- return text ? text.replace(/[&<>'"]/g, convertHTMLChar) : text;
- }
- function convertHTMLChar(c) { return charMap[c]; }
- var charMap = {
- '&': '&amp;',
- '<': '&lt;',
- '>': '&gt;',
- "'": '&apos;',
- '"': '&quot;'
- };
-
- function showPrompt(text, pos, subject) {
- var msg = "Please enter the input text for \n" + subject;
- var s = window.prompt(msg);
- return (s === null) ? "" : s;
- }
-
function setupEventHandlers() {
document.addEventListener( 'mouseup', function (ev) {
chrome.windows.getCurrent(function (w) {

0 comments on commit 39ed1cd

Please sign in to comment.