1

So, what I have is a Chrome extension. It scrapes a page looking for certain phrases (such as 'smile') and replaces them with an image stored locally with the same name ('smile').

The problem is, I have ~1k images that are a mixture of .gif, .jpg and .png

So I am not sure of the most efficient way to load up the image with the proper src location.

Idealy I would have it try smile.gif, smile.jpg and smile.png until it found which one is right but I don't want crazy overhead

To clarify, I already know how to detect the reference words in text -> which gives me part of the filename (for example 'smile') -> but I have no way to know at this point whether 'smile' is stored locally as a jpg/png/gif so I can't direct the <img src=.... to the exact right location.

Would it be possible to go through the directory and create some json with filename: extension, and then reference this? I am assuming I can't do this in Javascript though

Any suggestions?

user1079404
  • 308
  • 1
  • 5
  • 15
  • Let me get this straight... You have a bank of smilies and you want to replace every reference to the word smily with one of those images? – Isaiah Taylor Jun 04 '14 at 00:35
  • Until "*it [finds] one which is right*"? How does 'it' assess rightness, the first one that loads successfully/exists? – David Thomas Jun 04 '14 at 00:38
  • If you're just talking about inserting html into a page, you don't have to care about the image type. The browser can handle the standard stuff: jpg, png, gif, etc. – SaganRitual Jun 04 '14 at 00:38
  • Somehow you know a ":)" = "smile". Is this in a database? – cgatian Jun 04 '14 at 00:38
  • @GreatBigBore: He wants to have `smile` replaced by `` or `` etc; he does not know the extension. That said, a simple dictionary explicitly listing image names for strings is a simple solution, and easy to autogenerate. – Amadan Jun 04 '14 at 00:41
  • I just updated the question to try and make it a little more clearer, @Amadan just explained what I typed out and also just suggested the same thing I thought might work, fair enough – user1079404 Jun 04 '14 at 00:43
  • Can't a browser extension do a simple directory listing of a local folder? – SaganRitual Jun 04 '14 at 00:43
  • @GreatBigBore: I don't know. According to [this](http://stackoverflow.com/questions/11452758/chrome-extension-how-to-list-the-contents-of-local-directory), it might not be that easy. – Amadan Jun 04 '14 at 00:48
  • @Amadan Well, at least now we know what he's asking. – SaganRitual Jun 04 '14 at 00:49
  • 3
    I'd change my point of view... What about converting all those images to the same format? (It could be done in batch from command line in seconds) – miguel-svq Jun 04 '14 at 00:50
  • I never considered just changing them all, I suppose having them all as gif would work. Alternatively I feel like the only thing I can do is make some Python script to crawl the image directory, create a file that I can then read into using my extension, turn it into an object and then query the object for extensions. I don't know if that is too elaborate though – user1079404 Jun 04 '14 at 00:52
  • @user1079404 Maybe ImageMagick and a little script run just once is all you need. – miguel-svq Jun 04 '14 at 00:57
  • @miguel-svq if you want to submit an answer I will mark it as correct, batch converting the png/jpg to gif suited my needs in this case and wasn't something I would have considered, thanks – user1079404 Jun 04 '14 at 04:36

0 Answers0