Firefox scripting language: URL of active Tab and counting open Tabs - firefox-addon

Edit: I was able to go one step further :)
PSEUDO CODE (what I want)
if (CurrentTabURL == empty Tab) and (Tab.Count == 1)
{close Firefox}
else
{close Tab}
MY CODE (only the if statements don't work. Both actions are working)
if (gBrowser.currentURI == "")
and (tabbrowser.browsers.length == 1)
then
goQuitApplication();
else
gBrowser.removeTab(gBrowser.mCurrentTab);
end
This link helped me a lot.

gBrowser.currentURI is an nsIURI instance. If you want to compare the URL to a string you should look at gBrowser.currentURI.spec. The URL of an "empty tab" is about:blank by the way. Also, I guess that you want to use JavaScript? Corrected code:
if (gBrowser.currentURI.spec == "about:blank" && gBrowser.browsers.length == 1)
goQuitApplication();
else
gBrowser.removeTab(gBrowser.selectedTab);

Related

How do I add url links to WinWheel.js image slices?

How do I add a clickable link to any of the image slices so the user can click a slice to go to a url? Currently they can spin the wheel to get a random url choice. I got the random spin part working using “IF” but I got stuck figuring out the image slice click idea.
I'm currently working at the top of page in an area called:
//add code here?
if (clickedSegment) {
clickedSegment.indicatedSegment = "Jane";
window.location = "https://www.mikeloucas.com";
theWheel.draw();
and I have one url working due to the "clickedSegment" but it's active on all the slices and not what I need (I'm try to target Jane) so I'll keep at it unless some has an answer.
Separately but related:
This is one of the image segments I'm trying to add add link to:
{'image' : 'https://mikeloucas.com/wheel/jane.png', 'text' : 'Jane'},
The original tutorial shows how to change the colour of a slice on click, but I’d like to go to a url instead.
Original Click Code: http://dougtesting.net/winwheel/docs/tut15_get_segment_clicked
The thing Im working on: https://codepen.io/mikeloucas/pen/qBarWoP
In my Codepen example I turned off the “IF” load url because its just in the way when testing, but feel free to turn it on see it in action. It’s WAY down the page lol; you’ll see:
if (indicatedSegment.text == "Jane") {
window.location = "https://www.mikeloucas.com";
WAS:
let clickedSegment= theWheel.getSegmentAt(e.clientX, e.clientY);
if (clickedSegment) {
clickedSegment.indicatedSegment = "Jane";
window.location = "https://www.mikeloucas.com";
theWheel.draw();
IS NOW:
let segmentImage = theWheel.getSegmentAt(e.clientX, e.clientY);
if (segmentImage.text == "Jane") {
window.location = "https://www.mikeloucas.com";}
else if (segmentImage.text == "othername")...{
}
...theWheel.draw();
The trick (for me) was changing "clickedSegment" to "segmentImage" because "segmentImage" was already calling the array of names. I also add an "ELSE IF" because I had lots of slices to work work with.
See working thingy here: https://codepen.io/mikeloucas/pen/qBarWoP
My version needs to have the URL and NAME LIST entered twice; it would be cool if someone knows how to do it once (one set instead of two) but maintain both functions of the "CLICK url" and "SPIN url", so I'm still open to advice.
I'd prefer one set over two. :-)

Append strings if not nil

In the view of a rail's app, I want to achieve:
If user's height isn't nil: return the height value with string "cm"
Otherwise: return string "N/A".
I'm wondering if there's a way to do this. The code below:
user.try(:profile_name).try(:push("as Alias")) || "N/A"
isn't working. The part try(:push("cm")) gives me an error. I thought of using the << operator to append strings by using , but I think there should be a neater way to complete this. Is there anyone who can give me a hint?
--another similar example which I want to accomplish but not working:
user.try(:height).try(:to_s).try(:push("cm")) || "N/A"
How about:
(user && user.profile.present?) ? "#{user.profile} as Alias" : 'N/A'

Error using Ruby OR in if

I have this simple condition in my ruby code:
if user.text != ("Empty" || "Damaged")
do something...
the thing is that when it is "Damaged" it still enters the loop, so I have to use this:
if user.text != "Empty"
if user.text != "Damaged"
...
..
What is the correct syntax to make the first one work?
Thanks!
Use this:
unless ["Empty", "Damaged"].include?(user.text)
...
end
The problem with your first approach is that a || b means that: if a != nil then it is a, else it is b. As you can see it is good for variables, not constants as they are rarely nil. Your expression ("Empty" || "Damaged") simply equals "Empty".
You want to use the this as a binary operator, the correct form would be:
if (user.text != "Empty") && (user.text != "Damaged")
The upper solution is just shorter. It consists of an array of elements you want to avoid, and you just simply check is the user.text is not present in it.
#Matzi has the right answer, for sure. But here's why what you're doing is failing:
Your statement ("Empty" || "Damaged") evaluates to "Empty"
You're saying return either one of these, whichever non-false thing you find first. In Ruby, any string, number or Object returns true, so you return "Empty" every time.
A better way to lay it out if you really want to use an if statement:
if user.text != "Empty" && user.text != "Damaged"
...
end
I assume, by the way, that you're trying to say "If the user text is neither Damaged nor Empty".
Hope that helps.
if ((text != "Empty") && (text != "Damaged"))

How to create link in grails webflow without execution state in url?

I want to display an image in my webflow wich comes from my domain object.
The domain object has an byte[] Array holding an image. The "image" method in my controller delivers the image to browser. This works fine.
In my webflow I'm doing this:
<img src="${createLink(controller:'shop', action:'image', id:shopInstance.id)}">
I can see the image in my frontend (in browser) but it reloads each time i click "next" or change my state in webflow because the image url contains a param that changes each event in webflow.
The created image url (example above) looks like this:
http://localhost:8080/project/shop/image/2?execution=e2s5
I don't want that the execution param is delivered into my image url. How can i fix this?
My guess is that this is a bug. A work around I used is the following
${createLink(controller: 'controller', action: 'action').replaceAll(/\?.*$/, "")}
This will use a regex to remove the execution parameter.
Not sure why you're getting that execution param but you could try: <img src="${resource(dir: 'shop/image', file: shopInstance.id)}"> and make sure the UrlMappings.groovy file has a mapping for 'ship/image' to the proper controller.
As of Grails 2.0.4, look at ApplicationTagLib.groovy, you can see
if (request['flowExecutionKey']) {
params."execution" = request['flowExecutionKey']
urlAttrs.params = params
if (attrs.controller == null && attrs.action == null && attrs.url == null && attrs.uri == null) {
urlAttrs[LinkGenerator.ATTRIBUTE_ACTION] = GrailsWebRequest.lookup().actionName
}
}
So Grails is forcing every link rendered within webflow to include that execution params. It looks like bug to me.

ruby: how avoid if-then-else when doing case-insensitive comparison that handles nil too

I frequently find myself having to do an extra nil check (to avoid throwing an 'undefined for nil' error) that I'm pretty sure is unnecessary.
In one common example, when looking at optional url params, e.g. which might or might not exist, I like them to be case insensitive (if they exist).
for example one of my controller methods allows (doesn't require) &showdate=true and I like to make it user friendly by not caring about true vs TRUE vs True.
I could just say
#showdate = params[:showdate] == "true"
but that assumes lower case.
If I do this:
#showdate = params[:showdate].lower == "true"
it of course throws an unwanted error if showdate is missing from the url becasue nil.lower throws an error.
So I end up doing something like this LOTS of time in my code:
if params[:showdate]
#showdate = params[:showdate].lower == "true"
else
#showdate = false (or maybe nil)
I know there's a Better Way, just don't know what it is. Any help would be appreciated.
#showdate = params[:showdate].to_s.downcase == "true"
Rails?
#showdate = params[:showdate].try(:downcase) == "true"
or
#showdate = !!params[:showdate] && params[:showdate].downcase == "true"
You could do it this way:
#showdata = (params[:showdate] || '').downcase == 'true'

Resources