mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Send the text selection along with URL
This commit is contained in:
23
extension/content_script.js
Normal file
23
extension/content_script.js
Normal 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);
|
||||
@@ -27,6 +27,6 @@
|
||||
"popup": "popup.html"
|
||||
},
|
||||
"permissions": [
|
||||
"tabs", "http://chrometophone.appspot.com/*", "https://www.google.com/accounts/ServiceLogin"
|
||||
"tabs", "http://*/*", "https://*/*"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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()">
|
||||
|
||||
Reference in New Issue
Block a user