Custom FireFox Extension

Discussion in 'Programming' started by T0PS3O, Jul 22, 2005.

  1. #1
    Would it be possible to code a custom FF extension that by just clicking the button, prints to a pre-configured printer a snippet of text from the page it's on?

    I basically need to print an address label. I can place unique, identifying tags around the address I need printed but I want to reduce the number of clicks invloved to a minimum. So with this solution I was hoping to reduce it to one click.

    Would this be technically possible?

    And while we're at it, can you make it? :)
     
    T0PS3O, Jul 22, 2005 IP
  2. wkw

    wkw Well-Known Member

    Messages:
    205
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    148
    #2
    Probably. I think what you'd have to do is grab the selected text then create a new web page formatted with that text, suitable for printing. Both of these techniques I know are possible based on various FF plug-ins I use.

    I started writing a couple FF plugins a while back but got distracted before finishing. But I suggest you locate a couple plugins which do these two things (grab selection, create new custom page) and pick apart their source code. A lot of what I learned came from this technique.
     
    wkw, Jul 22, 2005 IP
  3. dkalweit

    dkalweit Well-Known Member

    Messages:
    520
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    150
    #3
    It's open source, of course it's possible! :) Feasable? who knows...


    --
    Derek
     
    dkalweit, Jul 22, 2005 IP
  4. mystikmedia

    mystikmedia Jedi Master

    Messages:
    5,564
    Likes Received:
    498
    Best Answers:
    0
    Trophy Points:
    270
    #4
    Off topic, but PM me the info on text link advertising on your NES ROMs site. I saw from the site you have an option for it.
     
    mystikmedia, Jul 22, 2005 IP
  5. someonewhois

    someonewhois Peon

    Messages:
    177
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not sure if this is 100% what you want, but it's a bookmarklet (a lot quicker to do than an extension).
    javascript:(function(){ /* Get text */ var txt = window.getSelection(); /* hide EVERYTHING */ var el = document.createElement('style'); el.setAttribute('type', 'text/css'); el.innerHTML = '@media print { body { display: none; } #showforprint { display: block; } }'; document.getElementsByTagName('head').item(0).appendChild(el); /* show the text we selected */ var nd = document.createElement('div'); nd.setAttribute('id', 'showforprint'); nd.innerHTML = txt; document.getElementsByTagName('html').item(0).appendChild(nd); /* and print */ window.print(); })();
    Code (markup):
    You select something, click that, then hit enter, and it'll print. Hopefully it does what you're looking for.
     
    someonewhois, Jul 22, 2005 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sounds good. But I'd like to skip having to go to a custom page. I can custom format the text on the initial page if need be, no problem. It's only a back-end order processing page, so if it can;t take it from the current address being displayed I can make a custom prepared snippet somewhere else on the page, possibly even hidden if need be.
     
    T0PS3O, Jul 22, 2005 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Wow! Hold on a second though, I know nothing whatsoever about anything that isn't PHP/HTML.

    Where would that go? Embedded in the page? How do I get the clickable 'button' or whatever it is? How do I skip having to select the printer (we have 4 but the 5th will be the label printer)?
     
    T0PS3O, Jul 22, 2005 IP
  8. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's like the "Blog This" button...

    Stick it on your toolbar...
     
    SEbasic, Jul 22, 2005 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    My new colleague knows his JS stuff, it actually goes in the html header, with a button where you want it to go.
     
    T0PS3O, Jul 22, 2005 IP
  10. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I have it working now and that is such a nice solution whilst I was thinking all complicated.

    My colleague is going to adjust it so the text will have a div tag around it and that way you don't have to select the text first, it will be pre-loaded.

    One issue left. Skipping the print dialogue window. Can I pre define the chosen printer's ID somehow?
     
    T0PS3O, Jul 22, 2005 IP
  11. someonewhois

    someonewhois Peon

    Messages:
    177
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #11
    What you should've done for this is right clicked your bookmarks toolbar, clicked "New Bookmark", then put that exact code into the "Location" field. You don't want to put it in the HTML header.

    I don't think JavaScript can bipass the print dialogue: If it could, I could write a script that wastes all your ink. ;)

    Oh, and to modify it to be a div, change:
    var txt = window.getSelection();
    Code (markup):
    Into:
    var txt = document.getElementById('YourIdGoesHere').innerHTML;
    Code (markup):
    Very easy fix.
     
    someonewhois, Jul 22, 2005 IP
    T0PS3O likes this.
  12. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks again!

    I figured it would be better to have it in the html embedded as I have now since that way it will work in all of our staff's broweser without being dependant on browser settings (as I initially envisaged). Because I now stuck it in the code, it's available to all, in FireFox as well as IE without any intervention necessary from individual's parts first.

    Or do you reckon this has a downside I overlooked? You explicitly state "You don't want to put it in the HTML header.", why is that?
     
    T0PS3O, Jul 23, 2005 IP
  13. someonewhois

    someonewhois Peon

    Messages:
    177
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I was under the impression you needed it for multiple pages. If you're only using it on a single page (or dynamically added to multiple pages), it realy doesn't matter. :)
     
    someonewhois, Jul 23, 2005 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Yeah it's for one page, the order processing page, only.

    Thanks for your ideas, they've been a great help.

    Now off to buy a labeling printer.
     
    T0PS3O, Jul 23, 2005 IP
  15. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Besides the printer choice, one issue remains.

    It prints without any formatting.

    And address that's on the page in a <td> like this:

    <td class="main">Mr John Smith <br>1a Street Road<br>Town, County<br>PO5 TC0, Great Britain<br /><br />
    				<INPUT TYPE="button" NAME="myButton" VALUE="Select Text & Print Label" onClick="print_label()">
    				</td>
    Code (markup):
    Comes out as plain text in one line like this:

    I've tried adding \n or \r behind each line but to no avail.

    Any ideas on how to keep formatting?
     
    T0PS3O, Jul 23, 2005 IP