Skip to content

Instantly share code, notes, and snippets.

@erwan
Created August 31, 2010 13:31
Show Gist options
  • Save erwan/559016 to your computer and use it in GitHub Desktop.
Save erwan/559016 to your computer and use it in GitHub Desktop.
Chrome Screencast 2
<script>
// Global variable accessed by the popup
var telList;
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
telList = request["phones"];
chrome.pageAction.show(sender.tab.id);
sendResponse({});
});
</script>
var phoneRe = /^\s*((\d{2}\s?){5})\s*$/;
var texts = document.evaluate(".//text()[normalize-space(.)!='']", document.body, null, 6, null);
var phones = [];
for (var i = 0; i < texts.snapshotLength; i++) {
var textNode = texts.snapshotItem(i);
var text = textNode.textContent;
var m = phoneRe.exec(text);
if (m != null) {
var newText = document.createElement("a");
newText.setAttribute("href", "callto:" + m[1]);
newText.appendChild(document.createTextNode(m[0]));
textNode.parentNode.replaceChild(newText, textNode);
phones.push(m[1]);
}
}
if (phones.length > 0) {
chrome.extension.sendRequest({"phones": phones}, function(response) {});
}
{
"name": "Telify",
"version": "0.2",
"description": "Click to call",
"background_page": "background.html",
"content_scripts": [{
"js": ["contentScript.js"],
"matches": ["http://*/*"]
}],
"permissions": [
"tabs",
"http://ajax.googleapis.com/"
],
"page_action": {
"default_icon": "icons/tel.gif",
"default_popup": "popup.html"
}
}
<!DOCTYPE html>
<html>
<style>
body {
width: 200px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
var telList = chrome.extension.getBackgroundPage().telList;
if (telList && telList.length > 0) {
for (i in telList)
$("ul").append("<li><a href='callto:" + telList[i] + "'>" + telList[i] + "</a></li>");
}
});
</script>
<body>
<ul></ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment