From 6abd44a267e445ed6b72b7786cf10d9f9255504b Mon Sep 17 00:00:00 2001 From: "rich.tibbett" Date: Sun, 12 Dec 2010 02:08:48 +0000 Subject: [PATCH] Opera to Phone - Version 1.3: general bug fixes --- third_party/operatophone/config.xml | 10 +- third_party/operatophone/constants.js | 14 +- .../operatophone/includes/operatophone.js | 102 ++++++++------ third_party/operatophone/index.html | 129 +++++++----------- third_party/operatophone/popup.html | 105 ++++++-------- third_party/operatophone/send_logic.js | 19 ++- 6 files changed, 178 insertions(+), 201 deletions(-) diff --git a/third_party/operatophone/config.xml b/third_party/operatophone/config.xml index b53300d..c66a5ff 100644 --- a/third_party/operatophone/config.xml +++ b/third_party/operatophone/config.xml @@ -1,9 +1,9 @@ - - Opera to Phone - This extension adds a button to Opera that lets you seamlessly push links, maps, youtube videos, phone numbers and text to copy to the clipboard directly to your Android device. You also need to install the Chrome to Phone Android application on your phone. The application can be downloaded from Android Market (search for 'Chrome to Phone'). Requires a mobile phone running Android 2.2 ("Froyo") or later. - Rich Tibbett - + + Opera to Phone + Seamlessly push links, maps, youtube videos, phone numbers and text directly to your Android device. Requires a mobile phone running Android 2.2+ and the 'Chrome to Phone' application. + Rich Tibbett + Copyright 2010 Opera Software ASA. diff --git a/third_party/operatophone/constants.js b/third_party/operatophone/constants.js index e37f833..00a4d67 100644 --- a/third_party/operatophone/constants.js +++ b/third_party/operatophone/constants.js @@ -8,25 +8,23 @@ var apiVersion = 5; var baseUrl = 'https://chrometophone.appspot.com'; var sendUrl = baseUrl + '/send?ver=' + apiVersion; var signInUrl = baseUrl + '/signin?extret=' + -encodeURIComponent('http://code.google.com/p/chrometophone/logo') + '?login'; +encodeURIComponent('http://code.google.com/p/chrometophone/logo') + '?login&ver=' + apiVersion;; var signOutUrl = baseUrl + '/signout?extret=' + -encodeURIComponent('http://code.google.com/p/chrometophone/logo') + '?logout'; +encodeURIComponent('http://code.google.com/p/chrometophone/logo') + '?logout&ver=' + apiVersion;; var registerUrl = baseUrl + '/register?ver=' + apiVersion; var apkUrl = 'http://code.google.com/p/chrometophone/wiki/AndroidApp'; var ACTION_START_SEND = 'start_send_process'; var ACTION_SEND_PAGE = 'send_to_phone'; var ACTION_APK_REQUIRED = 'apk_required'; -var ACTION_REGISTER_USERJS = 'register_userjs'; -var ACTION_DEREGISTER_USERJS = 'deregister_userjs'; var ACTION_CAPTURE_SELECTION = 'capture_selection'; -var ACTION_SELECTION_CAPTURED = 'selection_captured'; +var ACTION_OPEN_TAB = 'open_tab'; +var ACTION_CLOSE_TAB = 'close_tab'; var STATUS_SUCCESS = 'success'; var STATUS_LOGIN_REQUIRED = 'login_required'; var STATUS_DEVICE_NOT_REGISTERED = 'device_not_registered'; -var STATUS_HTTP_PAGES_ONLY = 'http_pages_only'; -var STATUS_GENERAL_ERROR = 'general_error'; var STATUS_NO_TAB_ACCESS = 'no_tab_access'; +var STATUS_GENERAL_ERROR = 'general_error'; -var BROWSER_CHANNEL_RETRY_INTERVAL_MS = 10000 * (1 + Math.random() - 0.5); +var BROWSER_CHANNEL_RETRY_INTERVAL_MS = 10000 * (1 + Math.random() - 0.5); \ No newline at end of file diff --git a/third_party/operatophone/includes/operatophone.js b/third_party/operatophone/includes/operatophone.js index 609f6d4..4c3c2fb 100644 --- a/third_party/operatophone/includes/operatophone.js +++ b/third_party/operatophone/includes/operatophone.js @@ -1,64 +1,76 @@ // ==UserScript== -// @include * +// @include http://* +// @include https://* // ==/UserScript== -// obtain access to all tabs - -var ACTION_REGISTER_USERJS = 'register_userjs'; -var ACTION_DEREGISTER_USERJS = 'deregister_userjs'; var ACTION_CAPTURE_SELECTION = 'capture_selection'; var ACTION_SEND_PAGE = 'send_to_phone'; +var ACTION_CLOSE_TAB = 'close_tab'; -function S4() { - return (((1+Math.random())*0x10000)|0).toString(16).substring(1); -} -function guid() { - return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); -} +var currentUrl = document.location.href; -if(!window.top.userJS_UUID) { - window.top.userJS_UUID = guid(); - - window.addEventListener('focus', function(){ - opera.extension.postMessage({action: ACTION_REGISTER_USERJS, uuid: window.top.userJS_UUID}); - }, false); - - window.addEventListener('blur', function(){ - opera.extension.postMessage({action: ACTION_DEREGISTER_USERJS, uuid: window.top.userJS_UUID}); - }, false); - - // register this tab as the active userjs onload - opera.extension.postMessage({action: ACTION_REGISTER_USERJS, uuid: window.top.userJS_UUID}); -} - -opera.extension.addEventListener('message', function(request) { - - //opera.postError('Callback received at UserJS: ' + request.data.action); - - if(request.data.action==ACTION_CAPTURE_SELECTION - && request.data.uuid==window.top.userJS_UUID) { - - var currentUrl = document.location.href; - +opera.extension.addEventListener( 'message', function( message ) { + if( message.data.action === ACTION_CAPTURE_SELECTION ) { var pageInfo = { action: ACTION_SEND_PAGE, data: { - link: encodeURIComponent(currentUrl), - title: encodeURIComponent(document.title), - selection: encodeURIComponent(window.getSelection()) + link: currentUrl, + title: document.title, + selection: window.getSelection() ? window.getSelection().toString() : null } }; - // URL overrides - if (currentUrl.match(/^http[s]?:\/\/maps\.google\./) || - currentUrl.match(/^http[s]?:\/\/www\.google\.[a-z]{2,3}(\.[a-z]{2})\/maps/)) { + if ( currentUrl.match( /^http[s]?:\/\/maps\.google\./i ) || + currentUrl.match( /^http[s]?:\/\/www\.google\.[a-z]{2,3}(\.[a-z]{2})\/maps/i ) ) { var link = document.getElementById('link'); if (link && link.href) - pageInfo.data.link = encodeURIComponent(link.href); + pageInfo.data.link = link.href; } - - opera.extension.postMessage(pageInfo); + opera.extension.postMessage( pageInfo ); } }, false); -//opera.postError("UserJS loaded: " + window.location.href + " / " + window.top.userJS_UUID); \ No newline at end of file +function findAndReplace(searchText, replacement, searchNode) { + if (!searchText || typeof replacement === 'undefined') { + // Throw error here if you want... + return; + } + var regex = typeof searchText === 'string' ? + new RegExp(searchText, 'g') : searchText, + childNodes = (searchNode || document.body).childNodes, + cnLength = childNodes.length, + excludes = 'html,head,style,title,link,meta,script,object,iframe'; + while (cnLength--) { + var currentNode = childNodes[cnLength]; + if (currentNode.nodeType === 1 && + (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) { + arguments.callee(searchText, replacement, currentNode); + } + if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) { + continue; + } + var parent = currentNode.parentNode, + frag = (function(){ + var html = currentNode.data.replace(regex, replacement), + wrap = document.createElement('div'), + frag = document.createDocumentFragment(); + wrap.innerHTML = html; + while (wrap.firstChild) { + frag.appendChild(wrap.firstChild); + } + return frag; + })(); + parent.insertBefore(frag, currentNode); + parent.removeChild(currentNode); + } +} +window.addEventListener( 'DOMContentLoaded', function() { + if( currentUrl.match( /^http[s]?:\/\/www\.google\.com\/accounts\/ServiceLogin\?(.*)?ahname=Chrome\+to\+Phone(.*)?$/i ) ) { + // Opera log in message so users know what they are logging in to. + findAndReplace('Chrome', 'Opera', document.body); + } else if (currentUrl.match( /^http:\/\/code\.google\.com\/p\/chrometophone\/logo(.*)?$/i )) { + opera.extension.postMessage({ + action: ACTION_CLOSE_TAB + }); + } +}, false); diff --git a/third_party/operatophone/index.html b/third_party/operatophone/index.html index 6db3e47..0908c11 100644 --- a/third_party/operatophone/index.html +++ b/third_party/operatophone/index.html @@ -4,7 +4,6 @@ diff --git a/third_party/operatophone/popup.html b/third_party/operatophone/popup.html index 324b990..b2a2d28 100644 --- a/third_party/operatophone/popup.html +++ b/third_party/operatophone/popup.html @@ -7,77 +7,62 @@ body { min-width: 320px; overflow: hidden; } - +a, a:visited, a:active { + color: blue; +} td { - font-family: verdana; + font-family: Verdana, Arial; font-size: 12px; color: black; } - - +