diff --git a/background_scripts/actions.js b/background_scripts/actions.js index 0c8ff179..17d7f67d 100644 --- a/background_scripts/actions.js +++ b/background_scripts/actions.js @@ -35,6 +35,15 @@ Actions = (function() { o.callback(o.sender.tab.url); }; + _.getRootUrlObj = function(o) { + o.callback({ + a: { + '@href': o.sender.tab.url, + '_': o.sender.tab.title + } + }); + }; + _.viewSource = function(o) { o.url = 'view-source:' + o.sender.tab.url; _.openLink(o); @@ -341,6 +350,10 @@ Actions = (function() { Clipboard.copy(o.request.text); }; + _.copyHtmlFormatted = function(o) { + Clipboard.copyHtmlFormatted(o.request.nodeObj); + }; + _.goToTab = function(o) { var id = o.request.id, index = o.request.index; chrome.tabs.query({currentWindow: true}, function(tabs) { diff --git a/background_scripts/clipboard.js b/background_scripts/clipboard.js index 89b50396..b4dfac9f 100644 --- a/background_scripts/clipboard.js +++ b/background_scripts/clipboard.js @@ -16,6 +16,34 @@ Clipboard.copy = function(text) { document.body.removeChild(t); }; +Clipboard.copyHtmlFormatted = function(nodeObj) { + var convertToNode = function(parent, nodeObj) { + for (var key in nodeObj) { + if (key.indexOf('@') === 0) { + parent.setAttribute(key.substr(1), nodeObj[key]); + } else if (key === '_') { + var child = document.createTextNode(nodeObj[key]); + parent.appendChild(child); + } else { + var tag = document.createElement(key); + parent.appendChild(convertToNode(tag, nodeObj[key])); + } + } + return parent; + }; + + var wrapper = document.createElement('div'); + var node = convertToNode(wrapper, nodeObj); + document.body.appendChild(node); + var r = document.createRange(); + r.selectNode(wrapper); + var s = window.getSelection(); + s.removeAllRanges(); + s.addRange(r); + document.execCommand('Copy'); + wrapper.remove(); +}; + Clipboard.paste = function() { var t = this.createTextArea(); document.body.appendChild(t); diff --git a/content_scripts/clipboard.js b/content_scripts/clipboard.js index 7265f258..4b8b68f3 100644 --- a/content_scripts/clipboard.js +++ b/content_scripts/clipboard.js @@ -8,6 +8,9 @@ var Clipboard = { } RUNTIME('copy', {text: this.store}); }, + copyHtmlFormatted: function(nodeObj) { + RUNTIME('copyHtmlFormatted', {nodeObj: nodeObj}); + }, paste: function(tabbed) { var engineUrl = Complete.getEngine(settings.defaultengine); engineUrl = engineUrl ? engineUrl.requestUrl : diff --git a/content_scripts/mappings.js b/content_scripts/mappings.js index 36455caf..778585f9 100644 --- a/content_scripts/mappings.js +++ b/content_scripts/mappings.js @@ -445,6 +445,12 @@ Mappings.actions = { Status.setMessage(url, 2); }); }, + yankDocumentUrlHtmlFormatted: function() { + RUNTIME('getRootUrlObj', function(nodeObj) { + Clipboard.copyHtmlFormatted(nodeObj); + Status.setMessage('copied', 2); + }); + }, yankFrameUrl: function() { Clipboard.copy(document.URL); Status.setMessage(document.URL, 2);