Add-ons SDK Mozilla Firefox currentURI - javascript

I work with a https://addons.mozilla.org/en-US/developers/builder (Add-ons builder) and I try to do the following things:
1.How to change currentURI address? Method setTabURL() is not suitable because immediately opens the URL.
While found a way out:
tab.attach ({
contentScript: "history.pushState ('','', '" + tab.url + "');",
});
2.How to get the url address that is entered in the address bar? Method getTabURL() only shows the address at which settled.
3.How to add text to an icon in the toolbar? I use it here: https://builder.addons.mozilla.org/package/166563/

To access the URL bar and it's relate value you have to dig into the browser chrome a bit.
This code snippet will get/set the URL bar value for the currently focused browser window:
var wuntils = require('sdk/window/utils');
var document = wuntils.getMostRecentBrowserWindow().document;
// log the current URL bar value
console.log(document.getElementById("urlbar").value);
// change the current URL bar value (this won't change the page)
document.getElementById("urlbar").value = "Thrift Shop";

Related

Trying to redirect Chrome to new URL using Chrome extension

I am just starting out writing chrome extensions.
y first project: For certain sites, if you click on the extension, the extension will redirect Chrome to the current domainName/archive.
I am using chrome.tabs.update(); to redirect.
If I try to redirect to a hard-coded URL such asYahoo.com, it works correctly.
However, if I try to redirect so what I really want, such as developer.google.com/archive (which I know does not exist), the resulting URL Chrome tries to fetch is:
chrome-extension://injepfpghgbmjnecbgiokedggknlmige/developer.chrome.com/archive
Chrome is prepending the extension's ID to the URL for some reason?
In my background.js, I have the following:
chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update( {url: request.redirect});
I got the code above from this article.
Someone else had the same issue here (but not answered)
The full options.js is:
function createButton () {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
archiveURL = domain + "/archive";
let page = document.getElementById('buttonDiv');
let button = document.createElement('button');
button.addEventListener('click', function() {
chrome.runtime.sendMessage({redirect: archiveURL}); // works if I send "Yahoo.com"
});
button.textContent = "> " + archiveURL; // So I know I am sending the right URL
page.appendChild(button);
})
}
createButton();
SUMMARY:
If I set:
archiveURL = "http://sundxwn.tumblr.com/archive";
the system correctly goes to http://sundxwn.tumblr.com/archive.
But... if I set
archiveURL = domain + "/archive";
and confirm that the button text shows: http://sundxwn.tumblr.com/archive,
Chrome tries to go to: chrome-extension://injepfpghgbmjnecbgiokedggknlmige/sundxwn.tumblr.com/archive
Any help would be truly appreciated.
url.hostname is just that, the hostname. For the original URL https://example.com/some/path, it would be example.com.
Then, your constructed URL is example.com/archive. It does not start with a protocol (e.g. http://) or a slash, so it's interpreted as a relative URL to the current page's URL.
And you are calling this from some extension page, e.g. page.html or the auto-generated background page. Its URL is, for example:
chrome-extension://injepfpghgbmjnecbgiokedggknlmige/page.html
So, to construct an absolute URL from a relative URL, Chrome chops that path off at the last slash and adds your relative part.
You need to specify the protocol so that Chrome understands it's a URL that has nothing to do with the current document. And url.origin does that, it would be https://example.com, which then is properly interpreted.

Get Url From Another Opened Window With Different Origin

I want ask something:
I need to open different site from main window using
window.open(http://different-site.com/new_window_pt1,'New Window')
And I want to make that opened window closed if the url on that window contains token params:
http://different-site.com/new_window_pt2?token=sometokenthingshere
then send that params to main window.
Is it possible?
Thanks in advance.. :)
when you do
var win1 = window.open;
you have the handle to that window already and you can read its URL by
var win1URL = win1.location.href;
and if you are looking for only parameter values after ? then
var win1URLSearch = win1.location.search;
now you can search this value to see if it contains token (didn't get from your question about this search part). If this condition is true then
win1.close();

How can I get the new URL, old URL and transition type when navigating?

I'm trying to write a Chrome extension which triggers when a navigation occurs. I'm using the onComitted event for that, but I can't seem to find a way to get the previous URL of the tab.
chrome.webNavigation.onCommitted.addListener(function(data) {
transitionType = data.transitionType;
newUrl = data.url;
oldUrl = ?
});
What I've tried so far:
Getting the URL of the tab with id data.tabId, but its URL seems to be identical to data.url (i.e. already updated).
Using onBeforeNavigate instead of onComitted, but I could not find the old URL there, plus that event does not have the transition type.
Sticking with onComitted and just getting the previous URL of the current tab, but I could not find any way to do that.
How can I get all three pieces of information from each navigation?
I managed to find a solution. Basically I follow the instructions here to find the previous URL of a tab, and I perform that retrieval also on onComitted, where I can find the new URL and the transition type.

Custom Social Sharing: shareUrl passing to URL but not actually sharing the shareURL

I have five custom share icons for Facebook, Twitter, Google Plus, Pinterest and Email. For each, on click, I would like them to share the current URL. The problem is that, while the shareURL is getting passed to the window URL (such as: https://twitter.com/share?url=shareURL), this shareURL is NOT getting passed into the dialogue box for each social medium. So even thought it appears in the browser URL box, the individual social users aren't actually sharing anything. The dialogue box appears empty.
In my example above, shareURL = thisURL in my script below. Here is a sample of my script:
function share_twitter(thisURL){
window.open("https://twitter.com/share?url=" + encodeURIComponent(thisURL));
}
$(document).ready(function(){
thisURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
$(".twitter").click(function(){
share_twitter(thisURL);
});
My hunch is that this has to do with global variables and the document ready function. Not sure where to go from here.
the arguments is not complete to redirect command, need variable %(cmd) to finish it up..
ex: https://twitter.com/share?url=%{url} , & make sure it has right url path check. try...

Newly opened window shows my dynamic content, but wrong location

My script opens a new window and then writes its content. The destination window displays "Hello World" as expected, but the URL is the same as the window that the script ran in and I don't understand why.
Is there a way to build a new window without picking up the old URL?
function doTest() {
var impl = document.implementation;
var tempDoc = impl.createHTMLDocument("Hello");
var tempBody = tempDoc.getElementsByTagName("body")[0];
var tempDiv = tempDoc.createElement('div');
var tempMsg = tempDoc.createTextNode('Hello World.');
tempDiv.appendChild(tempMsg);
tempBody.appendChild(tempDiv);
var destWindow=window.open("about:blank", "title", null, false);
var destDoc=destWindow.document;
destDoc.open();
destDoc.write("<html>"+tempDoc.documentElement.innerHTML+"</html>");
destDoc.close();
}
var btnTest = document.createElement( 'input' );
btnTest.setAttribute( 'value', 'Test' );
btnTest.setAttribute( 'type', 'button' );
btnTest.addEventListener('click',doTest,true);
document.body.appendChild( btnTest );
I'm using Firefox 20 and Greasemonkey 1.8.
I don't think you can do this. This is probably a security feature; see BugĀ 337344, "Disable location bar hiding by default, to make chrome spoofing harder" and related bugs.
I think that as soon as parent-window javascript tries to alter the content, then the anti-spoofing protection forces the location to the opener's URL. This is true even if you open the window with a URL other than about:blank.
You can hide the location bar by change the setting dom.disable_window_open_feature.location to true in about:config and then hide the address bar by passing location=false in window.open.
But if you do, then the opener's URL will be prepended to the title bar.
You'll probably have to live with this and be grateful that phishing is that much harder. ;-) Or, if you can make a compelling case for why you should be able to alter or blank the location: (1) add it to your question and (2) open a bug or feature request with Mozilla.
Workaround:
You can set your script to also fire on about:blank, and then have GM make the page from the blank instance, not the opening instance. See this answer for an almost identical scenario/solution.
Be sure to open the window with an explicit about:blank, like so:
var destWindow = window.open("about:blank", "title", null);

Categories

Resources