Skip to content

Commit

Permalink
second commit: Chrome Extension adaptation and first version of dedup…
Browse files Browse the repository at this point in the history
…licate
  • Loading branch information
AlexToro committed Nov 5, 2013
1 parent bf29cad commit e1dc4d1
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 19 deletions.
44 changes: 34 additions & 10 deletions RegexAddon.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// @namespace RegexAddon
// @version 1
// @include *
// @require utils.user.js
// @require takeUrls.user.js
// @require phpToJavascriptRegex.user.js
// @resource regexAddonCSS style.css
// @require /home/alex/.mozilla/firefox/pvwmvtab.default/gm_scripts/RegexAddon/utils.user.js
// @require /home/alex/.mozilla/firefox/pvwmvtab.default/gm_scripts/RegexAddon/takeUrls.user.js
// @require /home/alex/.mozilla/firefox/pvwmvtab.default/gm_scripts/RegexAddon/phpToJavascriptRegex.user.js
// @resource regexAddonCSS /home/alex/.mozilla/firefox/pvwmvtab.default/gm_scripts/RegexAddon/style.css
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
Expand All @@ -18,8 +18,11 @@
//////////////////////////////////////////////////// HTML FRAMEWORK ///////////////////////////////////////////////////////////
//////////////////////////////////////////////////// HTML FRAMEWORK ///////////////////////////////////////////////////////////

var newCSS = GM_getResourceText ("regexAddonCSS");
GM_addStyle (newCSS);
if (!(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)){
var newCSS = GM_getResourceText ("regexAddonCSS");
GM_addStyle (newCSS);
}


if (window.self === window.top) {
createGlobalDiv();
Expand All @@ -29,7 +32,6 @@ if (window.self === window.top) {
document.getElementById('regexAddonHidden').style.display = "block";
}


function createGlobalDiv() {
var wholeDiv = document.createElement('div');
wholeDiv.id = "wholeRegexAddon";
Expand Down Expand Up @@ -71,7 +73,15 @@ function addResultsTag(element) {
handButton.id = "handButtonRegexAddon";
handButton.className = 'pestanaRegexAddon';
handButton.appendChild(handImage);
handButton.innerHTML += '<code> </code>';

var duplicatesImage = document.createElement('img');
duplicatesImage.id = 'duplicatesImageRegexAddon';
duplicatesImage.src = 'http:https://img30.imageshack.us/img30/7378/i8mv.png';

var duplicatesButton = document.createElement('button');
duplicatesButton.id = "duplicatesButtonRegexAddon";
duplicatesButton.className = 'pestanaRegexAddon';
duplicatesButton.appendChild(duplicatesImage);

var portapapelesImage = document.createElement('img');
portapapelesImage.id = 'portapapelesImageRegexAddon';
Expand All @@ -81,7 +91,6 @@ function addResultsTag(element) {
portapapelesButton.id = "portapapelesButtonRegexAddon";
portapapelesButton.className = 'pestanaRegexAddon';
portapapelesButton.appendChild(portapapelesImage);
portapapelesButton.innerHTML += '<code> </code>';

var resetImage = document.createElement('img');
resetImage.id = 'resetImageRegexAddon';
Expand All @@ -91,11 +100,11 @@ function addResultsTag(element) {
resetButton.id = "resetButtonRegexAddon";
resetButton.className = 'pestanaRegexAddon';
resetButton.appendChild(resetImage);
resetButton.innerHTML += '<code> </code>';

var pestanaDiv = document.createElement('div');
pestanaDiv.id = "pestanaDivRegexAddon";
pestanaDiv.appendChild(handButton);
pestanaDiv.appendChild(duplicatesButton);
pestanaDiv.appendChild(portapapelesButton);
pestanaDiv.appendChild(resetButton);
pestanaDiv.appendChild(resultsExpand);
Expand Down Expand Up @@ -139,6 +148,7 @@ function addRegexHtmlInput() {
addHandButtonListener();
addResetButtonListener();
addPortapapelesButtonListener();
addDuplicatesButtonListener();
}


Expand Down Expand Up @@ -209,6 +219,20 @@ function addResultTopInfoToList(maxResults) {
//////////////////////////////////////////////////// LISTENERS ///////////////////////////////////////////////////////////
//////////////////////////////////////////////////// LISTENERS ///////////////////////////////////////////////////////////

/*
*
*/
function addDuplicatesButtonListener() {
var duplicatesButton = document.getElementById("duplicatesButtonRegexAddon");
duplicatesButton.addEventListener('click', unduplicateAndExecute, true);
}

function unduplicateAndExecute() {
if (unduplicate()){
selectMatch();
}
}

/*
*
* listener of portapapelesButton
Expand Down
10 changes: 10 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manifest_version": 2,
"content_scripts": [ {
"matches": ["http:https://*/*", "https://*/*"],
"js": [ "RegexAddon.user.js","utils.user.js","takeUrls.user.js","phpToJavascriptRegex.user.js"],
"css": ["style.css"]
} ],
"name": "Regex Addon",
"version": "1"
}
7 changes: 7 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_54
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=true
web.root=.
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:https://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http:https://www.netbeans.org/ns/php-project/1">
<name>RegexAddon</name>
</data>
</configuration>
</project>
116 changes: 107 additions & 9 deletions takeUrls.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

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;

/*
*
Expand All @@ -22,6 +27,7 @@ var listener = function(e) {
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");
Expand Down Expand Up @@ -84,7 +90,12 @@ function findParent(tagname, el) {
function resetTakeUrls() {
regexLinksElements = new Array();
regexLinksElementsSC = new Array();
regexLinksElementsSCOriginal = new Array();
regexLinksElementsSCindex = new Array();
regexLinksElementsSCindexOriginal = new Array();
index = 0;
antType = '';
unduplicatedTimes = 0;
}


Expand All @@ -100,12 +111,26 @@ function takeUrls(html) {
handlePosHtml();
if (document.getElementById('handImageRegexAddon').src == "http:https://img96.imageshack.us/img96/1623/sh9v.png") {
document.getElementById('handImageRegexAddon').src = "http:https://img19.imageshack.us/img19/1624/5r7k.png";
document.getElementById('duplicatesButtonRegexAddon').style.display = "none";
document.body.addEventListener('mousedown', blacklistener, false);
document.body.addEventListener('mouseup', blacklistener, false);
document.body.addEventListener('click', listener, 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:https://img96.imageshack.us/img96/1623/sh9v.png";
document.getElementById('duplicatesButtonRegexAddon').style.display = "inline-block";
document.body.removeEventListener('mousedown', blacklistener, false);
document.body.removeEventListener('mouseup', blacklistener, false);
document.body.removeEventListener('click', listener, false);
Expand All @@ -115,6 +140,58 @@ function takeUrls(html) {
return false;
}

function copyArray(array){
var result = new Array();
for (var i = 0; i < array.length; i++){
result[i] = array[i];
}
return result;
}

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;
}

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)
Expand Down Expand Up @@ -276,6 +353,7 @@ function addElementToRegex(regex, element) {
if (breaker) {
breaker = false;
var resultCua = '';
result = result.replace(/[\s\t]+$/, "");
result = result.replace(/(███SPACES███|███TEXT███)+$/, "");
var lastRegex = tagsRegex.length - 1;
var lastElement = tagsElement.length - 1;
Expand Down Expand Up @@ -514,10 +592,10 @@ function getNodePos(node, parentId) {
*/
function handlePosHtml(){
htmlOriginalPos = htmlOriginal;
htmlOriginalPos = htmlOriginalPos.replace(/<script[^>]*>[\s\S]*?<\/script>/ig, '');
htmlOriginalPos = htmlOriginalPos.replace(/<noscript[^>]*>[\s\S]*?<\/noscript>/ig, '');
htmlOriginalPos = htmlOriginalPos.replace(/<\!--[\s\S]*?-->/ig, '');
htmlOriginalPos = htmlOriginalPos.replace(/<xmp[^>]*>[\s\S]*?<\/xmp>/ig, '');
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");
}

/*
Expand Down Expand Up @@ -545,33 +623,53 @@ function getParentElementId(node){
*/
function getNodeSourceCode(node) {
var htmlAux = htmlOriginalPos;
var htmlIndexAux = htmlAux;
regexLinksElementsSCindex[index] = 0;

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");;
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 position = getNodePos(node, parentId);
console.log("Position: " + position);
//console.log("Position: " + position);
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");
if (j == position)
attrRegexIndex = new RegExp(attrRegexIndex, "i");
if (j == position){
htmlAux = htmlAux.replace(attrRegex, "$1");
else
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;
Expand Down

0 comments on commit e1dc4d1

Please sign in to comment.