Skip to content

Commit

Permalink
move ajax call to background page
Browse files Browse the repository at this point in the history
  • Loading branch information
kviktor committed Jun 18, 2019
1 parent 62a21dc commit 1060f65
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
19 changes: 19 additions & 0 deletions clients/chrome/js/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var API_URL = "http://localhost:5000/get_scores/";


chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.contentScriptQuery == "get_scores") {
$.ajax({
url: API_URL,
method: "POST",
data: JSON.stringify(request.urls),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(re) {
sendResponse({'scores': re});
}
});
return true;
}
});
21 changes: 9 additions & 12 deletions clients/chrome/js/magic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var API_URL = "https://localhost:5000/get_scores/";
var MAX_SPLIT_NUM = 6;

var urls = new Set();
Expand All @@ -16,25 +15,23 @@ chrome.storage.local.get(key, function(result) {
if(key in result) {
addScores(result[key]);
} else {
$.ajax({
url: API_URL,
method: "POST",
data: JSON.stringify(Array.from(urls)),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(re) {
chrome.runtime.sendMessage(
{contentScriptQuery: 'get_scores', urls: Array.from(urls)},
function(response) {
chrome.storage.local.clear();

if(!hasNullValue(re)) {
var value = {}; value[key] = re;
var scores = response.scores;
if(!hasNullValue(scores)) {
var value = {}; value[key] = scores;
chrome.storage.local.set(value);
}
addScores(re);
addScores(scores);
}
});
);
}
});


function addScores(scores) {
velvetLinks.each(function() {
var url = decodeURIComponent(gup("url", $(this).prop("href")));
Expand Down
6 changes: 5 additions & 1 deletion clients/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
"matches" : ["https://index.hu/", "http://index.hu/" ],
"js" : [ "js/jquery.js", "js/magic.js" ]
}],
"background": {
"scripts": ["js/jquery.js", "js/background.js"],
"persisent": false
},
"permissions": [
"storage",
"https://localhost/"
"http://localhost/"
]
}
6 changes: 3 additions & 3 deletions misc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ then
fi

gitroot=$(git rev-parse --show-toplevel)
magicjs="clients/chrome/js/magic.js"
backgroundjs="clients/chrome/js/background.js"
manifest="clients/chrome/manifest.json"
api=$1
pem=$2

sed -i "s/localhost:5000/$api/g" $gitroot/$magicjs
sed -i "s/localhost/$api/g" $gitroot/$manifest
sed -i "s/http:\/\/localhost:5000/https:\/\/$api/g" $gitroot/$backgroundjs
sed -i "s/http:\/\/localhost/https:\/\/$api/g" $gitroot/$manifest

chromium-browser --pack-extension="$gitroot/clients/chrome/" --pack-extension-key="$pem"

0 comments on commit 1060f65

Please sign in to comment.