diff --git a/extension/_locales/en/messages.json b/extension/_locales/en/messages.json index 04b49ea..1218e72 100644 --- a/extension/_locales/en/messages.json +++ b/extension/_locales/en/messages.json @@ -138,5 +138,27 @@ "signed_out_message": { "message": "You are now signed out of Chrome to Phone.", "description": "Signed out message." + }, + "request_message": { + "message": "The page at $domain$ wants to send content to your phone.", + "description": "Confirmation when a page initiates a phone action. $domain$ is automatically replaced with the domain.", + "placeholders": { + "domain": { + "content": "$1", + "example": "google.com" + } + } + }, + "request_deny_message": { + "message": "Deny", + "description": "Deny a page's request to send content to your phone." + }, + "request_allow_message": { + "message": "Allow", + "description": "Allow a page's request to send content to your phone." + }, + "request_approve_message": { + "message": "Allow Forever", + "description": "Allow a page's request - and all subsequent requests - to send content to your phone." } } diff --git a/extension/background.html b/extension/background.html index 708f5a9..6b111c4 100644 --- a/extension/background.html +++ b/extension/background.html @@ -23,22 +23,32 @@ function onClickHandler(info, tab) { var url = info.srcUrl; if (url == undefined) url = info.linkUrl; - if (url == undefined) url = tab.url; + if (url == undefined) url = info.url || tab.url; var msgType = info.mediaType; if (msgType == undefined) msgType = 'page'; - var sel = info.selectionText; - if (sel == undefined) sel = ''; + var sel = info.selectionText || ''; var bg = chrome.extension.getBackgroundPage(); - bg.sendToPhone(tab.title, url, msgType, sel, function(status) { + bg.sendToPhone(info.title || tab.title, url, msgType, sel, function(status) { if (status == STATUS_LOGIN_REQUIRED) { chrome.tabs.create({url: signInUrl}); } }); } +function isWhitelisted(domain) { + var domains = JSON.parse(localStorage.getItem('whitelist')) || {}; + return domains[domain]; +} + +function addToWhitelist(domain) { + var domains = JSON.parse(localStorage.getItem('whitelist')) || {}; + domains[domain] = true; + localStorage.setItem('whitelist', JSON.stringify(domains)); +} + if (chrome.contextMenus) { chrome.contextMenus.create({'title': chrome.i18n.getMessage('app_name_short'), 'documentUrlPatterns': [ 'http://*/*', 'https://*/*' ], @@ -46,7 +56,28 @@ if (chrome.contextMenus) { 'contexts': ['link', 'selection', 'image', 'video', 'audio']}); } -//initializeBrowserChannel(); +var lastRequest; + +// Handle content script requests. +chrome.extension.onRequest.addListener( + function(info, sender, respond) { + var msgType = (info.selection && info.selection.length > 0) ? 'selection' : 'page'; + var domain = info.url.match(/:\/\/(.[^/]+)/)[1]; + + if (isWhitelisted(domain)) { + sendToPhone(info.title, info.url, msgType, info.selection, function(status) { + alert('sent to phone!'); + if (status == STATUS_LOGIN_REQUIRED) { + chrome.tabs.create({url: signInUrl}); + } + }); + } else { + var notification = webkitNotifications.createHTMLNotification('notification.html'); + lastRequest = info; + notification.show(); + } + respond(); + }); diff --git a/extension/content_script.js b/extension/content_script.js index 18860dc..a630e7c 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -14,19 +14,39 @@ * limitations under the License. */ -var pageInfo = { - "url": document.location.href, - "title": document.title, - "selection": window.getSelection().toString() -}; +function getPageInfo() { + var pageInfo = { + "url": document.location.href, + "title": document.title, + "selection": window.getSelection().toString() + }; -// URL overrides -if (pageInfo.url.match(/^http[s]?:\/\/maps\.google\./) || - pageInfo.url.match(/^http[s]?:\/\/www\.google\.[a-z]{2,3}(\.[a-z]{2})\/maps/)) { - var link = document.getElementById('link'); - if (link && link.href) { - pageInfo.url = link.href; + // URL overrides. + if (pageInfo.url.match(/^http[s]?:\/\/maps\.google\./) || + pageInfo.url.match(/^http[s]?:\/\/www\.google\.[a-z]{2,3}(\.[a-z]{2})\/maps/)) { + var link = document.getElementById('link'); + if (link && link.href) { + pageInfo.url = link.href; + } } + return pageInfo; } -chrome.extension.connect().postMessage(pageInfo); +// Respond to extension requests with the current page info. +chrome.extension.onRequest.addListener( + function(request, sender, sendResponse) { + if (request == 'run') { + sendResponse(getPageInfo()); + } + }); + +// Allow pages to initiate sending as well. +var element = document.createElement('sendtophone'); +element.style.display='none'; +element.style.background='url(\'' + chrome.extension.getURL("icon_16.png") + '\')'; +element.addEventListener('sendToPhone', function() { + var pageInfo = getPageInfo(); + pageInfo.selection = element.innerText; + chrome.extension.sendRequest(pageInfo, function(response) {}); +}); +document.head.appendChild(element); diff --git a/extension/manifest.json b/extension/manifest.json index 2fa0b7c..7412753 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -16,7 +16,7 @@ }, "background_page": "background.html", "permissions": [ - "contextMenus", "tabs", "http://*/*", "https://*/*" + "contextMenus", "notifications", "tabs", "http://*/*", "https://*/*" ], "content_scripts": [ { diff --git a/extension/notification.html b/extension/notification.html new file mode 100644 index 0000000..7c994b2 --- /dev/null +++ b/extension/notification.html @@ -0,0 +1,93 @@ + + + + + + + + + + + + +
+
+
+

+ + + +

+
+ diff --git a/extension/popup.html b/extension/popup.html index 8097619..2723b8d 100644 --- a/extension/popup.html +++ b/extension/popup.html @@ -32,6 +32,9 @@ td {