Skip to content

Commit

Permalink
Fix notifications appear/disappear
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustem Mussabekov committed Nov 21, 2019
1 parent 71120f1 commit 0eaa9c7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 191 deletions.
147 changes: 65 additions & 82 deletions src/background/browserActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,71 @@ var {extension, getCurrentTab, browserName, isNewTabPage, updateTabAndGoToRaindr
var manifest = require('json!../config/manifest.json');
var popupPath = manifest.browser_action.default_popup;

const button = {
/*icons: {},
initIcons() {
var idle = manifest.browser_action.default_icon;
var statuses = {saved: {}, newtab: {}};//, loading: {}, savedloading: {}
for(var i in statuses)
for (var j in manifest.browser_action.default_icon)
statuses[i][j] = manifest.browser_action.default_icon[j].replace("idle", "/"+i);
statuses.idle = manifest.browser_action.default_icon;
if (__PLATFORM__=="firefox"){
for(var i in statuses.idle)
statuses.idle[i] = statuses.idle[i].replace("idle","firefox_idle");
for(var i in statuses.saved)
statuses.idle[i] = statuses.idle[i].replace("saved","firefox_saved");
class BrowserAction {
constructor() {
const onMessage = (r, sender, sendResponse)=>{
switch(r.action){
case "rerenderBrowserAction":
this.render();
return true

case "setStatus":
getCurrentTab((tab)=>{
var obj = Object.assign({},r);
if (!obj.url)
obj.url = tab.url;

links.setStatus(obj);
this.render();
});
return true

case "appStarted":
//Inject script
extension.tabs.executeScript({
code: 'window.raindropInjectScriptLoaded'
}, function([alreadyInjected=false]) {
if (!alreadyInjected)
extension.tabs.executeScript({
file: 'inject.js'
})
})

links.resetAll();
return true
}
}

const onTabUpdate = (tab)=>{
setTimeout(this.render, 100)
}
button.icons = statuses;
},*/

const onClicked = (tab)=>{
updateTabAndGoToRaindrop()
}

if (extension){
extension.browserAction.onClicked.removeListener(onClicked);
extension.browserAction.onClicked.addListener(onClicked);

extension.runtime.onMessage.removeListener(onMessage);
extension.runtime.onMessage.addListener(onMessage);

extension.tabs.onUpdated.removeListener(onTabUpdate);
extension.tabs.onUpdated.addListener(onTabUpdate);

extension.tabs.onActivated.removeListener(onTabUpdate);
extension.tabs.onActivated.addListener(onTabUpdate);

//button.initIcons();
extension.browserAction.setBadgeBackgroundColor({color: '#0087EA'})
try{
extension.browserAction.setBadgeTextColor({color: '#FFFFFF'})
}catch(e){}

this.render();
}
}

render() {
getCurrentTab((tab={})=>{
Expand Down Expand Up @@ -67,65 +111,4 @@ const button = {
}
}

const onMessage = (r, sender, sendResponse)=>{
switch(r.action){
case "rerenderBrowserAction":
button.render();
return true

case "setStatus":
getCurrentTab((tab)=>{
var obj = Object.assign({},r);
if (!obj.url)
obj.url = tab.url;

links.setStatus(obj);
button.render();
});
return true

case "appStarted":
//Inject script
extension.tabs.executeScript({
code: 'window.raindropInjectScriptLoaded'
}, function([alreadyInjected=false]) {
if (!alreadyInjected)
extension.tabs.executeScript({
file: 'inject.js'
})
})

links.resetAll();
return true
}
}

const onTabUpdate = (tab)=>{
setTimeout(()=>button.render(),100);
}

const onClicked = (tab)=>{
updateTabAndGoToRaindrop()
}

if (extension){
extension.browserAction.onClicked.removeListener(onClicked);
extension.browserAction.onClicked.addListener(onClicked);

extension.runtime.onMessage.removeListener(onMessage);
extension.runtime.onMessage.addListener(onMessage);

extension.tabs.onUpdated.removeListener(onTabUpdate);
extension.tabs.onUpdated.addListener(onTabUpdate);

extension.tabs.onActivated.removeListener(onTabUpdate);
extension.tabs.onActivated.addListener(onTabUpdate);

//button.initIcons();
extension.browserAction.setBadgeBackgroundColor({color: '#0087EA'})
try{
extension.browserAction.setBadgeTextColor({color: '#FFFFFF'})
}catch(e){}

button.render();
}
export default new BrowserAction()
32 changes: 8 additions & 24 deletions src/background/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const Notify = {
return ids[id];
},

hashId(id, prefix='') {
return prefix+hash(id)
},

getGrant(callback) {
if (__PLATFORM__=="firefox")
return callback(true);
Expand All @@ -31,7 +35,7 @@ const Notify = {
},

show(_options, _originalId="empty", _prefix="") {
var _id = _prefix+hash(_originalId);
var _id = Notify.hashId(_originalId, _prefix)
ids[_id] = {
id: _originalId,
type: _prefix
Expand All @@ -41,7 +45,7 @@ const Notify = {
if (!granted)return;

var options = Object.assign({
type: "basic",
type: _options.type||"basic",
iconUrl: 'assets/icon-'+extensionConfig.notificationIconSize+'.png',
//priority: 2
}, _options);
Expand All @@ -52,33 +56,13 @@ const Notify = {
delete options.buttons;
}

extension.notifications.getAll((allNotifications)=>{
var update=false;
for(var i in allNotifications){
if (i==_id){
update=true;
break;
}
}

if (update){
if (__PLATFORM__=="firefox"){
extension.notifications.clear(_id);
update=false;
}
else
extension.notifications.update(_id, options);
}

if (!update)
extension.notifications.create(_id, options);
})
extension.notifications.create(_id, options)
})
},

close(_id) {
if (_id)
extension.notifications.clear(_id);
extension.notifications.clear(_id)
}
}

Expand Down
Loading

0 comments on commit 0eaa9c7

Please sign in to comment.