Skip to content

Instantly share code, notes, and snippets.

@tshm
Last active April 4, 2016 23:10
Show Gist options
  • Save tshm/02c215b7266d63130004deb473c7afb9 to your computer and use it in GitHub Desktop.
Save tshm/02c215b7266d63130004deb473c7afb9 to your computer and use it in GitHub Desktop.
gv bookmarklet dev.
// use following converter:
// http://chriszarate.github.io/bookmarkleter/
var d = document;
var re = new RegExp(/(\+\d{1,2})?([ ()-]{0,2}\d{3,4}){2,3}\b/);
var createLink = function(txt) {
var a = d.createElement('a');
a.setAttribute('class', '_ph_');
a.href='#';
a.setAttribute('data-clipboard-text', txt);
a.innerHTML = txt;
return a;
};
var embedLink = function(node) {
var match = re.exec(node.data);
if (!match) return;
//console.log(node.data, match, match.index);
node.splitText(match.index);
node.nextSibling.splitText(match[0].length);
node.parentNode.replaceChild(createLink(match[0]), node.nextSibling);
};
var ignore = { 'a':1, 'style':1, 'script':1, 'iframe':1, 'button':1 };
var substitude = function(el) {
//console.log(el, el.childNodes);
[].forEach.call(el.childNodes, function(node) {
if (node.nodeType == 3) { // TEXT_NODE
embedLink( node );
return;
}
if (1 != ignore[node.nodeName.toLowerCase()]) substitude(node);
});
}
//console.log(rr, nn);
var openGV = function(ev) {
var a;
setTimeout(function() {
window.open('https://www.google.com/voice/b/0/#inbox');
}, 0);
return false;
};
var script = d.createElement('script');
script.src = 'https://cdn.jsdelivr.net/clipboard.js/1.5.10/clipboard.min.js';
script.onload = function() {
clipboard = new Clipboard('._ph_');
//clipboard.on('success', function(e) { console.log('success ', e); });
//console.log(clipboard);
}
d.querySelector('head').appendChild(script);
substitude(d.body);
[].forEach.call(d.querySelectorAll('._ph_'), function(e) {
e.addEventListener('click', openGV);
});
<html>
<ul>
<p><button id="st">test</button>
</p>
<li>+1-877-870-3723
<li>203-539-1129
<li>(203)539-1129
<li>203 539 1129
<li>phone number is: 203 539 1129 .OK?
</ul>
<script>
var b = document.querySelector('#st');
b.addEventListener('click', function() {
var script = document.createElement('script');
script.src = './gv.js';
document.querySelector('head').appendChild(script);
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment