Using a bookmarklet to track a package - javascript

I'm trying to write a bookmarklet that tracks a package in the mail. First it checks to see if the tracking page is open, if not it opens it in a new tab, and then sets the value of the form to the tracking number. Finally, it submits the form. What I'm so far unable to do is set the value of the form in the case where the bookmarklet opens up a new tab.
Here's what I have:
javascript: (function(){
var trackingNumber = "/*tracking number*/";
var a = document.forms.trackingForm;
if ('http://fedex.com/Tracking' == document.location) {
trackingForm.trackNbrs.value = trackingNumber;
document.forms.trackingForm.submit();
}
else {
window.open('http://fedex.com/Tracking');
this.window.onload = function(){ //This seems to be the problem
trackingForm.trackNbrs.value = trackingNumber;
onload(document.forms.trackingForm.submit());
}
}
})();
Any ideas?

window.open opens a new window, so if this is going to work at all (I have little experience with bookmarklets), you would have to address the new window directly. Something like this:
else {
new_window = window.open('http://fedex.com/Tracking');
new_window.onload = function(){
new_window.document.trackingForm.trackNbrs.value = trackingNumber;
new_window.document.forms.trackingForm.submit();
// I didn't get at all what the onload() was for, re-add if necessary
}

Related

Closing a popup window after performing print

I have a requirement tp popup a window display a pdf page, perform silent print and close the same.
String s =
"var win = window.open('PrintPopUp.jsf','_blank',\"height=300,width=200,scrollbars=no," +
"status=no, resizable=no, screenx=0, screeny=0\");" +
"win.onclick= function(){ win.close();}"
I used the above code to get the popup , on click of print I write this code to my page and the following to call a servlet to generate the pdf;
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
RequestDispatcher dispatcher = request.getRequestDispatcher("/hisprintservlet");
My question is this, I have been able to bring up the window, perform silent print but no matter what I do the popup wont close.
I am using IE 11 and the project uses ADF 12c.
Please help..
Checkout the docs on Popups here.
function showPopup(event)
{
event.cancel();
var source = event.getSource();
var popupid="popup";
var popup = AdfPage.PAGE.findComponentByAbsoluteId(popupId);
if (!popup.isPopupVisible())
{
var hints = {};
hints[AdfRichPopup.HINT_LAUNCH_ID] = source.getClientId();
hints[AdfRichPopup.HINT_ALIGN_ID] = source.getClientId();
hints[AdfRichPopup.HINT_ALIGN] = AdfRichPopup.ALIGN_AFTER_START;
popup.show(hints);
}
}
function hidePopup(event)
{
event.cancel();
var source = event.getSource();
var popupId = source.getProperty("popupId");
var isCanceled = source.getProperty("isCanceled");
var popup = AdfPage.PAGE.findComponentByAbsoluteId(popupId);
if (popup.isPopupVisible())
{
if (isCanceled)
popup.cancel();
else
popup.hide();
}
}
...
...
Hello World!
...
...
I found a solution for this.. i added an iframe to my popup window. In the body tag I added the followingg code to close ..
function test(win){if(win==null){alert('null');} else {setTimeout(function(win){this.close();},3000);}}
This works well in IE11

How to dynamically change tab title when it is not selected?

I want to display dynamic information (score) in the window tab of a javascript games running in a browser (chrome) : my goal is to run several instances of the game in different tabs, running in parallel, and to be able to see the current scores in the tab titles. I tried :
document.title = score
... but it works only in the selected tab, the other one are not refreshed until selected (although the games are running well in background).
==> is there a way to force the update of the tab titles... even if not selected ?
I found couple of same questions on the http://stackoverflow.com but they did not work for me.
You can find your solution here: http://www.raymondcamden.com/2010/10/19/Using-JavaScript-to-update-the-browser-window-title-when-the-user-is-away
So, basically that kind of code will work:
var focused = true;
var baseTitle = "";
var chatsMissed = 0;
//I'm the fake function that represents some process. We randomly determine if a new chat happened
function fakeStuff() {
if(Math.random() > 0.5) {
if(!focused) {
chatsMissed++;
window.document.title = baseTitle + " ("+chatsMissed+")";
}
}
}
$(document).ready(function() {
//store the base title
baseTitle = window.document.title;
//When the window is focused...
$(window).focus(function() {
focused = true;
// window.document.title = baseTitle;
//chrome bug: http://heyman.info/2010/oct/7/google-chrome-bug-when-setting-document-title/
setTimeout(function() {
document.title = baseTitle;
}, 100);
chatsMissed = 0;
});
//When the window is blurred...
$(window).blur(function() {
focused = false;
});
//setup a process
window.setInterval('fakeStuff()',2000);
})
Unfortunately JSfiddle do not support title changing. But I tested, and it works.

firefox javascript window.open _self

My problem is:
When I use:
window.open("example.com","_self");
or
self.open("example.com");
or
window.location.href="example.com";
Firefox removes all menus, buttons, window's window minimization buttons, everything. Also context menu stop working, but site opens fine except this chaos, which ruins everything.
So how to fix this?
EDIT:
I'm using FF22, fresh install.
Looks like its not a simple case so I drop here entire code, it's slightly edited addon for creating new tabs from context menu:
let _ = require("l10n").get;
let winUtils = require("window-utils");
let { isBrowser } = require("api-utils/window/utils");
var delegate = {
onTrack: function (window) {
if (isBrowser(window) ){
let menu = window.document.getElementById("tabContextMenu");
let newtab = window.document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","menuitem");
newtab.setAttribute("id", "contexttab-newtab");
newtab.setAttribute("label", _("newtab_string"));
newtab.setAttribute("accesskey", _("newtabaccesskey_string"));
newtab.setAttribute("oncommand", "window.location.href='http://www.example.com'");
menu.insertBefore(newtab, menu.firstChild);
} // End isBrowser
} // End ontrack
} // End delegate function
let tracker = new winUtils.WindowTracker(delegate);
// code to remove the menuitem when extension is disabled for satisfy requirement on AMO for pass a full review
// On uninstall the menuitem is not removed, see: https://bugzilla.mozilla.org/show_bug.cgi?id=627432
exports.onUnload = function(reason) {
var unloader = {
onTrack: function (window) {
if (isBrowser(window) ){
let menu = window.document.getElementById("tabContextMenu");
let newtab = window.document.getElementById("contexttab-newtab");
menu.removeChild(newtab);
}
}
}; // End unloader function
let remover = new winUtils.WindowTracker(unloader);
}
This is the only line I edited:
newtab.setAttribute("oncommand", "window.location.href='http://www.example.com'");
gBrowser.loadURI('http://www.example.com');
works properly.
gBrowser.loadURI loads a page into the selected tab I think.
If you want to open a new window you have to do it like this:
var url = Cc['#mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
url.data = 'http://www.bing.com/';
Services.ww.openWindow(null, 'chrome://browser/content/browser.xul', '_blank', 'chrome,all', url);

Check popup window status

When someone requests a chat, an entry is made in the database. I have an hidden iframe on our dashboard that checks the database every 20 seconds to see if there is a chat and if there is it launches a popup window. Even if the popup is open the iframe still refreshes the popup every 20 seconds. Want I am trying to achieve is a javascript to check the status of the popup. If it is closed I want it to reopen it... if it is open then it bring it into focus... but I dont want the popup to refresh.. as I have an ajax script doing this..
Here is my code:
<script language="javascript" type="text/javascript">
function myOpenWindow(winURL, winName, winFeatures, winObj)
{
var theWin;
if (winObj != null)
{
if (!winObj.closed)
{
winObj.focus();
return winObj;
}
}
else
{
theWin = window.open(winURL, winName, winFeatures);
return theWin;
}
}
</script>
<% IF ChatSessionID <> "" THEN %>
<script language="javascript" type="text/javascript">
var gmyWin = null;
window.onload = function()
{
var w = 900;
var h = 500;
var l = (screen.width-w)/2;
var t = (screen.height-h)/2;
var params = 'status=0,resizable=0,scrollbars=0,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t;
gmyWin = myOpenWindow("/chat/chat_window.asp?ChatSession=<%=ChatSessionID%>&id=3", "myWin", params, gmyWin)
}
</script>
<% END IF %>
Any help you could provide would be greatly appreciated..
Best Regards,
Paul
I am not sure, but I believe if you name the window (e.g. myWin) when you call window.open, then later call window.open again using the same name, it will return the existing window if its already open or open/re-open the window and return a handle to that.
Edit
Ah, there you go -- from window.open:
If a window with the name
strWindowName already exists, then,
instead of opening a new window,
strUrl is loaded into the existing
window. In this case the return value
of the method is the existing window
and strWindowFeatures is ignored.
Providing an empty string for strUrl
is a way to get a reference to an open
window by its name without changing
the window's location. If you want to
open a new window on every call of
window.open(), you should use the
special value _blank for
strWindowName.
I believe according to the above mentioned specs, this might work:
var hChatWindow = window.open("", "ChatWindow", "whatever features"); // url intentionally left blank
// hChatWindow now contains a reference to new, existing or re-opened window
hChatWindow.focus();
if (hChatWindow.location=="about:blank") { // not sure; you need to experiment here
hChatWindow.location = "/chat/chat_window.asp?whatever";
}
Demo here, source here.
Register a callback on gmyWin.onunload.
You will find it tricky to subvert "block pop-up windows" in most browsers. However, if it is disabled, the following will work.
Main window:
var status = false;
function winOpen(){
window.open("child.html");
}
function winStatus(){
alert(status);
}
Pop-up window:
window.opener.status = true;
window.onblur = window.focus;
window.onunload = function(){
window.opener.status = false;
};

How do you make a post request into a new browser tab using JavaScript / XUL?

I'm trying to open a new browser tab with the results of a POST request. I'm trying to do so using a function containing the following code:
var windowManager = Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interface
s.nsIWindowMediator);
var browserWindow = windowManager.getMostRecentWindow("navigator:browser");
var browser = browserWindow.getBrowser();
if(browser.mCurrentBrowser.currentURI.spec == "about:blank")
browserWindow.loadURI(url, null, postData, false);
else
browser.loadOneTab(url, null, null, postData, false, false);
I'm using a string as url, and JSON data as postData. Is there something I'm doing wrong?
What happens, is a new tab is created, the location shows the URL I want to post to, but the document is blank. The Back, Forward, and Reload buttons are all grayed out on the browser. It seems like it did everything except executed the POST. If I leave the postData parameter off, then it properly runs a GET.
Build identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1
Something which is less Mozilla specific and should work reasonably well with most of the browsers:
Create a hidden form with the fields set up the way you need them
Make sure that the "target" attribute of the form is set to "_BLANK"
Submit the form programatically
The answer to this was found by shog9. The postData parameter needs to be a nsIMIMEInputStream object as detailed in here.
try with addTab instead of loadOneTab, and remove the last parameter.
Check out this page over at the Mozilla Development Center for information on how to open tabs.
You could use this function, for example:
function openAndReuseOneTabPerURL(url) {
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser");
// Check each browser instance for our URL
var found = false;
while (!found && browserEnumerator.hasMoreElements()) {
var browserInstance = browserEnumerator.getNext().getBrowser();
// Check each tab of this browser instance
var numTabs = browserInstance.tabContainer.childNodes.length;
for(var index=0; index<numTabs; index++) {
var currentBrowser = browserInstance.getBrowserAtIndex(index);
if ("about:blank" == currentBrowser.currentURI.spec) {
// The URL is already opened. Select this tab.
browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];
// Focus *this* browser
browserInstance.focus();
found = true;
break;
}
}
}
// Our URL isn't open. Open it now.
if (!found) {
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
// Use an existing browser window
recentWindow.delayedOpenTab(url, null, null, null, null);
}
else {
// No browser windows are open, so open a new one.
window.open(url);
}
}
}

Categories

Resources