Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firefox extension #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions browser-extension-firefox/addWhatsappLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
Code to be executed to convert simple tel: links to whatsapp links
**/

//Make sure assets/utility.js is loaded so we have access to _utility.getWhatsappMessageUrl
var logoImg = chrome.runtime.getURL('assets/whatsapp_logo.png');
var all = document.getElementsByTagName('a');
for (elem of all) {
let link = elem.href;
if (link && link.indexOf('tel:') !== -1) {
_utility.getWhatsappMessageUrl(link, function (whatsappMessageUrl) {
//Add a whatsapp icon with link to whatsapp message
elem.textContent =
'<a href="' +
whatsappMessageUrl +
'" target="_blank" style="margin: 0 auto;">' +
'<img src="' +
logoImg +
'" style="padding-right:12px;width:auto;height:16px;" /></a>' +
elem.innerHTML;
//let newWhatsappBtn = document.createElement('span')
//newWhatsappBtn.innerHTML = '<a href="'+whatsappLink+'" target="_blank">'+'<img src="'+logoImg+'" width="16px" height="16px" /></a>'
//elem.insertAdjacentHTML('beforebegin', newWhatsappBtn)
});
}
}
28 changes: 28 additions & 0 deletions browser-extension-firefox/assets/utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
Common code to be used in the project
**/

//ToDo:We can use better design pattern(s.a facade) here for extending this work to multiple message systems
const _utility = {
getWhatsappMessageUrl: function(phonestring, cb){
//Returns message url in the form of "https://api.whatsapp.com/send?phone=91232132&text=hi"
const MESSAGE_API_URL="https://api.whatsapp.com/send?phone="
chrome.storage.sync.get({'defaultCountryCode': 91, 'defaultMessage': ''}, function(configs){
if(!phonestring){
return cb("#NotAPhoneNumber");
}
//Format the selection - remove the non-digit characters
var phone = phonestring.replace(/\D+/g,'');
//Create the url with the given text and message api url
if(!phone || phone.length<10){
return cb("#NotAPhoneNumber");
} else if(phone.length<11){
return cb(MESSAGE_API_URL+configs.defaultCountryCode+phone+"&text="+encodeURIComponent(configs.defaultMessage));
} else if(phone.length<14){
return cb(MESSAGE_API_URL+phone+"&text="+encodeURIComponent(configs.defaultMessage));
} else {
return cb("#NotAPhoneNumber");
}
});
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser-extension-firefox/icons/favicon.ico
Binary file not shown.
Binary file added browser-extension-firefox/icons/logo_16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser-extension-firefox/icons/logo_192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser-extension-firefox/icons/logo_32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
378 changes: 378 additions & 0 deletions browser-extension-firefox/icons/safari-pinned-tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions browser-extension-firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "WhatsappAnywhere",
"browser_specific_settings": {
"gecko": {
"id": "",
"strict_min_version": "89.0.2"
}
},
"version": "1.0.0",
"description": "Turn any phone number into clickable whatsapp link",
"permissions": [
"contextMenus",
"storage"
],
"content_scripts": [{
"matches": ["*://*/*"],
"run_at": "document_idle",
"js": ["assets/utility.js","addWhatsappLink.js"]
}],
"background": {
"scripts": [
"assets/utility.js",
"whatsappLinkInContextMenu.js"
]
},
"icons": {
"16": "icons/logo_16x16.png",
"32": "icons/logo_32x32.png",
"192": "icons/logo_192x192.png"
},
"web_accessible_resources": [
"assets/*"
],
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"manifest_version": 2
}
50 changes: 50 additions & 0 deletions browser-extension-firefox/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>

<head>
<title>Whatsapp Anywhere Options</title>
<style>
body {
padding: 10px;
}

input {
width: 55px
}

span#status {
display: flex;
justify-content: flex-end;
}

div.option {
margin: 8px 0
}

div.option:first-of-type {
margin-top: 0;
}

div.option:last-of-type {
margin-bottom: 0;
}
</style>
</head>

<body>
<div class="option">
<b>Default Country Code</b><br/>
<input id="default-country-code" type="number" name="default-country-code" min="0" max="999" />
</div>
<br/>
<div class="option" title="Will only close the ad after the 'Skip ad' button has appeared (usually after 5 seconds).">
<b>Default Message</b><br/>
<textarea id="default-message" name=""default-message"" rows="5" cols="70"></textarea>
</div>
<span id="status">&nbsp;</span>
<button id="save">Save</button>

<script src="options.js"></script>
</body>

</html>
24 changes: 24 additions & 0 deletions browser-extension-firefox/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";
const saveOptions = function(){
var defaultCountryCode = document.getElementById('default-country-code').value;
var defaultMessage = document.getElementById('default-message').value;
chrome.storage.sync.set({
defaultCountryCode: defaultCountryCode,
defaultMessage: defaultMessage
}, function(){
const status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function(){
status.innerHTML = '&nbsp;';
}, 1000);
});
};
const restoreOptions = function(){
chrome.storage.sync.get({'defaultCountryCode': 91, 'defaultMessage': ''}, function(configs){
document.getElementById('default-country-code').value = configs.defaultCountryCode;
document.getElementById('default-message').value = configs.defaultMessage;
});
};
document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions);

18 changes: 18 additions & 0 deletions browser-extension-firefox/whatsappLinkInContextMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
Code to be executed for context menu functionality
**/

//Make sure assets/utility.js is loaded so we have access to _utility.getWhatsappMessageUrl
function getword(info,tab) {
_utility.getWhatsappMessageUrl(info.selectionText, function(whatsappMessageUrl){
chrome.tabs.create({
url: whatsappMessageUrl
});
})
}

chrome.contextMenus.create({
title: "Whatsapp: %s",
contexts:["selection"],
onclick: getword
});