473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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="thisClass">Key01<a>

the element

<a href="Url01">Text01<a>

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

replacement = {
"Key01":"Key01</a><a href=\"Url01\">Text01",
"Key02":"Key02</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.evaluate(
"//text()",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < nodes.snapshotLength; i++) {
node = nodes.snapshotItem(i);
text = node.data;
for (key in replacements) {
if(s==key)
helpkey=key;
}
newLink = document.createElement('a');
// newLink.href = 'Url0x'; ????
// newLink.appendChild(document.createTextNode(Text0x )); ???
newLink.parentNode.insertBefore(newLink, node.nextSibling);
}

I'm really lost. :(

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

Alex

Nov 27 '06 #1
2 6375
* 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="thisClass">Key01<a>

the element

<a href="Url01">Text01<a>
You can use XPath to find the first link e.g.
var link = document.evaluate(
'.//a[@href][. = "Key01"]',
document.body,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE
null
).singleNodeValue;
Then you can create the new link and insert it:
if (link != null) {
var newLink = document.createElement('a');
newLink.href = 'Url01';
newLink.appendChild(document.createTextNode('Text0 1'));
link.parentNode.insertBefore(newLink, link.nextSibling);
}
--

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
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...
2
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...
4
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...
1
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('&');...
3
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...
1
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...
5
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.