Skip to content

Commit

Permalink
merge branch 'webext' by @Treora towards #16
Browse files Browse the repository at this point in the history
  • Loading branch information
bnvk committed Dec 21, 2017
2 parents 8f32cd7 + e3958fb commit 1414783
Show file tree
Hide file tree
Showing 26 changed files with 1,075 additions and 153 deletions.
100 changes: 54 additions & 46 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var startTime = null;

var updateTimeOnPageInterval = 1000 * 10; // 10 seconds // 1 minute.

chrome.alarms.onAlarm.addListener(function( alarm ) {
browser.alarms.onAlarm.addListener(function( alarm ) {
if(localStorage['automaticDonate'] == "true"){
var val = '',
address = '',
Expand All @@ -22,8 +22,8 @@ chrome.alarms.onAlarm.addListener(function( alarm ) {
window.alarmManager.doToggleAlarm();

db.clear('sites');
chrome.browserAction.setBadgeBackgroundColor({color:'#5bc0de'});
chrome.browserAction.setBadgeText({text: 'Sent!'});
browser.browserAction.setBadgeBackgroundColor({color:'#5bc0de'});
browser.browserAction.setBadgeText({text: 'Sent!'});
}, function(error){
// If doToggleAlarm() is not called, the alarm will fire every 2 hours after the set period.
// Maybe the balance is too low, or blockcypher.com is not working, or there are no
Expand All @@ -32,22 +32,22 @@ chrome.alarms.onAlarm.addListener(function( alarm ) {
window.alarmManager.doToggleAlarm();

db.clear('sites');
chrome.browserAction.setBadgeBackgroundColor({color:'#5bc0de'});
chrome.browserAction.setBadgeText({text: 'Sent!'});
browser.browserAction.setBadgeBackgroundColor({color:'#5bc0de'});
browser.browserAction.setBadgeText({text: 'Sent!'});
console.log(error.message);
});
});
} else if (localStorage['manualRemind'] == 'true') {
localStorage['weeklyAlarmReminder'] = true;
chrome.browserAction.setBadgeBackgroundColor({color:'#9BDBFC'});
chrome.browserAction.setBadgeText({text: '....'});
browser.browserAction.setBadgeBackgroundColor({color:'#9BDBFC'});
browser.browserAction.setBadgeText({text: '....'});
}
});

chrome.runtime.onInstalled.addListener(function(details){
browser.runtime.onInstalled.addListener(function(details){
//if(details.reason == "update"){
preferences.convert();
var thisVersion = chrome.runtime.getManifest().version;
var thisVersion = browser.runtime.getManifest().version;
console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
//}
});
Expand All @@ -56,24 +56,24 @@ window.addEventListener("storage", function(e){
// Let the user see their available balance in the browserAction
// Doesn't have to be super accurate. Certainly don't need to hit
// an API call constantly.
chrome.browserAction.setBadgeBackgroundColor({color:'#dddddd'});
browser.browserAction.setBadgeBackgroundColor({color:'#dddddd'});
if(localStorage['availableBalanceFiat'] == '0'){
chrome.browserAction.setBadgeText({text: '0.00'});
browser.browserAction.setBadgeText({text: '0.00'});
} else {
chrome.browserAction.setBadgeText({text: localStorage['availableBalanceFiat']});
browser.browserAction.setBadgeText({text: localStorage['availableBalanceFiat']});
// currencyManager.amount(parseFloat(localStorage['availableBalanceFiat'])).then(function(formattedMoney) {
// chrome.browserAction.setBadgeText({text: formattedMoney});
// browser.browserAction.setBadgeText({text: formattedMoney});
// });
}

if(insufficientBalance()) {
chrome.browserAction.setBadgeBackgroundColor({color:'#ff0000'});
browser.browserAction.setBadgeBackgroundColor({color:'#ff0000'});
}

// If weekly reminder active, then show the [....] flag.
// if (localStorage['manualRemind'] == 'true' && localStorage['weeklyAlarmReminder'] == 'true'){
// chrome.browserAction.setBadgeBackgroundColor({color:'#9BDBFC'});
// chrome.browserAction.setBadgeText({text: '....'});
// browser.browserAction.setBadgeBackgroundColor({color:'#9BDBFC'});
// browser.browserAction.setBadgeText({text: '....'});
// }
}, false);

Expand Down Expand Up @@ -110,9 +110,9 @@ function updateTimeOnPage() {
return;
}

chrome.tabs.get(currentTabId, function(tab) {
browser.tabs.get(currentTabId).then(function(tab) {
// Ensure set on focused window.
chrome.windows.get(tab.windowId, function(window) {
browser.windows.get(tab.windowId).then(function(window) {
if (!window.focused) {
return;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ function updateTimeOnPage() {
}

function isBlacklisted(url, callback){
hostname = new URL(url).hostname;
var hostname = new URL(url).hostname;
db.get('blacklistedhostnames', hostname).then(function(record){
if(!record){ // if hostname not blacklisted.
db.get('blacklist', url).then(function(record){
Expand Down Expand Up @@ -177,7 +177,7 @@ function hasKnownBtcAddress(url, callback){


function isStarredUser(url, callback){
twitterHandle = url.match(/[https|http]:\/\/twitter\.com\/(.*)/);
var twitterHandle = url.match(/[https|http]:\/\/twitter\.com\/(.*)/);
if(twitterHandle){
twitterHandle = twitterHandle[1];
db.get('sponsors', twitterHandle).then(function(record){
Expand Down Expand Up @@ -212,6 +212,7 @@ Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};

var db;
function initialize() {
db = new ydn.db.Storage('protip', schema);

Expand All @@ -225,28 +226,31 @@ function initialize() {
}


chrome.tabs.onSelectionChanged.addListener(
function(tabId, selectionInfo) {
browser.tabs.onActivated.addListener(
function(activeInfo) {
console.log("Tab changed");
currentTabId = tabId;
currentTabId = activeInfo.tabId;
updateTimeOnPage();
});

chrome.tabs.onUpdated.addListener(
browser.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
if (tabId == currentTabId) {
console.log("Tab updated");
updateTimeOnPage();
}
});

chrome.windows.onFocusChanged.addListener(
browser.windows.onFocusChanged.addListener(
function(windowId) {
console.log("Detected window focus changed.");
chrome.tabs.getSelected(windowId,
function(tab) {
browser.tabs.query({windowId: windowId, active: true})
.then(function(tabs) {
console.log("Window/Tab changed");
currentTabId = tab.id;
var tab = tabs[0];
if (tab !== undefined) {
currentTabId = tab.id;
}
updateTimeOnPage();
});
});
Expand All @@ -255,45 +259,48 @@ function initialize() {
window.setInterval(updateTimeOnPage, updateTimeOnPageInterval);

// Keep track of idle time.
chrome.idle.queryState(60, checkIdleTime);
chrome.idle.onStateChanged.addListener(checkIdleTime);
browser.idle.queryState(60).then(checkIdleTime);
browser.idle.onStateChanged.addListener(checkIdleTime);
}

initialize();


chrome.runtime.onMessage.addListener(
browser.runtime.onMessage.addListener(
function(request, sender, sendResponse) {

if(request.action && request.action == "isBlacklisted") {
isBlacklisted(request.url, function(blacklistFound){
chrome.tabs.getSelected(null, function(tab) {
browser.tabs.query({active: true}).then(function(tabs) {
var tab = tabs[0];
if(blacklistFound){
chrome.browserAction.setBadgeBackgroundColor({color:'#000000', tabId: tab.id});
chrome.browserAction.setBadgeText({text: 'x', tabId: tab.id});
browser.browserAction.setBadgeBackgroundColor({color:'#000000', tabId: tab.id});
browser.browserAction.setBadgeText({text: 'x', tabId: tab.id});
} else {
hasKnownBtcAddress(request.url, function(record){
// give permission for the content script to scan the DOM for BTC addresses
chrome.tabs.sendRequest(tab.id, {method: 'isBlacklisted', response: false, knownBTCAddress: record.bitcoinAddress});
browser.tabs.sendMessage(tab.id, {method: 'isBlacklisted', response: false, knownBTCAddress: record.bitcoinAddress});
})
}
});
});
} else if(request.action && request.action == "isStarredUser") {
isStarredUser(request.url, function(starredFound){
if( starredFound ) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {method: 'isStarredUser', response: true});
browser.tabs.query({active: true}).then(function(tabs) {
var tab = tabs[0];
browser.tabs.sendMessage(tab.id, {method: 'isStarredUser', response: true});
});
}
});
} else if(request.action && request.action == "deleteBitcoinAddress"){
db.remove('sites', request.url);
db.put('blacklist', { url: request.url });
chrome.tabs.getSelected(null, function(tab) {
chrome.browserAction.setBadgeBackgroundColor({color:'#000000', tabId: tab.id});
chrome.browserAction.setBadgeText({text: 'x', tabId: tab.id});
chrome.browserAction.setIcon({path: 'assets/images/icon_48.png', tabId: tab.id});
browser.tabs.query({active: true}).then(function(tabs) {
var tab = tabs[0];
browser.browserAction.setBadgeBackgroundColor({color:'#000000', tabId: tab.id});
browser.browserAction.setBadgeText({text: 'x', tabId: tab.id});
browser.browserAction.setIcon({path: 'assets/images/icon_48.png', tabId: tab.id});
});
} else if(request.action && request.action == "putBitcoinAddress" && validAddress(request.bitcoinAddress)){
db.get('sites', request.url).done(function(record) {
Expand All @@ -304,14 +311,15 @@ chrome.runtime.onMessage.addListener(
}
db.remove('blacklist', request.url);
});
chrome.tabs.getSelected(null, function(tab) {
chrome.browserAction.setBadgeBackgroundColor({color:'#00ff00', tabId: tab.id});
browser.tabs.query({active: true}).then(function(tabs) {
var tab = tabs[0];
browser.browserAction.setBadgeBackgroundColor({color:'#00ff00', tabId: tab.id});
if (request.source && request.source == 'metatag') {
chrome.browserAction.setBadgeText({text: 'meta', tabId: tab.id}); // request.bitcoinAddresses.length.toString(), tabId: tab.id});
browser.browserAction.setBadgeText({text: 'meta', tabId: tab.id}); // request.bitcoinAddresses.length.toString(), tabId: tab.id});
} else {
chrome.browserAction.setBadgeText({text: request.bitcoinAddress.trim().substring(0,4), tabId: tab.id}); // request.bitcoinAddresses.length.toString(), tabId: tab.id});
browser.browserAction.setBadgeText({text: request.bitcoinAddress.trim().substring(0,4), tabId: tab.id}); // request.bitcoinAddresses.length.toString(), tabId: tab.id});
}
chrome.browserAction.setIcon({path: 'assets/images/heart48x48.png', tabId: tab.id});
browser.browserAction.setIcon({path: 'assets/images/heart48x48.png', tabId: tab.id});
});
}
}
Expand Down
32 changes: 16 additions & 16 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ window.addEventListener("message", function (event) {
return;
}

chrome.runtime.sendMessage({
browser.runtime.sendMessage({
action: event.data.action,
bitcoinAddress: event.data.bitcoinAddress,
url: document.URL,
title: document.title
});
}, false);

var port = chrome.runtime.connect();
knownBTCAddress = '' // set global
var port = browser.runtime.connect();
var knownBTCAddress = '';
if(document.URL.match(/http/)){ // only send http or https urls no chrome:// type addresses.

chrome.runtime.sendMessage({action: 'isBlacklisted', url:document.URL});
chrome.runtime.sendMessage({action: 'isStarredUser', url:document.URL});
browser.runtime.sendMessage({action: 'isBlacklisted', url:document.URL});
browser.runtime.sendMessage({action: 'isStarredUser', url:document.URL});

// ProTip finds addresses in a 2 step in process using 2 different functions.
// 1) It scans the whole page and wraps all the bitcoin addresses it finds.
// 2) It loops over all the wrapped bitcoin addresses and chooses the
// bitcoin address it is going to put into the database.

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
knownBTCAddress = request.knownBTCAddress // Set global
// If the URL is not blackedlisted, scan the page.
if (request.method == 'isBlacklisted' && request.response == false){
Expand Down Expand Up @@ -68,7 +68,7 @@ function selectPrioritizedBitcoinAddress(options){
// the known bitcoin addresss.
// Otherwise people don't think that ProTip is not detecting the bitcoin
// address in the Metatag on repeat visits to the URL.
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
source: 'metatag',
action: "putBitcoinAddress",
bitcoinAddress: metatag,
Expand All @@ -81,7 +81,7 @@ function selectPrioritizedBitcoinAddress(options){
recordAndHighlightBitcoinAddress(options.knownBTCAddress)
} else if (metatag){
// (2) Don't select any bitcoin addresses. Display 'Meta' in ProTip icon.
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
source: 'metatag',
action: "putBitcoinAddress",
bitcoinAddress: metatag,
Expand All @@ -100,7 +100,7 @@ function selectPrioritizedBitcoinAddress(options){
function scanLinks() {
var matchedLinks = [];
var links = document.links;
for ( i = 0; i < links.length; i++ ) {
for (var i = 0; i < links.length; i++ ) {

// The standard for most third party software such as tipping services and wallets.
// <a href="bitcoin:1ProTip9x3uoqKDJeMQJdQUCQawDLauNiF">foo</a>
Expand Down Expand Up @@ -146,7 +146,7 @@ function scanText(target){
//var words = node.textContent.split(/(\r\n|\n|\r|\s)/gm)
var words = node.textContent.split(/(\r\n|\n|\r|\s|\,|\;|\.)/gm)
var parent_span = document.createElement("span");
for ( i = 0; i < words.length; i++ ) {
for (var i = 0; i < words.length; i++ ) {
//if(words[i].trim() == 'and'){
//debugger;
if(validAddress(words[i].trim())){
Expand Down Expand Up @@ -201,14 +201,14 @@ var matchText = function(node, regex, callback, excludeElements) {

$(function() {

observeDOM = (function(){
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;

return function(obj, callback){
if( MutationObserver ){
// define a new observer
obs = new MutationObserver(function(mutations, observer){ // set as global
var obs = new MutationObserver(function(mutations, observer){
//var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ){
if(mutations[0].addedNodes.length > 0){
Expand Down Expand Up @@ -286,7 +286,7 @@ $(function() {
function scanMetatags(){
//<meta name="microtip" content="1PvxNMqU29vRj8k5EVKsQEEfc84rS1Br3b" data-currency="btc">
var metatags = document.getElementsByTagName('meta');
for ( i = 0; i < metatags.length; i++ ) {
for (var i = 0; i < metatags.length; i++ ) {
if( metatags[i].name == 'microtip' && validAddress(metatags[i].content) ) {
return metatags[i].content // only get the first instance of a microtip metatag.
}
Expand Down Expand Up @@ -352,7 +352,7 @@ function tagElementWithProTipUI(match, klass_name){
}

function recordAndHighlightBitcoinAddress(btcAddress){
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
action: 'putBitcoinAddress',
bitcoinAddress: btcAddress,
title: document.title,
Expand Down Expand Up @@ -410,7 +410,7 @@ function starredUser(){
span.style.backgroundColor = '#7FE56F';

//Code for displaying <extensionDir>/images/myimage.png:
var imgURL = chrome.extension.getURL("assets/images/star.png");
var imgURL = browser.extension.getURL("/assets/images/star.png");
var img = document.createElement("img");
img.setAttribute("src", imgURL);

Expand All @@ -425,4 +425,4 @@ function starredUser(){

twitterUserContainer.appendChild(img);
twitterUserContainer.appendChild(span);
}
}
6 changes: 3 additions & 3 deletions controllers/financial.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $(document).ready(function() {
// Promise.all([currencyManager.amount(balance), currencyManager.amount(FEE)]).then(function(results) {
// localStorage['availableBalanceFiat'] = results[0];
// debugger;
// //chrome.browserAction.setBadgeText({text: localStorage['availableBalanceFiat']});
// //browser.browserAction.setBadgeText({text: localStorage['availableBalanceFiat']});
// });
});
setupWallet();
Expand Down Expand Up @@ -429,7 +429,7 @@ $(document).ready(function() {
*/

if (typeof chrome !== 'undefined') {
$('#version').text(chrome.runtime.getManifest().version);
$('#version').text(browser.runtime.getManifest().version);
} else {
addon.port.on('version', function(version) {
$('#version').text(version);
Expand All @@ -438,7 +438,7 @@ $(document).ready(function() {

$('#aboutModal').on('click', 'a', function() {
if (typeof chrome !== 'undefined') {
chrome.tabs.create({
browser.tabs.create({
url: $(this).attr('href')
});
} else {
Expand Down
Loading

0 comments on commit 1414783

Please sign in to comment.