在firefox附加组件中检测TabMove

时间:2015-10-08 21:07:39

标签: firefox-addon firefox-addon-sdk

试了一下impl。添加或删除标签时在https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser#Notification中给出

跟踪' tabmove'。没有工作。

非常感谢这方面的任何帮助。

BTW,已经尝试过下面的代码了。只有' TabOpen'收到活动。 ' TabClos​​e'和' TabMove'不起作用:

var browserWindows = require("sdk/windows").browserWindows;
var activeWindow = browserWindows ? browserWindows.activeWindow : {};
var browserWindow = activeWindow ? require("sdk/view/core").viewFor(activeWindow) : {};
var DOMWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);

var exampleTabAdded = function(event) {
	var browser = DOMWindow.gBrowser.getBrowserForTab(event.target);
	console.log('tab added: '+ event.target);
};
var exampleTabMoved = function(event) {
	var browser = DOMWindow.gBrowser.getBrowserForTab(event.target);
	console.log('tab moved: '+ event.target);
};

function exampleTabRemoved(event) {
  var browser = gBrowser.getBrowserForTab(event.target);
  console.log('tab removed: '+ event.target);
}


function exampleTabSelected(event) {
  var browser = gBrowser.getBrowserForTab(event.target);
  console.log('tab selected: '+ event.target);
}

var container = DOMWindow.gBrowser.tabContainer;
container.addEventListener("TabMove", exampleTabMoved, false);
container.addEventListener("TabOpen", exampleTabAdded, false);
container.addEventListener("TabClose", exampleTabRemoved, false);
container.addEventListener("TabSelect", exampleTabSelected, false);

由于

1 个答案:

答案 0 :(得分:0)

这似乎有效。使用sdk api和XUL内容文档。 如果有更好的方法来处理'tabmove',请发布答案。

var tabs = require("sdk/tabs");
var { modelFor } = require("sdk/model/core");
var { viewFor } = require("sdk/view/core");
var tab_utils = require("sdk/tabs/utils");

var contentDocumentMap = new Map();

function mapHighLevelTabToLowLevelContentDocument(tab) {
	var lowLevelTab = viewFor(tab);
	var browser = tab_utils.getBrowserForTab(lowLevelTab);
	return browser.contentDocument;
}

function onOpen(tab) {
	tab.on("pageshow", logShow);
	tab.on("activate", logActivate);
	tab.on("deactivate", logDeactivate);
	tab.on("close", logClose);
}

function logShow(tab) {
	var contentWindow = mapHighLevelTabToLowLevelContentDocument(tab);
	if ((contentWindow.URL === 'about:newtab') || (contentWindow.URL === 'about:blank')) {
		return;
	}
	if (contentDocumentMap.has(contentWindow)) {
		return;
	}
	contentDocumentMap.set(contentWindow, tab.id.toString());
}

function logActivate(tab) {
	var contentWindow = mapHighLevelTabToLowLevelContentDocument(tab);
	if ((contentWindow.URL === 'about:newtab') || (contentWindow.URL === 'about:blank')) {
		return;
	}

	if (contentDocumentMap.has(contentWindow)) {
		return;
	}
	contentDocumentMap.set(contentWindow, tab.id.toString());
}

function logDeactivate(tab) {
	setTimeout(function() {
		var windows = require("sdk/windows").browserWindows;
		for (let window of windows) {
			var activeTabContentWindow = mapHighLevelTabToLowLevelContentDocument(window.tabs.activeTab);
			var activeTabId = window.tabs.activeTab.id.toString();
			if ((contentDocumentMap.has(activeTabContentWindow)) && (contentDocumentMap.get(activeTabContentWindow) !== activeTabId)) {
				console.log('M O V E D. url: '+ window.tabs.activeTab.url);
				console.log('from tabid: '+ contentDocumentMap.get(activeTabContentWindow) + ' to tabid: ' + activeTabId);
				contentDocumentMap.delete(activeTabContentWindow);
				contentDocumentMap.set(activeTabContentWindow, activeTabId);
			}
		}
	}, 150);
}



function logClose(tab) {
	var targetTabId = tab.id.toString();
	setTimeout(function() {
		var windows = require("sdk/windows").browserWindows;
		for (let window of windows) {
			var activeTabContentWindow = mapHighLevelTabToLowLevelContentDocument(window.tabs.activeTab);
			var activeTabId = window.tabs.activeTab.id.toString();
			if (contentDocumentMap.has(activeTabContentWindow)) {
				if (contentDocumentMap.get(activeTabContentWindow) !== activeTabId) {
					console.log('M O V E D. url: '+ window.tabs.activeTab.url);
					console.log('from tabid: '+ contentDocumentMap.get(activeTabContentWindow) + ' to tabid: ' + activeTabId);
					contentDocumentMap.delete(activeTabContentWindow);
					contentDocumentMap.set(activeTabContentWindow, activeTabId);
				}
				else if (targetTabId === activeTabId){
					contentDocumentMap.delete(activeTabContentWindow);
				}
			}
		}
	}, 150);
}

tabs.on('open', onOpen);