text-replacement-utf-8.js

Forums » General Opera topics » User JavaScript

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

15. July 2006, 14:48:34

profiT

Posts: 453

text-replacement-utf-8.js

And again, few month after publishing on russian operafan.net forum I eventually decided to publish here few of my scripts. Some decent ones I'm not ashamed of.

text-replacement-utf-8.js (discussion on operafan).

Script is encoded in UTF-8, and that is necessary for correct interpetation of non-ASCII and Unicode symbols. First 'unneeded' semicolon is necessary too. Don't change it.

You can add your own replacements like (to replace 'foo' to 'bar'):

'foo','bar',


You can make some formatting in replaced text:
'foo','<b>bar</b>',


But it is recommended to make formatting with CSS in 'replacedText' tag:
'foo','<replacedText style="font-weight:bold">bar</replacedText>',


There are more advanced examples in script.

And make sure that replacement doesn't contain replaced text or you will face recursive and infinite cycle.

It uses DOM iterators to traverse text nodes, so speed is better that it might be, but still is an issue if you have are viewing big pages (>1Mb).
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

15. July 2006, 15:03:47

profiT

Posts: 453

Forgot to give you sample page.

http://my.opera.com/profiT/homes/files/logotyps.htm
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

14. January 2007, 06:21:55

philry4n

suspecting victim

Posts: 4855

how bout changing a text with certain styles? like bold and underlined?

so if in the page source has e.g.
<b><u>J</u></b>
is it changeable?

14. January 2007, 06:41:29

profiT

Posts: 453

That is pretty hard to implement, i guess, because script will be much slower analyzing all source, not just text.

You can do something like:

document.documentElement.innerHTML=document.documentElement.innerHTML.replace(/<b><u>J<\/u><\/b>/ig,"<b><u>replaced!</u></b>");


But speed... Especially in case when you have many replacements...
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

14. January 2007, 09:14:23

philry4n

suspecting victim

Posts: 4855

ok thanks up , what to include to make the script site specific? let's say i want the script only to replace text in certain site(s)

14. January 2007, 13:56:07

philry4n

suspecting victim

Posts: 4855

yeah it happens sometimes, on big sized page

14. January 2007, 14:43:08

philry4n

suspecting victim

Posts: 4855

restart opera? i've seen that trick worked on me bigsmile

15. January 2007, 12:57:25

profiT

Posts: 453

Version 0.3
I've squashed few bugs and tried to improve functionality:

Fixes, to make it work on any page.
Script won't do replacements on a big pages to prevent lags.
Ignoring contents of PRE tags.

Thanks everyone for feedback.

Now, next part is for js-geeks.

To filter some branches from the DOM-tree (to properly ignore all contents of PRE, STYLE, SCRIPT nodes), first i've converted script to TreeWalker but speed degraded by visible extent, so i decided to keep on with NodeIterator, but i had to use data directly attached to DOM nodes, to hash hiearchy information and it's kinda obtrusive...

Those interested may have a look to a TreeWalker version.
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

15. January 2007, 16:22:51

philry4n

suspecting victim

Posts: 4855

how bout my question about only replaces texts on certain sites?

15. January 2007, 18:42:33

xErath

javascript guru

Posts: 6588

Originally posted by profiT:

Now, next part is for js-geeks.

How about a xpath snapshot ?
//text()[not(parent::script) and not(parent::style) and not(ancestor::pre)]
For a collection of user scripts visit
http://my.opera.com/xErath/blog/

15. January 2007, 18:54:53

profiT

Posts: 453

Originally posted by xErath:

How about a xpath snapshot ?


I am writting this script mostly for myself, and i still use 8.52 for my everyday browsing.

But i may add this, even if i won't use it for now...
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

4. February 2007, 08:42:24

xssxss

提高境界中……

Posts: 194

thanks!
留白是一种境界

14. February 2007, 19:13:45

YinYanger

Posts: 918

It works on O 9.1??
Not for me sad
I save it on my Profile\UserJS folder and restarted Opera, but it seems not working :´(

15. February 2007, 15:19:33

philry4n

suspecting victim

Posts: 4855

it works here, maybe you got another userjs that conflicts with this one?

have you set the userjs folder in tools -> preferences -> advanced -> content -> javascript options -> user javascript folder

16. February 2007, 17:26:07

YinYanger

Posts: 918

I don't think so sad
I created a UserJS folder into my own profile folder (C:\Documents and Settings\<MyUserName>\Application Data\Opera\Opera\profile\UserJS\)
Then I copied the text-replacement-utf-8.js file into that folder.
I also setted this path as the user javascript folder.
It's the one file into that folder.
I restarted Opera.
Then I go to the sample page at http://my.opera.com/profiT/homes/files/logotyps.htm but nothing happens! I don't see the text translated into logos sad
I looked the logs of my VirusScan and appears it is NOT blocking me :O

I didn't read the UserJS tutorial, but I guessed it was as easy as the steps I did. I'm wrong?

Opera 9.1 final, WinXP SP2
Any ideas?

16. February 2007, 18:54:59

profiT

Posts: 453

YinYanger
Is Javascript in your F12 menu checked?..

Have you tried relocating your userJS folder (to make it "c:\userJS", for example)?

And your error console (Tools -- Advanced -- Error console)? Does it say anything?

Or maybe site-preferences for my.opera.com?

By the way, i've just updated script a bit. Nothing really useful, just added few embedded images in two logos ('Google', 'Firefox') and support for them to script.
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

22. February 2007, 11:53:44

YinYanger

Posts: 918

After downloading the new version of the script into a new folder c:\userJS, setting this path as the userJS folder and restarted Opera, it worked!
But then I copied the script into my profile again, and setted this folder as the userJS folder (again). After the weekend, I turned ON my PC and it works too!!!
bigeyes confused yes cheers

As stated in my post in the thread "[Req] my.opera.com emoticons everywhere":

I'm using the Opera's IRC client + BitlBee ( http://bitlbee.org - an IRC to other chat networks gateway) for my chat needs.
But then I can't see the smilies I send/receive (besides the few supported by M2)!
So I'm making a custom menu with all the smilies used in MSN messenger and the MyOpera forums. I already got the images, put them into a test skin, and created the menu. I click on the smiley and the menu inserts the proper text.
But then I need a way to also interpret a received text and translate it into a smiley! This is why I want this UserJS working


But seems this JS don´t works on M2 or the IRC client sad I did send a mail to myself with the string "^_^" and it wasn´t translated into a smile.
See the replys of philry4n to my post.
Can you help? Thank you! up

22. February 2007, 12:00:40

philry4n

suspecting victim

Posts: 4855

hey yin yanger it is most certain that the irc client html can not be modified by a js, the converting from : ) into smile smiley is hardcoded, so it's not possible atm, i also heard that this has been asked before

22. February 2007, 12:27:37

YinYanger

Posts: 918

So lets begin a campaign to change this!!! wink
How about using the IM.CSS file for this?

22. February 2007, 12:39:51

philry4n

suspecting victim

Posts: 4855

nope the im.css doesn't handle converting the text into smiley, it has been asked before so i think there's a wishlist for this, lazy to find it lol

22. February 2007, 15:53:53 (edited)

YinYanger

Posts: 918

In an .htm file (even a local .htm file), the script works very well!
I changed a skin, creating a MSN folder into the smilies folder and putting into it all the MSN emoticons.
In the skin.ini file I have a line like this:

Emoticon MSN_embarrassed = smilies/MSN/embarrassed.png


Then I zipped again the skin and make it the current skin.
Then I changed the script to translate '^_^' to the MSN's embarrassed emoticon

'^_^', '<replacedText style=\'display:inline-block; width: -o-skin; height: -o-skin; background-image: -o-skin("Emoticon MSN_embarrassed")\'></replacedText>',


Then I opened a test .htm page with the '^_^' string and it was translated to the correct emoticon!!
But when I see a mail I send to myself with the same string, it wasn't translated. cry

So IT IS possible to use addittional emoticons, but not into M2 (and I guess into the IRC client too) cry

22. February 2007, 16:05:08

philry4n

suspecting victim

Posts: 4855

yes that's what i said, i had added almost all of myopera smiley to my skin, and successfully loaded them in webpages using this userjs and it worked. But in irc, the process of translating smileys are hardcoded, meaning only default smiley codes are supported

9. April 2007, 10:41:16

ssfcu3itx

Posts: 539

In order to apply into M2/IRC, you should edit mime.css smile

9. April 2007, 11:07:12

philry4n

suspecting victim

Posts: 4855

still you can't add new smileys

26. July 2007, 13:13:53

profiT

Posts: 453

New version with enhanced speed and it's now using XPath if available.

selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

20. August 2007, 14:08:02

profiT

Posts: 453

text-replacement-utf-8.js 0.5.
New features:
  • Now replacements have pop-up hints with original text.
  • Clicking on replacement will disable them (all of them on the page), click again -- and they will show up again.
selection2rtf.js -- exporting selection to Word files (with images and some formatting)
Convert2PHF.js -- saving in single htm or mht file

20. April 2008, 15:59:43

serafins

Posts: 204

should be sticky.

Forums » General Opera topics » User JavaScript