Reputation: 768
I have written a Firefox extension that requires the background document's URL. Normally, JavaScript's document.URL
could achieve this - but this is different.
Please see my example below:
As can be seen, there are 4 tabs open:
And, the page currently being viewed is StackOverflow.com (.. indeed).
My question is: how can I retrieve the URL of the user's active window? (i.e. http://www.stackoverflow.com).
Below is the panel.html
code.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href=panel.css rel="stylesheet" type="text/css">
</head>
<body>
<header><h3>Where aM I</h3></header>
This Mozilla extension will display the current <i>background</i> URL
<main>
<fieldset>
<legend>Click the Button</legend>
<button onclick="PageViewing()">Fetch</button>
</fieldset>
</main>
<script>
function PageViewing() {
alert(document.URL);
}
</script>
</body></html>
EDIT
If placed in the main.js
file, this code snippet works:
var tabs = require("sdk/tabs");
console.log("URL of active tab is " + tabs.activeTab.url); //yields SO.com
So in the context of my example, how could I retrieve it from P-Name/lib, for use in the P-Name/data directory - as a variable?
Upvotes: 4
Views: 676
Reputation: 5054
You have to establish a communication protocol between module and content script. This is done with port
.
In your main.js
panel.port.on('askactivetaburl', function(){
panel.port.emit('sentactivetaburl', tabs.activeTab.url);
})
and in your panel script
self.port.on('sentactivetaburl', function(activetaburl){
// work with activetaburl
});
self.port.emit('askactivetaburl');
Upvotes: 2