Skip to content

Commit

Permalink
feat: Faster and more reliable thanks to switching to an async based …
Browse files Browse the repository at this point in the history
…server with Quart and more.
  • Loading branch information
elblogbruno committed Feb 19, 2021
1 parent a0b1492 commit 002f5bd
Show file tree
Hide file tree
Showing 22 changed files with 376 additions and 261 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.9]

### Added

- Faster and more reliable thanks to switching to an async based server with Quart.
- Added json responses on server with built-in responses that would be translated to other languages. When requesting to the API, it returns the same text and status code on chrome, firefox and android/ios platforms.
- Updated app to work with these changes.
- When clicking on the message box, you can go to the url of the added content and see it.

### Fixed

- Refactored code, made it easier for example to implement in the near future a way to translate to different languages, and removed unused code.
- No message was shown when trying to add something witouth having entered credentials.

## [1.8]

### Added
Expand Down
80 changes: 51 additions & 29 deletions Chrome Extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ async function AddUrlToMind(tab) {
var url = tab.url;
var title = tab.title;
const urlParams = `add_url_to_mind?url=${url}&title=${title}`;

req.open("GET", baseUrl+urlParams, true);

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand All @@ -97,7 +96,7 @@ async function AddTextToMind(info,tab) {

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand Down Expand Up @@ -135,13 +134,13 @@ async function ProcessSelection(info,tab) {
default:
break;
}

alert(baseUrl+urlParams);
req.open("GET", baseUrl+urlParams, true);
req.send();

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand All @@ -151,31 +150,31 @@ async function ProcessSelection(info,tab) {
}
}

function parseAndSwitchResponse(response_from_api){
var myObj = JSON.parse(response_from_api);
switchResponse(String(myObj['status_code']), String(myObj['block_url']),String(myObj['status_text']),String(myObj['text_response']));
}

function switchResponse(response){
switch (response) {
case '200':
createHandler(
() =>
showNotification({
message: "Added to your mind.",
status: "success",
redirect: "http://www.laguiaempresarial.com/item/10731/",
})
);
break;
case '-1':
createHandler(
() =>
showNotification({
message: "Error during accessing server. Make sure the ip/port are corrects, and the server is running.",
status: "error",
redirect: "http://www.laguiaempresarial.com/item/10731/",
})
);
break;
default:
break;
function switchResponse(status_code,block_url,status_text,text_response)
{
if (status_code == '-1'){
createHandler(
() =>
showNotification({
message: "Error during accessing server. Make sure the ip/port are corrects, and the server is running.",
status: "error",
redirect: "https://github.com/elblogbruno/NotionAI-MyMind#love-to-try-it",
})
);
}else{
createHandler(
() =>
showNotification({
message: text_response,
status: status_text,
redirect: block_url,
})
);
}
}

Expand Down Expand Up @@ -207,6 +206,10 @@ chrome.cookies.onChanged.addListener(function(info)
}
});

chrome.runtime.onMessage.addListener(function(request, sender) {
openNewTab(request.redirect);
});

// undefined
function createHandler(callback) {
chrome.tabs.insertCSS({
Expand Down Expand Up @@ -270,6 +273,25 @@ function isEmpty(str) {
return (!str || 0 === str.length);
}

async function GetMindUrl(){
const req = new XMLHttpRequest();
// var mainUrl = getServerUrl();
let permission = await getFromStorage("serverIP");
if(!isEmpty(permission)){
const baseUrl = permission;
const urlParams = `get_current_mind_url`;

req.open("GET", baseUrl+urlParams, true);

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
return this.responseText;
}
}
req.send();
}
}

async function openMindUrl(){
const req = new XMLHttpRequest();
// var mainUrl = getServerUrl();
Expand Down
8 changes: 2 additions & 6 deletions Chrome Extension/js/notificationBehaviour.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
(() => {
const usePromise = typeof browser !== "undefined";

const { id, icon, item, message, status, redirect } = window.naimm;
const { id, icon, message, status, redirect } = window.naimm;

const events = {};
const tags = [];
const tagCache = {};

let previousValue = "";

const createListener = (id, target, name, callback) => {
events[id] = {
Expand Down Expand Up @@ -44,7 +40,7 @@

createListener("click", notificationInner, "click", event => {
if (redirect && redirect !== "undefined") {
chrome.runtime.sendMessage({ openTab: redirect });
chrome.runtime.sendMessage({redirect: redirect});
}

createRemovalTimeout(0, true);
Expand Down
2 changes: 1 addition & 1 deletion Chrome Extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Notion AI My Mind",
"description": "This extension allows to click to add any image or website to your MIND in notion, and forget about everything else.",
"version": "1.0.6",
"version": "1.0.7",

"browser_action": {
"default_icon": "icon/icon.png",
Expand Down
60 changes: 33 additions & 27 deletions Firefox Extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function AddUrlToMind(tab) {

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand All @@ -97,7 +97,7 @@ async function AddTextToMind(info,tab) {

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand Down Expand Up @@ -141,7 +141,7 @@ async function ProcessSelection(info,tab) {

req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
switchResponse(this.responseText);
parseAndSwitchResponse(this.responseText);
} else if (this.status === 0) {
switchResponse("-1");
}
Expand All @@ -152,34 +152,36 @@ async function ProcessSelection(info,tab) {
}


function switchResponse(response){
switch (response) {
case '200':
createHandler(
() =>
showNotification({
message: "Added to your mind.",
status: "success",
redirect: "http://www.laguiaempresarial.com/item/10731/",
})
);
break;
case '-1':
createHandler(
() =>
showNotification({
message: "Error during accessing server. Make sure the ip/port are corrects, and the server is running.",
status: "error",
redirect: "http://www.laguiaempresarial.com/item/10731/",
})
);
break;
default:
break;
function parseAndSwitchResponse(response_from_api){
var myObj = JSON.parse(response_from_api);
switchResponse(String(myObj['status_code']), String(myObj['block_url']),String(myObj['status_text']),String(myObj['text_response']));
}

function switchResponse(status_code,block_url,status_text,text_response)
{
if (status_code == '-1'){
createHandler(
() =>
showNotification({
message: "Error during accessing server. Make sure the ip/port are corrects, and the server is running.",
status: "error",
redirect: "https://github.com/elblogbruno/NotionAI-MyMind#love-to-try-it",
})
);
}else{
createHandler(
() =>
showNotification({
message: text_response,
status: status_text,
redirect: block_url,
})
);
}
}



browser.cookies.onChanged.addListener(function(info)
{
if(info['cookie']['domain'] == ".notion.so" && info['cookie']['name'] == "token_v2")
Expand Down Expand Up @@ -207,6 +209,10 @@ browser.cookies.onChanged.addListener(function(info)
}
});

browser.runtime.onMessage.addListener(function(request, sender) {
openNewTab(request.redirect);
});

// undefined
function createHandler(callback) {
browser.tabs.insertCSS({
Expand Down
8 changes: 2 additions & 6 deletions Firefox Extension/js/notificationBehaviour.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
(() => {
const usePromise = typeof browser !== "undefined";

const { id, icon, item, message, status, redirect } = window.naimm;
const { id, icon, message, status, redirect } = window.naimm;

const events = {};
const tags = [];
const tagCache = {};

let previousValue = "";

const createListener = (id, target, name, callback) => {
events[id] = {
Expand Down Expand Up @@ -44,7 +40,7 @@

createListener("click", notificationInner, "click", event => {
if (redirect && redirect !== "undefined") {
chrome.runtime.sendMessage({ openTab: redirect });
chrome.runtime.sendMessage({redirect: redirect});
}

createRemovalTimeout(0, true);
Expand Down
2 changes: 1 addition & 1 deletion Firefox Extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Notion AI My Mind",
"description": "This extension allows to click to add any image or website to your MIND in notion, and forget about everything else.",
"version": "1.0.6",
"version": "1.0.7",

"browser_action": {
"default_icon": "icon/icon.png",
Expand Down
Loading

0 comments on commit 002f5bd

Please sign in to comment.