Send the text selection along with URL

This commit is contained in:
burke.davey
2010-05-22 21:18:05 +00:00
parent 3149070267
commit 1385db4dff
3 changed files with 42 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var additionalInfo = {
"title": document.title,
"url": document.location.href,
"selection": window.getSelection().toString()
};
chrome.extension.connect().postMessage(additionalInfo);

View File

@@ -27,6 +27,6 @@
"popup": "popup.html"
},
"permissions": [
"tabs", "http://chrometophone.appspot.com/*", "https://www.google.com/accounts/ServiceLogin"
"tabs", "http://*/*", "https://*/*"
]
}

View File

@@ -36,15 +36,16 @@ function loadHandler() {
chrome.tabs.getSelected(null, function(tab) {
if (tab.url.indexOf('http:') == 0 ||
tab.url.indexOf('https:') == 0) {
sendToPhone(tab.title, tab.url);
chrome.tabs.executeScript(null, {file: "content_script.js"});
} else {
document.getElementById('msg').innerHTML = chrome.i18n.getMessage('invalid_scheme_message');
}
});
}
function sendToPhone(title, url) {
var sendUrl = baseUrl + '?title=' + encodeURIComponent(title) + '&url=' + encodeURIComponent(url);
function sendToPhone(title, url, selection) {
var sendUrl = baseUrl + '?title=' + encodeURIComponent(title) +
'&url=' + encodeURIComponent(url) + '&sel=' + encodeURIComponent(selection);
req.open('GET', sendUrl, true);
req.onreadystatechange = function() {
@@ -67,6 +68,19 @@ function sendToPhone(title, url) {
req.send(null);
}
chrome.extension.onConnect.addListener(function(port) {
var tab = port.sender.tab;
// This will get called by the content script. We go through
// these hoops to get the optional text selection.
port.onMessage.addListener(function(info) {
var max_length = 1024;
if (info.selection.length > max_length) {
info.selection = info.selection.substring(0, max_length);
}
sendToPhone(info.title, info.url, info.selection);
});
});
</script>
<body onload="loadHandler()">