Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/uhh-lt/storyfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
remstef committed Sep 26, 2017
2 parents 566912f + 8a34863 commit 7efcb8b
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 82 deletions.
98 changes: 63 additions & 35 deletions plugin/backgroundscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var async = require('async');

// GLOBALS
var popupWindowId = null;
var mainWindowId = null;
var mainWindowId = chrome.windows.WINDOW_ID_CURRENT;
var mainURL = "";

var popupPercentage = 0.4;
var windowPercentage = 0.6;
Expand All @@ -20,12 +21,11 @@ var windowRectangle = {
chrome.browserAction.onClicked.addListener(function () {
// es wurde bereits einmal ein Popup erzeugt
if (popupWindowId !== undefined && popupWindowId !== null) {
chrome.windows.remove(popupWindowId);
popupWindowId = null;

chrome.windows.update(mainWindowId, { height: windowRectangle.height, width: windowRectangle.width, top: windowRectangle.top, left: windowRectangle.left });
closePopup();
// es wurde das erste mal auf den Button geklickt
} else {
mainURL = "";

chrome.windows.getCurrent(function (window) {

// save mainwindow dimensions
Expand Down Expand Up @@ -93,6 +93,34 @@ chrome.windows.onRemoved.addListener(function (windowID) {
}
});

chrome.windows.onFocusChanged.addListener(function (windowId) {
chrome.windows.get(windowId, { populate: true, windowTypes: ['normal'] }, function (window) {
if (window !== null && window !== undefined) {
mainWindowId = window.id;
chrome.tabs.query({ windowId: window.id, active: true }, function (tabs) {
if (tabs[0].url !== mainURL) {
mainURL = tabs[0].url;
onAttach();
}
});
}
});
});

chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.windows.get(activeInfo.windowId, { populate: true, windowTypes: ['normal'] }, function (window) {
if (window !== null && window !== undefined) {
mainWindowId = window.id;
chrome.tabs.get(activeInfo.tabId, function (tab) {
if (tab.url !== mainURL) {
mainURL = tab.url;
onAttach();
}
});
}
});
});

// FUNCTIONS
function saveRemote(url, data, callback) {
// früher onComplete
Expand Down Expand Up @@ -135,7 +163,7 @@ function setArticle(article, thetab) {
setArticleHelper(article, tab);
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
setArticleHelper(article, tabs[0]);
});
}
Expand Down Expand Up @@ -164,16 +192,12 @@ function setArticleHelper(article, tab) {
if (items.server === '') alert("Server is not defined!");

async.series([function (next) {
// favicon holen
data.Site.favicon = "https://www.google.com/s2/favicons?domain=" + url.host;
next();
/*
https://www.google.com/s2/favicons?domain=www.tagesschau.de
getFavicon(data.Site.host, function(favicon){
data.Site.favicon = favicon;
next();
});
*/
}, function (next) {
// seite prüfen: relevant oder nicht relevant
// seite parsen
saveRemote(items.server.replace(/\/$/, '') + '/Sites', data, function (err, response) {
console.log(err, response);
if (err) {
Expand Down Expand Up @@ -207,29 +231,19 @@ function setArticleHelper(article, tab) {
return;
}

chrome.windows.getCurrent(function (win) {
chrome.tabs.captureVisibleTab(win.id, { "format": "png" }, function (imgUrl) {
saveRemote(items.server.replace(/\/$/, '') + '/Sites/' + siteId + '/image', { image: imgUrl }, function (err) {
next(err);
});
// ausschnitt screenshot machen
chrome.tabs.captureVisibleTab(mainWindowId, { "format": "png" }, function (imgUrl) {
saveRemote(items.server.replace(/\/$/, '') + '/Sites/' + siteId + '/image', { image: imgUrl }, function (err) {
next(err);
});
});
}, function (next) {
if (!bIsNew) {
next();
return;
}
/*
captureTab(article.bounds, function(image){
saveRemote(
items.server.replace(/\/$/,'') + '/Articles/' + articleId + '/image',
{image: image},
function(err){
next(err);
}
);
});
*/

// volltext screenshot machen
}], function (err) {
if (err) {
throw err;
Expand All @@ -244,10 +258,12 @@ function onAttach() {
}, function (items) {
if (items.server === '') alert("Server is not defined!");

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
mainURL = tabs[0].url;
if (tabs[0].url.replace(/\/$/g, '').substr(0, items.server.replace(/\/$/g, '').length) === items.server.replace(/\/$/g, '')) {
chrome.tabs.sendMessage(tabs[0].id, { type: "hide-sidebar" });
closePopup();
} else {
console.log("TAB:" + tabs[0].url);
chrome.tabs.sendMessage(tabs[0].id, { type: "getArticle", data: {} });
}
});
Expand All @@ -257,13 +273,13 @@ function onAttach() {
function parseSite(url) {
console.log('Parsing site...', url);

chrome.tabs.query({ active: true, currentWindow: false }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { type: "getArticle", tab: tabs[0].id, data: { isRelevant: true } });
});
}

function addToHighlighting(entities) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { type: "addEntities", data: entities });
});
}
Expand All @@ -285,7 +301,7 @@ function captureTab2(p, callback) {
}
});

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { type: 'take-screenshot', data: rectangle });
});
}
Expand All @@ -300,7 +316,7 @@ function captureTab(p, callback) {
});

chrome.tabs.captureVisibleTab(function (screenshotUrl) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.query({ active: true, windowId: mainWindowId }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { type: 'take-screenshot', data: screenshotUrl });
});
});
Expand Down Expand Up @@ -340,6 +356,18 @@ function createPopup(left, top, width, height) {
});
}

function closePopup() {
chrome.windows.remove(popupWindowId);
popupWindowId = null;

chrome.windows.update(mainWindowId, {
height: windowRectangle.height,
width: windowRectangle.width,
top: windowRectangle.top,
left: windowRectangle.left
});
}

function storeCredentials(username, password) {
chrome.storage.sync.set({
username: username,
Expand Down
Loading

0 comments on commit 7efcb8b

Please sign in to comment.