Commit 210f8572 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[feature] Support turning off a tab

parent 46623cc83110
......@@ -320,6 +320,18 @@ chrome.runtime.onMessage.addListener(
sendResponse({ ok: null, error: reason });
});
return true;
} else if (request.requestType == "TurnOffTab") {
let tabId = request.payload;
if (tabId == null || tabId == undefined) {
// deduce from the sender
tabId = sender.tab.id;
}
let observation = allObservations[tabId];
if (observation == undefined) sendResponse(null);
observation.negotiated = null;
observation.preemptable = false;
chrome.pageAction.hide(tabId);
sendResponse(observation);
}
}
);
......
......@@ -228,6 +228,46 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
);
}
/**
* Sends to the background a message to turn the browser off for a tab
* @param id The identifier of a tab
*/
export function backgroundTurnOffForTab(
tabId: number
): Promise<DocumentObservations> {
return new Promise<DocumentObservations>(
(
resolve: (result: DocumentObservations) => void,
reject: (reason: any) => void
) => {
chrome.runtime.sendMessage(
{
requestType: "TurnOffTab",
payload: tabId
},
(result: DocumentObservations) => {
if (result == null) {
reject("Invalid tab");
} else {
resolve(result);
}
}
);
}
);
}
/**
* Sends to a tab a message to turn the browser off
* @param id The identifier of a tab
*/
export function turnOffTab(tabId: number): void {
chrome.tabs.sendMessage(tabId, {
requestType: "TurnOff",
payload: tabId
});
}
/**
* Gets the data for the current resource
* @param id The identifier of a tab
......
......@@ -19,7 +19,11 @@
******************************************************************************/
import "chrome";
import { Message, getObservationsFor } from "../common/messages";
import {
Message,
getObservationsFor,
backgroundTurnOffForTab
} from "../common/messages";
import { DocumentObservations, findLinksInDocument } from "../common/data";
import { Viewer, newViewer } from "../common/viewer";
import { LDBrowser, newBrowser } from "../common/browser";
......@@ -58,7 +62,15 @@ function initializeBrowser(): LDBrowser {
sender,
sendResponse
) {
if (request.requestType == "GetCurrentResource") {
if (request.requestType == "TurnOff") {
backgroundTurnOffForTab(request.payload)
.then((value: any) => {
window.location.reload(true);
})
.catch((reason: any) => {
console.log(reason);
});
} else if (request.requestType == "GetCurrentResource") {
sendResponse(browser.getCurrentResource());
} else if (request.requestType == "UpdateCurrentCommand") {
browser.setCommand(request.payload);
......
......@@ -27,7 +27,8 @@ import {
addViewSource,
removeViewSource,
getCurrentResource,
updateCurrentCommand
updateCurrentCommand,
turnOffTab
} from "../common/messages";
import {
ResourceData,
......@@ -148,7 +149,9 @@ class Popup extends React.Component<{}, State> {
this.setState(this.state);
}
onTunOff() {}
onTunOff() {
turnOffTab(this.state.tabId);
}
onUpdateTabCommand() {
updateCurrentCommand(this.state.tabId, this.state.data.command);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment