找到你要的答案

Q:Detecting TabMove in firefox add-on

Q:在Firefox的插件tabmove检测

Tried out the impl. given in : https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser#Notification when a tab is added or removed

for tracking 'tabmove'. Didn't work.

Would appreciate any help in this regard.

BTW, already tried below code. Only 'TabOpen' event is received. 'TabClose' and 'TabMove' does not work:

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);

Thanks

尝试执行。鉴于:https:/ /开发商Mozilla。org /美国/插件/ code_snippets / tabbed_browser #通知标签时,添加或删除

跟踪的tabmove”。没有工作。

感谢这方面的任何帮助。

顺便说一句,已经尝试了以下代码。只有tabopen”事件被接收。”tabclose '和' 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);

谢谢

answer1: 回答1:

This seems to work. Uses both sdk api's as well as XUL content document. If there is a better way to handle 'tabmove', please do post the answer.

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);

This seems to work. Uses both sdk api's as well as XUL content document. If there is a better way to handle 'tabmove', please do post the answer.

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);
firefox-addon  firefox-addon-sdk