Commit efb166f0 authored by CrystalFaith's avatar CrystalFaith Committed by Commit Bot

[Extension Docs]Update Mappy 2.0

Mappy used deprecated APIs.
Updated sendRequest and onRequest to sendMessage and onMessage.

Bug: 808592
Change-Id: I52a24f12d16cfde1bab8f9729bfe996ba97a356d
Reviewed-on: https://chromium-review.googlesource.com/907214Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Crystal Lambert <crystallambert@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543814}
parent f739a39b
...@@ -2,43 +2,10 @@ ...@@ -2,43 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Global accessor that the popup uses. 'use strict'
var addresses = {};
var selectedAddress = null;
var selectedId = null;
function updateAddress(tabId) { chrome.runtime.onMessage.addListener(function(req, sender) {
chrome.tabs.sendRequest(tabId, {}, function(address) { chrome.storage.local.set({address: req.address})
addresses[tabId] = address; chrome.pageAction.show(sender.tab.id);
if (!address) { chrome.pageAction.setTitle({tabId: sender.tab.id, title: req.address});
chrome.pageAction.hide(tabId);
} else {
chrome.pageAction.show(tabId);
if (selectedId == tabId) {
updateSelected(tabId);
}
}
});
}
function updateSelected(tabId) {
selectedAddress = addresses[tabId];
if (selectedAddress)
chrome.pageAction.setTitle({tabId:tabId, title:selectedAddress});
}
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if (change.status == "complete") {
updateAddress(tabId);
}
});
chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
selectedId = tabId;
updateSelected(tabId);
});
// Ensure the current selected tab is set up.
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
updateAddress(tabs[0].id);
}); });
{ {
"name": "Mappy", "name": "Mappy",
"version": "0.6.1", "version": "1.0",
"description": "Finds addresses in the web page you're on and pops up a map window.", "description": "Finds addresses in the web page you're on and pops up a map window.",
"icons": { "128": "icon.png" }, "icons": { "128": "icon.png" },
"background": { "scripts": ["background.js"] }, "background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [ "content_scripts": [
{ "matches": ["http://*/*"], "js": ["mappy_content_script.js"] } {
"matches": ["http://*/*"],
"js": ["mappy_content_script.js"]
}
], ],
"permissions": [ "permissions": [
"tabs", "storage",
"https://maps.google.com/*", "https://maps.google.com/*",
"https://maps.googleapis.com/*" "https://maps.googleapis.com/*"
], ],
......
...@@ -2,50 +2,28 @@ ...@@ -2,50 +2,28 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// The background page is asking us to find an address on the page.
if (window == top) {
chrome.extension.onRequest.addListener(function(req, sender, sendResponse) {
sendResponse(findAddress());
});
}
// Search the text nodes for a US-style mailing address. // Search the text nodes for a US-style mailing address.
// Return null if none is found.
var findAddress = function() { let findAddress = function() {
var found; let found;
var re = /(\d+\s+[':.,\s\w]*,\s*[A-Za-z]+\s*\d{5}(-\d{4})?)/m; let re = /(\d+\s+[':.,\s\w]*,\s*[A-Za-z]+\s*\d{5}(-\d{4})?)/m;
var node = document.body; let node = document.body.textContent.match(re);
var done = false; if (document.body.textContent.match(re)) {
while (!done) { found = node;
done = true;
for (var i = 0; i < node.childNodes.length; ++i) {
var child = node.childNodes[i];
if (child.textContent.match(re)) {
node = child;
found = node;
done = false;
break;
}
}
} }
if (found) { if (found) {
var text = ""; let text = node;
if (found.childNodes.length) { let match = re.exec(text);
for (var i = 0; i < found.childNodes.length; ++i) {
text += found.childNodes[i].textContent + " ";
}
} else {
text = found.textContent;
}
var match = re.exec(text);
if (match && match.length) { if (match && match.length) {
console.log("found: " + match[0]); console.log('found: ' + match[0]);
var trim = /\s{2,}/g; let trim = /\s{2,}/g;
return match[0].replace(trim, " "); let address = match[0].replace(trim, ' ')
chrome.runtime.sendMessage({'address': address})
} else { } else {
console.log("bad initial match: " + found.textContent); console.log('bad initial match: ' + found.textContent);
console.log("no match in: " + text); console.log('no match in: ' + text);
} }
} }
return null;
} }
findAddress();
...@@ -2,12 +2,14 @@ ...@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var maps_key = "AIzaSyBa5aieunaIp3Obco-dNVYMdbnTZGAVkKQ"; 'use strict'
const kMaps_key = 'AIzaSyBa5aieunaIp3Obco-dNVYMdbnTZGAVkKQ';
function gclient_geocode(address) { function gclient_geocode(address) {
var url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + let url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' +
encodeURIComponent(address) + '&sensor=false'; encodeURIComponent(address) + '&sensor=false';
var request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open('GET', url, true); request.open('GET', url, true);
console.log(url); console.log(url);
...@@ -15,13 +17,13 @@ function gclient_geocode(address) { ...@@ -15,13 +17,13 @@ function gclient_geocode(address) {
console.log(request, e); console.log(request, e);
if (request.readyState == 4) { if (request.readyState == 4) {
if (request.status == 200) { if (request.status == 200) {
var json = JSON.parse(request.responseText); let json = JSON.parse(request.responseText);
var latlng = json.results[0].geometry.location; let latlng = json.results[0].geometry.location;
latlng = latlng.lat + ',' + latlng.lng; latlng = latlng.lat + ',' + latlng.lng;
var src = 'https://maps.googleapis.com/maps/api/staticmap?center=' + let src = 'https://maps.googleapis.com/maps/api/staticmap?center=' +
latlng + '&markers=' + latlng + '&zoom=14' + latlng + '&markers=' + latlng + '&zoom=14' +
'&size=512x512&sensor=false&key=' + maps_key; '&size=512x512&sensor=false&key=' + kMaps_key;
var map = document.getElementById('map'); let map = document.getElementById('map');
map.src = src; map.src = src;
map.addEventListener('click', function () { map.addEventListener('click', function () {
window.close(); window.close();
...@@ -35,9 +37,9 @@ function gclient_geocode(address) { ...@@ -35,9 +37,9 @@ function gclient_geocode(address) {
} }
function map() { function map() {
var address = chrome.extension.getBackgroundPage().selectedAddress; chrome.storage.local.get(['address'], function(value){
if (address) gclient_geocode(value.address);
gclient_geocode(address); })
} }
window.onload = map; window.onload = map;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment