Pārlūkot izejas kodu

added "process frame" feature (through the context menu)

Gildas lormeau 14 gadi atpakaļ
vecāks
revīzija
e7667bdc5d

+ 12 - 8
WebContent/core/scripts/bg/background.js

@@ -54,7 +54,7 @@
 		pageData.portsId.push(port.portId_);
 		if (!pageData.getDocData(message.winId))
 			pageData.processDoc(port, message.topWindow, message.winId, message.index, message.content, message.title, message.url, message.baseURI,
-					message.characterSet, message.canvasData, {
+					message.characterSet, message.canvasData, message.contextmenuTime, {
 						init : docInit,
 						progress : docProgress,
 						end : docEnd
@@ -153,14 +153,17 @@
 		pageData.setDocContent(docData, content, setContentResponse);
 	}
 
-	function process(tabId, senderId, config, processSelection) {
-		var pageData, configScript = "singlefile.config = " + JSON.stringify(config) + "; singlefile.pageId = " + pageId + ";"
+	function process(tabId, senderId, config, processSelection, processFrame) {
+		var pageData, configScript;
+		if (processFrame)
+			config.processInBackground = true;
+		configScript = "singlefile.config = " + JSON.stringify(config) + "; singlefile.pageId = " + pageId + ";"
 				+ (processSelection ? "singlefile.processSelection = " + processSelection : "");
 		if (tabs[tabId] && tabs[tabId].processing)
 			return;
 		tabs[tabId] = tabs[tabId] || [];
 		tabs[tabId].processing = true;
-		pageData = new singlefile.PageData(tabId, pageId, senderId, config, processSelection, function() {
+		pageData = new singlefile.PageData(tabId, pageId, senderId, config, processSelection, processFrame, function() {
 			executeScripts(tabId, [ {
 				code : "var singlefile = {};"
 			}, {
@@ -236,7 +239,6 @@
 				}
 			}
 		}
-
 		if (port.name == "singlefile") {
 			port.onMessage.addListener(onMessage);
 			port.onDisconnect.addListener(onDisconnect);
@@ -250,13 +252,15 @@
 			for (property in request.config)
 				config[property] = request.config[property];
 		if (request.processSelection)
-			process(request.id, sender.id, config, true);
+			process(request.id, sender.id, config, true, false);
+		else if (request.processFrame)
+			process(request.id, sender.id, config, false, true);
 		else if (request.tabIds)
 			request.tabIds.forEach(function(tabId) {
-				process(tabId, sender.id, config);
+				process(tabId, sender.id, config, false, false);
 			});
 		else
-			process(request.id, sender.id, config);
+			process(request.id, sender.id, config, false, false);
 		sendResponse({});
 	}
 

+ 16 - 4
WebContent/core/scripts/bg/bgcore.js

@@ -23,7 +23,7 @@
 	singlefile.PageData = PageData;
 	singlefile.DocData = DocData;
 
-	function PageData(tabId, pageId, senderId, config, processSelection, callback) {
+	function PageData(tabId, pageId, senderId, config, processSelection, processFrame, callback) {
 		var timeoutError, that = this;
 		this.pageId = pageId;
 		this.docs = [];
@@ -33,6 +33,7 @@
 		this.senderId = senderId;
 		this.config = config;
 		this.processSelection = processSelection;
+		this.processFrame = processFrame;
 		this.processing = true;
 		this.tabId = tabId;
 		this.requestManager = new singlefile.nio.RequestManager();
@@ -43,6 +44,8 @@
 		this.top = null;
 		this.timeoutPageInit = null;
 		this.portsId = [];
+		this.contextmenuTime = null;
+		this.frameDocData = null;
 		timeoutError = setTimeout(function() {
 			that.processing = false;
 			chrome.extension.sendRequest(that.senderId, {
@@ -68,7 +71,7 @@
 					docData.process();
 			});
 		},
-		processDoc : function(port, topWindow, winId, index, content, title, url, baseURI, characterSet, canvasData, callbacks) {
+		processDoc : function(port, topWindow, winId, index, content, title, url, baseURI, characterSet, canvasData, contextmenuTime, callbacks) {
 			var that = this, docData;
 			docData = new DocData(port, winId, index, content, baseURI, characterSet, canvasData);
 			if (topWindow) {
@@ -77,6 +80,10 @@
 				this.url = url;
 			}
 			this.docs.push(docData);
+			if (this.processFrame && contextmenuTime && (!this.contextmenuTime || contextmenuTime > this.contextmenuTime)) {
+				this.contextmenuTime = contextmenuTime;
+				this.frameDocData = docData;
+			}
 			if (this.config.processInBackground && docData.content) {
 				docData.parseContent();
 				docData.processDocCallback = singlefile.initProcess(docData.doc, docData.doc.documentElement, topWindow, baseURI, characterSet, this.config,
@@ -100,7 +107,7 @@
 					});
 		},
 		setDocContent : function(docData, content, callback) {
-			var that = this;
+			var selectedDocData, that = this;
 
 			function buildPage(docData, setFrameContent, getContent, callback) {
 				function setContent(docData) {
@@ -155,8 +162,13 @@
 					if (docData.parent)
 						docData.parent.setChild(docData);
 				});
+				if (this.frameDocData) {
+					selectedDocData = this.frameDocData;
+					selectedDocData.parent = null;
+				} else
+					selectedDocData = this.top;
 				if (this.config.processInBackground)
-					buildPage(this.top, function(docData, callback) {
+					buildPage(selectedDocData, function(docData, callback) {
 						docData.parent.docFrames[docData.index].setAttribute("src", "data:text/html;charset=utf-8,"
 								+ encodeURI(singlefile.util.getDocContent(docData.doc)));
 						delete docData.doc;

+ 6 - 0
WebContent/core/scripts/content/content.js

@@ -158,6 +158,8 @@
 	}
 
 	function sendFgProcessInit(title, url, baseURI, winId, winIndex) {
+		var contextmenuTime = window.contextmenuTime;
+		window.contextmenuTime = null;
 		bgPort.postMessage({
 			processInit : true,
 			pageId : pageId,
@@ -166,11 +168,14 @@
 			title : title || doc.title,
 			baseURI : baseURI || doc.baseURI,
 			winId : winId || wininfo.winId,
+			contextmenuTime : contextmenuTime,
 			index : winIndex || wininfo.index
 		});
 	}
 
 	function sendBgProcessInit(content, title, url, baseURI, characterSet, winId, winIndex) {
+		var contextmenuTime = window.contextmenuTime;
+		window.contextmenuTime = null;
 		bgPort.postMessage({
 			processInit : true,
 			pageId : pageId,
@@ -182,6 +187,7 @@
 			characterSet : characterSet || doc.characterSet,
 			canvasData : canvasData,
 			winId : winId || wininfo.winId,
+			contextmenuTime : contextmenuTime,
 			index : winIndex || wininfo.index
 		});
 	}

+ 5 - 2
WebContent/core/scripts/content/wininfo.js

@@ -140,7 +140,6 @@ var wininfo = {};
 		delete message.contentRequestId;
 		delete message.getContentResponse;
 		contentRequestCallbacks[id](message);
-		delete contentRequestCallbacks[id];
 	}
 
 	function initRequest(message) {
@@ -221,6 +220,10 @@ var wininfo = {};
 	if (window == top)
 		wininfo.getContent = getContent;
 	chrome.extension.onRequest.addListener(onRequest);
+	addEventListener("contextmenu", function() {
+		window.contextmenuTime = (new Date()).getTime();
+	}, false);
+
 	addListener(onMessage);
 
-})();
+})();

+ 19 - 8
WebContent/ui/scripts/bg/background.js

@@ -46,7 +46,7 @@
 		return !url.indexOf("https://chrome.google.com") == 0 && (url.indexOf("http://") == 0 || url.indexOf("https://") == 0);
 	}
 
-	function process(tabId, url, processSelection) {
+	function process(tabId, url, processSelection, processFrame) {
 		var SINGLE_FILE_CORE_EXT_ID = dev ? "onlinihoegnbbcmeeocfeplgbkmoidla" : "jemlklgaibiijojffihnhieihhagocma";
 		detectExtension(SINGLE_FILE_CORE_EXT_ID, function(detected) {
 			if (detected) {
@@ -54,6 +54,7 @@
 					singlefile.ui.notifyProcessInit(tabId);
 					chrome.extension.sendRequest(SINGLE_FILE_CORE_EXT_ID, {
 						processSelection : processSelection,
+						processFrame : processFrame,
 						id : tabId,
 						config : singlefile.config.get()
 					});
@@ -78,6 +79,7 @@
 			});
 	}
 
+	// TODO : onSelectionChanged, getSelected are deprecated
 	chrome.tabs.onSelectionChanged.addListener(function() {
 		chrome.tabs.getSelected(null, function(tab) {
 			notifyProcessable(tab.id, tab.url);
@@ -87,7 +89,8 @@
 		notifyProcessable(tab.id, tab.url);
 	});
 	chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
-		notifyProcessable(tab.id, tab.url, true);
+		if (changeInfo.status = "loading")
+			notifyProcessable(tab.id, tab.url, true);
 	});
 	chrome.tabs.onRemoved.addListener(function(tabId) {
 		singlefile.ui.notifyTabRemoved(tabId);
@@ -129,25 +132,33 @@
 				closeBanner : true
 			});
 		else
-			process(sender.tab.id, sender.tab.url);
+			process(sender.tab.id, sender.tab.url, false, false);
 	});
 	chrome.browserAction.onClicked.addListener(function(tab) {
-		process(tab.id, tab.url);
+		process(tab.id, tab.url, false, false);
 	});
 
 	singlefile.refreshMenu = function() {
 		if (singlefile.config.get().displayInContextMenu) {
 			chrome.contextMenus.create({
-				title : "Process page with SingleFile",
+				contexts : [ "page" ],
+				title : "process page",
 				onclick : function(info, tab) {
-					process(tab.id, tab.url);
+					process(tab.id, tab.url, false, false);
+				}
+			});
+			chrome.contextMenus.create({
+				contexts : [ "frame" ],
+				title : "process frame",
+				onclick : function(info, tab) {
+					process(tab.id, tab.url, false, true);
 				}
 			});
 			chrome.contextMenus.create({
 				contexts : [ "selection" ],
-				title : "Process selection with SingleFile",
+				title : "process selection",
 				onclick : function(info, tab) {
-					process(tab.id, tab.url, true);
+					process(tab.id, tab.url, true, false);
 				}
 			});
 		} else

+ 1 - 0
WebContent/ui/scripts/bg/ui.js

@@ -64,6 +64,7 @@
 			refreshBadgeProperty("path", chrome.browserAction.setIcon);
 		}
 
+		// TODO : getAllInWindow is deprecated
 		if (tabId)
 			refreshTabBadge(tabId);
 		else