473,804 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Greasemonkey: insert an element using regular expression

Hi!

I'm a newbie to JavaScript and Greasemonkey and just started learning
with 'Dive Into Greasemonkey'. I looked for an example on the web, but
unfortunately I'm still missing something...

I have a text file which is a simple database:

Key01 Text01 Url01
Key02 Text02 Url02
....

I'd like to insert after

<a href="anUrl" class="thisClas s">Key01<a>

the element

<a href="Url01">Te xt01<a>

I tried to reuse the "Dumb Quotes" from 'Dive Into Greasemonkey' by
replacing

replacement = {
"Key01":"Ke y01</a><a href=\"Url01\"> Text01",
"Key02":"Ke y02</a><a href=\"Url01\"> Text02"
};

Unfortunately, this doesn't work, since </a><a href...is not
interpreted as a HTML tag.

So I tried this:

nodes = document.evalua te(
"//text()",
document,
null,
XPathResult.UNO RDERED_NODE_SNA PSHOT_TYPE,
null);
for (var i = 0; i < nodes.snapshotL ength; i++) {
node = nodes.snapshotI tem(i);
text = node.data;
for (key in replacements) {
if(s==key)
helpkey=key;
}
newLink = document.create Element('a');
// newLink.href = 'Url0x'; ????
// newLink.appendC hild(document.c reateTextNode(T ext0x)); ???
newLink.parentN ode.insertBefor e(newLink, node.nextSiblin g);
}

I'm really lost. :(

I would be most grateful for any help or ideas...

Alex

Nov 27 '06 #1
2 6404
* Alexander Paul wrote:
Hi!

I'm a newbie to JavaScript and Greasemonkey and just started learning
with 'Dive Into Greasemonkey'. I looked for an example on the web, but
unfortunately I'm still missing something...

I have a text file which is a simple database:

Key01 Text01 Url01
Key02 Text02 Url02
...
(snip)

Looks like a job for XSLT to me.
Nov 28 '06 #2
Alexander Paul wrote:
I have a text file which is a simple database:

Key01 Text01 Url01
Key02 Text02 Url02
...

I'd like to insert after

<a href="anUrl" class="thisClas s">Key01<a>

the element

<a href="Url01">Te xt01<a>
You can use XPath to find the first link e.g.
var link = document.evalua te(
'.//a[@href][. = "Key01"]',
document.body,
null,
XPathResult.FIR ST_ORDERED_NODE _TYPE
null
).singleNodeVal ue;
Then you can create the new link and insert it:
if (link != null) {
var newLink = document.create Element('a');
newLink.href = 'Url01';
newLink.appendC hild(document.c reateTextNode(' Text01'));
link.parentNode .insertBefore(n ewLink, link.nextSiblin g);
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8295
by: Chris Lasher | last post by:
Hello, I would like to create a set of very similar regular expression. In my initial thought, I'd hoped to create a regular expression with a variable inside of it that I could simply pass a string into by defining this variable elsewhere in my module/function/class where I compile the regular expression, thus making for an easy substitution. However, still after JFGI'ing, I still cannot find the syntax to place a variable inside a...
2
5830
by: Newton | last post by:
I'm having some issues with a greasemonkey script i've made (nothing serious, and you'll notice that by the mess). I made it to learn javascript properly at first but it ended handy and now i'm addicted to it.. However, it doesn't quite work like i'd expect it to (and like it used to with older versions) I realise that it may be a greasemonkey-specific question, please bare with me if it is. I figured some of you guys may have used it...
4
5175
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
1
1486
by: Raistlin Majere | last post by:
javascript:(function(){var k,x,t,i,j,p; for(k=0;x=document.links;k+ +){t=x.href.replace(/3A/ig,':').replace(/2f/ ig,'/');i=t.lastIndexOf('http');if(i>0){ t=t.substring(i); j=t.indexOf('&'); if(j>0)t=t.substring(0,j); p=/https?\:\/\/*]/.exec(unescape(t)); if(p) x.href=p; } else if (x.onmouseover&&x.onmouseout){x.onmouseover(); if (window.status && window.status.indexOf('://')!=-1)x.href=window.status; x.onmouseout(); } x.onmouseover=null;...
3
3666
XtinaS
by: XtinaS | last post by:
I'm trying to write a script for Greasemonkey that will, in LiveJournal, replace a placeholdered embedded YouTube thing with a link to the video. In LiveJournal, you can set an option to have a placeholder instead of the actual embedded YouTube video. This is what that code looks like: <div class="LJ_Placeholder_Container" style="width: 475px; height: 405px;"> <div class="LJ_Placeholder_HTML" style="display:...
1
1926
by: nmccart | last post by:
Greetings. I have recently discovered the Grease Monkey add-in for FireFox. It has a lot of potential, and I am really enjoying using it to simplify and automate some repetitive tasks I perform on a regular (several times a day) basis. Here is what I cannot figure out: One site I visit (PHP) generates a list of links based on certain conditions (time of day, luck of the draw, who knows?). I want to click a SPECIFIC link if it exists. If...
5
2232
by: antar2 | last post by:
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = list2 = Suppose that I want to substitute the vowels from list2 that are in list1, into for example 'u'.
14
4996
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.