-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtakeUrls.user.js
795 lines (735 loc) · 25.6 KB
/
takeUrls.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// ==UserScript==
// @name takeUrls
// @namespace takeUrls
// @version 1
// @grant none
// author: Alex Toro - [email protected]
var regexLinksElements = new Array();
var regexLinksElementsSC = new Array();
var regexLinksElementsSCOriginal = new Array();
var regexLinksElementsSCindex = new Array();
var regexLinksElementsSCindexOriginal = new Array();
var index = 0;
var htmlOriginal = '';
var htmlOriginalPos = '';
var antType = 0;
var unduplicatedTimes = 0;
/*
*
* @param {event} e
*
* listener that catch click events in order to handle the element you clicked.
*/
var listener = function(e) {
var element = findParent('A', e.target);
if (element && element.nodeName == 'A') {
regexLinksElements[index] = element;
regexLinksElementsSC[index] = getNodeSourceCode(element);
regexLinksElementsSCOriginal[index] = regexLinksElementsSC[index];
console.log(regexLinksElementsSC[index]);
element.removeAttribute("onclick");
element.removeAttribute("onmousedown");
element.removeAttribute("onmouseup");
element.outerHTML = highlightHtml(element.outerHTML); //IMPORTANTE NO HACERLO ANTES
index++;
}
cancelEvent(e);
};
/*
*
* @param {event} e
*
* listener that catch click events only to cancel them.
*/
var blacklistener = function(e) {
cancelEvent(e);
};
/*
*
* @param {event} event
*
* cancel event default behavior
*/
function cancelEvent(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
/*
*
* @param {String} tagname
* @param {node} el
* @returns {node} el
*
* find first parent with tagName [tagname]
*/
function findParent(tagname, el) {
if ((el.nodeName || el.tagName).toLowerCase() === tagname.toLowerCase()) {
return el;
}
while (el = el.parentNode) {
if ((el.nodeName || el.tagName).toLowerCase() === tagname.toLowerCase()) {
return el;
}
}
return null;
}
/*
*
* reset global elements on user's demand.
*/
function resetTakeUrls() {
regexLinksElements = new Array();
regexLinksElementsSC = new Array();
regexLinksElementsSCOriginal = new Array();
regexLinksElementsSCindex = new Array();
regexLinksElementsSCindexOriginal = new Array();
index = 0;
antType = '';
unduplicatedTimes = 0;
}
/*
*
* @param {String} html
* @returns {Boolean}
*
* main function that starts or stops the regex suggester on user's demand
*/
function takeUrls(html) {
htmlOriginal = html;
handlePosHtml();
if (document.getElementById('handImageRegexAddon').src == "http://img96.imageshack.us/img96/1623/sh9v.png") {
document.getElementById('handImageRegexAddon').src = "http://img19.imageshack.us/img19/1624/5r7k.png";
document.getElementById('duplicatesButtonRegexAddon').style.display = "none";
document.body.addEventListener('mousedown', listener, false);
document.body.addEventListener('mouseup', blacklistener, false);
document.body.addEventListener('click', blacklistener, false);
}
else {
if (unduplicatedTimes > 0){
regexLinksElementsSC = copyArray(regexLinksElementsSCOriginal);
regexLinksElementsSCindex = copyArray(regexLinksElementsSCindexOriginal);
var unTimes = unduplicatedTimes;
unduplicatedTimes = 0;
for (var u = 0; u < unTimes; u++){
if (!unduplicate()) {
if (u > 1) alert ("In fact, elements DO HAVE a common pattern but no so many as before");
break;
}
}
}
document.getElementById('handImageRegexAddon').src = "http://img96.imageshack.us/img96/1623/sh9v.png";
document.getElementById('duplicatesButtonRegexAddon').style.display = "inline-block";
document.body.removeEventListener('mousedown', listener, false);
document.body.removeEventListener('mouseup', blacklistener, false);
document.body.removeEventListener('click', blacklistener, false);
document.getElementById('regexInput').value = getSuggestedRegex();
return true;
}
return false;
}
/*
* @param {Array} array
* @returns {Array} result
*
* make a copy of an Array
*/
function copyArray(array){
var result = new Array();
for (var i = 0; i < array.length; i++){
result[i] = array[i];
}
return result;
}
/*
*
* @returns {Boolean}
*
* Tries to improve regex by adding elements before it
*/
function unduplicate() {
antType = '';
if (regexLinksElementsSC[0]) {
var auxArray = copyArray(regexLinksElementsSC);
for (var i = 0; i < regexLinksElementsSC.length; i++){
var element = regexLinksElementsSC[i];
var e = addPreviousElement(element, i);
if (e){
element = e;
regexLinksElementsSC[i] = element;
}
else {
regexLinksElementsSC = copyArray(auxArray);
alert ("Elements do not have a common pattern");
return false;
}
}
unduplicatedTimes++;
document.getElementById('regexInput').value = getSuggestedRegex();
return true;
}
else{
alert ("No elements on selection for doing this");
}
return false;
}
/*
* @param {String} element
* @param {Integer} pos
* @returns {String} element
*
* returns the previus tag and text before the element
*/
function addPreviousElement(element, pos) {
if (regexLinksElementsSCindex[pos]){
var htmlAux = htmlOriginalPos;
htmlAux = htmlAux.substring(0,regexLinksElementsSCindex[pos]);
var piece = htmlAux.replace(/^[\s\S]*(<(\/?[\w\\-]+)[^>]*>[^><]*)$/i,"$1");
var type = htmlAux.replace(/^[\s\S]*(<(\/?[\w\\-]+)[^>]*>[^><]*)$/i,"$2");
// alert (antType + " " + type);
if (antType != type && antType != '') return false;
antType = type;
element = piece + element;
regexLinksElementsSCindex[pos] = regexLinksElementsSCindex[pos] - piece.length;
}
return element;
}
/*
*
* @returns {String} (regex)
*
* handle element by element of user's selection in order to make a regex
*/
function getSuggestedRegex() {
if (regexLinksElementsSC[0]) {
var regex = regexLinksElementsSC[0];
for (var i = 1; i < regexLinksElementsSC.length; i++) {
var element = regexLinksElementsSC[i];
regex = addElementToRegex(regex, element);
}
regex = finishSuggestedRegex(regex);
return "@" + regex + "@isU";
}
}
/*
*
* @returns {String} result (regex)
*
* auxiliar function to end properly the regex. It replaces some special text coming from main constructor.
*/
function finishSuggestedRegex(regex) {
regex = escapeRegex(regex);
var result = '';
var tags = regex.match(/<\/?\w[^>]*>/g);
var text = regex.split(/<\/?\w[^>]*>/g);
for (var i = 0; i < tags.length; i++) {
result += finishSuggestedRegexTag(tags[i]);
if (text[i + 1]){
text[i + 1] = text[i + 1].replace(/^[\s\t]+/, "███SPACES███");
text[i + 1] = text[i + 1].replace(/[\s\t]+$/, "███SPACES███");
result += finishSuggestedRegexText(text[i + 1]);
}
}
return result;
}
/*
*
* @returns {String} regex
*
* escape especial characters of the regex
*/
function escapeRegex(regex) {
regex = regex.replace(/(\!|\$|\(|\)|\*|\+|\.|\[|\]|\?|\\|\^|\{|\||@)/g, "\\" + "$1");
return regex;
}
/*
*
* @returns {String} tag
*
* do the finishSuggestedRegex function for the tags of the regex. <a href="...">
*/
function finishSuggestedRegexTag(tag) {
var attributes = tag.match(/\s* [\w\\-]+\s*=\s*["']/g);
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attrIni = (attributes[i]);
var comaType = attrIni[attrIni.length - 1 ];
var attrRegex = attrIni + '[^' + comaType + ']*' + comaType;
attrRegex = new RegExp(attrRegex, "i");
var thisAttr = tag.match(attrRegex);
if (thisAttr[0]) {
var thisReplace = thisAttr[0].replace(/███ATTR███/g, "[^" + comaType + "]*");
thisReplace = thisReplace.replace(/^\s* /, ' [^>]*');
tag = tag.replace(thisAttr[0], thisReplace);
}
}
if (tag.match(/^<\w/)) {
tag = tag.replace(/[\s\n]*>$/, "[^>]*>");
}
}
else {
attributes = tag.match(/\s* [\w\\-]+\s*=/);
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attrIni = attributes[i];
var comaType = attrIni[attrIni.length - 1];
var attrRegex = attrIni + '\s*[^"\'\s>]+';
attrRegex = new RegExp(attrRegex, "i");
var thisAttr = tag.match(attrRegex);
if (thisAttr[0]) {
var thisReplace = thisAttr[0].replace(/███ATTR███/g, "[^\\s>]*");
thisReplace = thisReplace.replace(/^\s* /, ' [^>]*');
tag = tag.replace(thisAttr[0], thisReplace);
}
}
}
if (tag.match(/^<\w/)) {
tag = tag.replace(/[\s\n]*>$/, "(?: [^>]*)?>");
}
}
return tag;
}
/*
*
* @returns {String} text
*
* do the finishSuggestedRegex function for the texts of the regex. ..>Submit here<..
*/
function finishSuggestedRegexText(text) {
text = text.replace(/███TEXT███/g, "[^<]*");
text = text.replace(/███NOT_MATCHING███/g, ".*");
text = text.replace(/███SPACES███/g, "\\s*");
return text;
}
/*
*
* @param {String} regex
* @param {String} element
* @returns {String} result
*
* function that compares previous "regex" with next element in order to incorporate it to the regex.
* first time you call the function, regex is not a regex, but another element.
* the result is not a well formet regex... it's an internal format that is handled after all the process ends.
*/
function addElementToRegex(regex, element) {
var result = '';
var tagsRegex = regex.match(/<\/?\w[^>]*>/g);
var textRegex = regex.split(/<\/?\w[^>]*>/g);
var tagsElement = element.match(/<\/?\w[^>]*>/g);
var textElement = element.split(/<\/?\w[^>]*>/g);
var limit = Math.min(tagsRegex.length, tagsElement.length);
var i = 0;
var breaker = false;
for (i = 0; i < limit; i++) {
//HANDLE TAGS
var currentRegexTag = tagsRegex[i];
var currentElementTag = tagsElement[i];
if (currentRegexTag.match(/<\//)) { //TAG TANCAR
if (takeTagType(currentRegexTag) == takeTagType(currentElementTag)) {
result += currentRegexTag;
}
else {
breaker = true;
}
}
else { //TAG OBRIR
if (takeTagType(currentRegexTag) == takeTagType(currentElementTag)) {
var attrs = takeAttributes(currentRegexTag);
for (var a = 0; a < attrs.length; a++) {
var currentAttr = attrs[a];
if (currentElemAttr = takeAttribute(currentElementTag, currentAttr)) {
currentRegexTag = currentRegexTag.replace(currentAttr, addTextRegex(currentAttr, currentElemAttr, "ATTR"));
}
else {
currentRegexTag = currentRegexTag.replace(currentAttr, '');
}
}
result += currentRegexTag;
}
else {
breaker = true;
}
}
var textIndex = i + 1;
if (!breaker && (textIndex < textRegex.length - 1)) {
//HANDLE TEXT
var currentRegexText = textRegex[textIndex];
if (textElement[textIndex]) {
var currentElementText = textElement[textIndex];
}
else
currentElementText = '';
result += addTextRegex(currentRegexText, currentElementText, "TEXT");
}
if (breaker)
break;
}
if (breaker) {
breaker = false;
var resultCua = '';
result = result.replace(/[\s\t]+$/, "");
result = result.replace(/(███SPACES███|███TEXT███)+$/, "");
result = result.replace(/[\s\t]+$/, "");
var lastRegex = tagsRegex.length - 1;
var lastElement = tagsElement.length - 1;
while (i <= lastRegex && i <= lastElement) {
//HANDLE TAGS
var currentRegexTag = tagsRegex[lastRegex];
var currentElementTag = tagsElement[lastElement];
if (currentRegexTag.match(/<\//)) { //TAG TANCAR
if (takeTagType(currentRegexTag) == takeTagType(currentElementTag)) {
resultCua = currentRegexTag + resultCua;
}
else {
breaker = true;
}
}
else { //TAG OBRIR
if (takeTagType(currentRegexTag) == takeTagType(currentElementTag)) {
var attrs = takeAttributes(currentRegexTag);
for (var a = 0; a < attrs.length; a++) {
var currentAttr = attrs[a];
if (currentElemAttr = takeAttribute(currentElementTag, currentAttr)) {
currentRegexTag = currentRegexTag.replace(currentAttr, addTextRegex(currentAttr, currentElemAttr, "ATTR"));
}
else {
currentRegexTag = currentRegexTag.replace(currentAttr, '');
}
}
resultCua = currentRegexTag + resultCua;
}
else {
breaker = true;
}
}
var textIndex = lastRegex;
var textIndexElement = lastElement;
if (!breaker && (textIndex > 0)) {
//HANDLE TEXT
var currentRegexText = textRegex[textIndex];
if (textElement[textIndexElement]) {
var currentElementText = textElement[textIndexElement];
}
else
var currentElementText = "";
resultCua = addTextRegex(currentRegexText, currentElementText, "TEXT") + resultCua;
}
if (breaker)
break;
lastRegex--;
lastElement--;
}
resultCua = resultCua.replace(/^[\s\t]+/, "");
resultCua = resultCua.replace(/^(███SPACES███|███TEXT███)+/, "");
resultCua = resultCua.replace(/^[\s\t]+/, "");
result += '███NOT_MATCHING███' + resultCua;
}
result = result.replace(/(███NOT_MATCHING███)+/g, '███NOT_MATCHING███');
result = result.replace(/(███TEXT███)+/g, '███TEXT███');
result = result.replace(/(███SPACES███)+/g, '███SPACES███');
return result;
}
/*
*
* @param {String} stringTag
* @param {String} currentAttr
* @returns {Boolean}
*
* search for an attribute type (currentAttr) on the stringTag that is a string like: <a href="#" title="TT">
* if you search for title it will return title="TT"
*/
function takeAttribute(stringTag, currentAttr) {
var attribute = currentAttr.match(/ [\w\\-]+\s*=\s*["']/);
if (attribute) {
var attrIni = attribute[0];
var comaType = attrIni[attrIni.length - 1];
var attrRegex = attrIni + '[^' + comaType + ']*' + comaType;
attrRegex = new RegExp(attrRegex, "i");
var thisAttr = stringTag.match(attrRegex);
if (thisAttr) {
return thisAttr[0];
}
}
else {
attribute = currentAttr.match(/ [\w\\-]+\s*=/);
if (attribute) {
var attrIni = attribute[0];
var comaType = attrIni[attrIni.length - 1];
var attrRegex = attrIni + '\s*[^"\'\s>]+';
attrRegex = new RegExp(attrRegex, "i");
var thisAttr = stringTag.match(attrRegex);
if (thisAttr) {
return thisAttr[0];
}
}
}
return false;
}
/*
*
* @param {String} stringTag
* @returns {Array} attrs
*
* returns an array with every attribute for a tag like: <a href="#" title="TT">
* E.g: href="#", title="TT"
*/
function takeAttributes(stringTag) {
var attrs = new Array();
var attributes = stringTag.match(/ [\w\\-]+\s*=\s*["']/g);
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attrIni = (attributes[i]);
var comaType = attrIni[attrIni.length - 1 ];
var attrRegex = attrIni + '[^' + comaType + ']*' + comaType;
attrRegex = new RegExp(attrRegex, "i");
var thisAttr = stringTag.match(attrRegex);
attrs.push(thisAttr[0]);
}
}
var attributes = stringTag.match(/ [\w\\-]+\s*=\s*[^"'\s>]+/g);
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var thisAttr = (attributes[i]);
attrs.push(thisAttr);
}
}
return attrs;
}
/*
*
* @param {String} stringTag
* @returns {String}
*
* returns a tag type for a tag like: <img src="#" title="TT">
* E.g: it will return img
*/
function takeTagType(stringTag) {
var tagRegex = '</?(\\S+)(>| [^>]*>)';
tagRegex = new RegExp(tagRegex, "i");
var tag = stringTag.match(tagRegex);
return tag[1].toLowerCase();
}
/*
*
* @param {String} regex
* @param {String} html
* @param {String} type
* @returns {String} result
*
* The same function as addElementToRegex but in this case you have only a piece of text without tags.
* This function does not only handle text like >Click here< but also text from attributes values.
*/
function addTextRegex(regex, text, type) {
if (regex == '███NOT_MATCHING███'){
return regex;
}
var innerRegex = "";
var innerTEXT = "";
var originalRegex = "";
if (!regex && !text)
return "";
originalRegex = innerRegex = regex;
innerTEXT = text;
var startRegex = '';
var endRegex = '';
while (innerRegex[0] && innerTEXT[0] && innerRegex[0].toLowerCase() == innerTEXT[0].toLowerCase()) {
startRegex += innerRegex[0];
innerRegex = innerRegex.substring(1);
innerTEXT = innerTEXT.substring(1);
}
var regexlastIndex = innerRegex.length - 1;
var textlastIndex = innerTEXT.length - 1;
while (innerRegex[regexlastIndex] && innerTEXT[textlastIndex] && innerRegex[regexlastIndex].toLowerCase() == innerTEXT[textlastIndex].toLowerCase()) {
endRegex = innerRegex[regexlastIndex] + endRegex;
innerRegex = innerRegex.substring(0, regexlastIndex);
innerTEXT = innerTEXT.substring(0, textlastIndex);
regexlastIndex = innerRegex.length - 1;
textlastIndex = innerTEXT.length - 1;
}
var result = '';
if (innerRegex.match(/███NOT_MATCHING███/)){
result = startRegex + "███NOT_MATCHING███";
}
else if (text != originalRegex) {
result = startRegex + "███" + type + "███" + endRegex;
}
else
result = startRegex + endRegex;
return result;
}
/*
*
* @param {node} node
* @param {String} parentId
* @returns {Number}
*
* return the "position" of a node taking as a parent determined by id as referer
* the position is not the tag position... is the position according with the rest of elements with the same type.
* E.g: <div><a><b><span><a thatisourelement>
* In this case it is in second position... because only have another a before it.
*/
function getNodePos(node, parent) {
var cointainer = '';
if (parent){
cointainer = parent;
}
else {
cointainer = document.getElementsByTagName('body')[0];
}
var tags = cointainer.getElementsByTagName(node.tagName);
for (var i = 0; i < tags.length; i++) {
if (tags[i] == node) {
return i;
}
}
return 0;
}
/*
*
* makes an auxiliar html without some tags in order to determine better the position of an element in the real source code.
*/
function handlePosHtml(){
htmlOriginalPos = htmlOriginal;
htmlOriginalPos = htmlOriginalPos.replace(/(<script[^>]*>)[\s\S]*?(<\/script>)/ig, "$1███NOT_MATCHING███$2");
htmlOriginalPos = htmlOriginalPos.replace(/(<noscript[^>]*>)[\s\S]*?(<\/noscript>)/ig, "$1███NOT_MATCHING███$2");
htmlOriginalPos = htmlOriginalPos.replace(/(<\!--)[\s\S]*?(-->)/ig, "$1███NOT_MATCHING███$2");
htmlOriginalPos = htmlOriginalPos.replace(/(<xmp[^>]*>)[\s\S]*?(<\/xmp>)/ig, "$1███NOT_MATCHING███$2");
}
/*
*
* @param {node} node
* @returns {String} (node id)
*
* return the id of the first parent element with id
*/
function getParentElementId(node){
var idunique = true;
if (node.id) idunique = checkIdIsUnique(node.id);
while (node && (!node.id || !idunique) && node.nodeName != 'BODY') {
node = node.parentNode;
if (node.id) idunique = checkIdIsUnique(node.id);
}
if (node && node.id && node.nodeName != 'BODY'){
return node.id;
}
}
function getParentElement(node){
var idunique = true;
if (node.id) idunique = checkIdIsUnique(node.id);
while (node && (!node.id || !idunique) && node.nodeName != 'BODY') {
node = node.parentNode;
if (node.id) idunique = checkIdIsUnique(node.id);
}
if (node && node.id && node.nodeName != 'BODY'){
return node;
}
}
/*
*
* @param {String} id
* @returns {Boolean}
*
* check if an ID is unique
* HTML rules says that ids must be unique, but there are some HTML that are wrong and uses duplicated ids
* this function is used only for handle these wrong HTMLs
*/
function checkIdIsUnique(id){
var htmlAux = htmlOriginalPos;
var attrRegex = '<[^>]* id\\s*=\\s*[\'"]?' + id + '[\'"]?(?:>| [^>]*>)';
attrRegex = new RegExp(attrRegex, "ig");
var match = htmlAux.match(attrRegex);
if (match){
if (match.length > 1){
return false;
}
return true;
}
}
/*
*
* @param {node} node
* @returns {String} htmlAux
*
* tries to return the real source code of a given node
*/
function getNodeSourceCode(node) {
var htmlAux = htmlOriginalPos;
var htmlIndexAux = htmlAux;
regexLinksElementsSCindex[index] = 0;
if (document.getElementById('domButtonRegexAddon').innerHTML == "USING DOM!"){
//WITH DOM... we only have to do this
regexLinksElementsSCindex[index] = htmlAux.indexOf(node.outerHTML);
return node.outerHTML;
}
var parentId = getParentElementId(node);
if (parentId){
var attrRegex = ' id\\s*=\\s*([\'"]?)' + parentId;
attrRegex = new RegExp(attrRegex, "i");
var attrRegexIndex = '';
var comaType = htmlAux.match(attrRegex);
if (comaType && comaType[1]){
comaType = comaType[1];
attrRegex = '^[\\s\\S]*?(<[^>]* id\\s*=\\s*'+ comaType + parentId + comaType + '[^>]*>)';
attrRegexIndex = '^([\\s\\S]*?)(<[^>]* id\\s*=\\s*'+ comaType + parentId + comaType + '[^>]*>)[\\s\\S]*$';
}
else{
attrRegex = '^[\\s\\S]*?(<[^>]* id\\s*=\\s*'+ parentId + '(>| [^>]*>))';
attrRegexIndex = '^([\\s\\S]*?)(<[^>]* id\\s*=\\s*'+ parentId + '(>| [^>]*>))[\\s\\S]*$';
}
attrRegex = new RegExp(attrRegex, "i");
attrRegexIndex = new RegExp(attrRegexIndex, "i");
htmlAux = htmlAux.replace(attrRegex, "$1");
htmlIndexAux = htmlIndexAux.replace(attrRegexIndex, "$1");
regexLinksElementsSCindex[index] = regexLinksElementsSCindex[index] + htmlIndexAux.length;
}
var parent = getParentElement(node);
var position = getNodePos(node, parent);
console.log("Position: " + position + " - Partent: " + parentId);
for (var j = 0; j <= position; j++) {
htmlIndexAux = htmlAux;
var attrRegex = '^[\\s\\S]*?(<' + node.tagName + '[\\s\\n][^>]*>)';
var attrRegexIndex = '^([\\s\\S]*?)(<' + node.tagName + '[\\s\\n][^>]*>)[\\s\\S]*$';
attrRegex = new RegExp(attrRegex, "i");
attrRegexIndex = new RegExp(attrRegexIndex, "i");
if (j == position){
htmlAux = htmlAux.replace(attrRegex, "$1");
htmlIndexAux = htmlIndexAux.replace(attrRegexIndex, "$1");
}
else{
htmlAux = htmlAux.replace(attrRegex, '');
htmlIndexAux = htmlIndexAux.replace(attrRegexIndex, "$1$2");
}
regexLinksElementsSCindex[index] = regexLinksElementsSCindex[index] + htmlIndexAux.length;
}
if (regexLinksElementsSCindex[index]) regexLinksElementsSCindexOriginal[index] = regexLinksElementsSCindex[index];
htmlAux = getWholeElement(htmlAux, node.tagName);
return htmlAux;
}
/*
*
* @param {String} html
* @param {String} tagName
* @returns {String}
*
* returns a tag element extracted from a string according to it's type_
*
* E.g: <a ...><label>Something<a>lalala</a></label></a><div></body>
* retuns <a ...><label>Something<a>lalala</a></label></a>
*/
function getWholeElement(html, tagName) {
var attrRegex = '';
var obert = 0;
var entra = true;
var attrRegex = '</?' + tagName + '(>| [^>]*>)';
attrRegex = new RegExp(attrRegex, "ig");
while ((obert > 0 || entra) && (match = attrRegex.exec(html))) {
entra = false;
if (match[0].match(/<\//))
obert--;
else
obert++;
}
return html.substring(0, attrRegex.lastIndex);
}