Skip to content

Manually serialize and deserialize the canvas capture to/from JSON and #71

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions extensions/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function listenForMessage(callback) {
var tabInfo = {}
var resultTab = null;
var currentCapture = null;
var currentCaptureChunks = [];
var currentFrameId = null;
var currentTabId = null;

Expand Down Expand Up @@ -113,13 +114,21 @@ listenForMessage(function(request, sender, sendResponse) {
popup.captureComplete(request.errorString);
}
}
else if (request.capture) {
else if (request.captureChunk) {
currentCaptureChunks.push(request.captureChunk);
}
else if (request.captureDone) {
// Concatenate the current capture chunks and reset the array.
var allChunks = currentCaptureChunks;
currentCaptureChunks = [];
var fullJSON = "".concat.apply("", allChunks);
var capture = JSON.parse(fullJSON);
// If a capture has been received,
var tabWindows = browser.extension.getViews({type: "tab"});
// Open the result view if not open (need to check if length == 1 that the function exists for Edge),
window.browser.tabs.create({ url: "result.html", active: true }, function(tab) {
resultTab = tab;
currentCapture = request.capture;
currentCapture = capture;
currentFrameId = frameId;
currentTabId = sender.tab.id;
});
Expand All @@ -134,4 +143,4 @@ listenForMessage(function(request, sender, sendResponse) {

// Return the frameid for reference.
sendResponse({ frameId: frameId });
});
});
24 changes: 22 additions & 2 deletions extensions/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,27 @@ if (sessionStorage.getItem(spectorLoadedKey)) {
});

document.addEventListener('SpectorOnCaptureEvent', function (e) {
sendMessage({ capture: e.detail.capture });
// The browser imposes limits on the size of the serialized JSON
// associated with these messages. To avoid running into these limits,
// and not have to introspect too deeply into the ICapture structure, we
// manually serialize to JSON and chop up the resulting string.
//
// Note for future reference: the object that comes in via
// e.detail.capture is actually immutable, because of the way it's
// serialized from the main world to the content script. If the goal
// were to stub out certain fields and send them separately,
// Object.assign would have to be used to create a new top-level object.
var serialized = JSON.stringify(e.detail.capture);
var len = serialized.length;
var ii = 0;
var step = 32 * 1024 * 1024; // 32 MB
while (ii < len) {
var nextIndex = Math.min(ii + step, len);
var substr = serialized.substring(ii, nextIndex);
sendMessage({ captureChunk: substr });
ii = nextIndex;
}
sendMessage({ captureDone: true });
}, false);

document.addEventListener('SpectorOnErrorEvent', function (e) {
Expand Down Expand Up @@ -415,4 +435,4 @@ listenForMessage(function (message) {
document.dispatchEvent(myEvent);
}
}
});
});