-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterate.php
174 lines (139 loc) · 5.13 KB
/
iterate.php
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
<?php
ini_set( 'display_errors', 1 );
/** Detect if we are running in windows */
define( 'WIN', strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' );
//region Terminal Color Codes (BASH CODES)
define( 'COLOR_BLACK', !WIN ? "\033[0;30m" : "" );
define( 'COLOR_RED', !WIN ? "\033[0;31m" : "" );
define( 'COLOR_GREEN', !WIN ? "\033[0;32m" : "" );
define( 'COLOR_YELLOW', !WIN ? "\033[0;33m" : "" );
define( 'COLOR_BLUE', !WIN ? "\033[0;34m" : "" );
define( 'COLOR_PURPLE', !WIN ? "\033[0;35m" : "" );
define( 'COLOR_CYAN', !WIN ? "\033[0;36m" : "" );
define( 'COLOR_WHITE', !WIN ? "\033[0;37m" : "" );
define( 'COLOR_RESET', !WIN ? "\033[0m" : "" );
//endregion
$doNotWant = [ "html", "body" ]; // We do not want these dom nodes
$currentLevel = 0; // Current level in dom iteration
$parentNames = array(); // Parent names
$usedNames = array(); // Used names for checkName
$toEcho = ""; // Dom tree map
$js = ""; // Built javascript code
/**
* Checks if the object name is used previously
* Returns a new name if needed
* @param string $name
* @param int $number
* @return string
*/
function checkName ( $name, $number = 0 )
{
global $usedNames;
// Convert UTF8 to ASCII for variable name
$name = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $name );
// We only want these characters in our variable names
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
$regex = sprintf( '/[^%s]/u', preg_quote( $characters, '/' ) );
$name = preg_replace( $regex, '', $name );
// Sometimes things can go wrong
if ( !strlen( $name ) ) {
$name = "domElement";
}
if ( in_array( ( $number ? $name . $number : $name ), $usedNames ) ) {
$number++;
return checkName( $name, $number );
}
$nName = ( $number ? $name . $number : $name );
array_push( $usedNames, $nName );
return $nName;
}
/**
* Iterates the given DOM element
* @param DOMNode $domNode
*/
function iterateDOM ( DOMNode $domNode )
{
global $currentLevel, $js, $parentNames, $doNotWant, $lastNodeName, $toEcho;
foreach ( $domNode->childNodes as $node ) {
if ( $node->nodeType === XML_ELEMENT_NODE ) {
if ( in_array( $node->nodeName, $doNotWant ) ) {
if ( $node->hasChildNodes() ) {
iterateDOM( $node );
}
break;
}
$toEcho .= COLOR_CYAN . str_repeat( " ", $currentLevel ) . $node->nodeName;
if ( $node->hasAttribute( "data-jsid" ) )
$toEcho .= COLOR_GREEN . " | " . COLOR_PURPLE . "JSID: " . $node->getAttribute( "data-jsid" );
if ( $node->hasAttribute( "style" ) )
$toEcho .= COLOR_GREEN . " | Style: " . $node->getAttribute( "style" );
if ( $node->hasAttribute( "class" ) )
$toEcho .= COLOR_GREEN . " | Class: " . $node->getAttribute( "class" );
if ( $node->hasAttribute( "src" ) )
$toEcho .= COLOR_GREEN . " | Src: " . $node->getAttribute( "src" );
$toEcho .= "\n";
if ( $node->hasAttribute( "data-jsid" ) ) {
$name = trim( $node->getAttribute( "data-jsid" ) );
} else if ( $node->hasAttribute( "class" ) ) {
$nameArr = explode( " ", $node->getAttribute( "class" ) );
$name = trim( array_shift( $nameArr ) );
} else {
$name = $node->nodeName;
}
$name = checkName( $name, 0 );
$parentNames[ $currentLevel ] = $name;
$lastNodeName = $name;
$js .= "const {$name} = document.createElement('{$node->nodeName}');\n";
if ( $node->hasAttributes() ) {
foreach ( $node->attributes as $attr ) {
$attrName = $attr->nodeName;
$attrValue = $attr->nodeValue;
if ( $attrName != "data-jsid" )
$js .= "{$name}.setAttribute('{$attrName}','{$attrValue}');\n";
}
}
if ( $currentLevel > 0 ) {
$pName = $parentNames[ $currentLevel - 1 ];
$js .= "{$pName}.appendChild({$lastNodeName});\n";
}
$js .= "\n";
}
if ( $node->nodeType === XML_TEXT_NODE && $node->textContent && strlen( $node->textContent ) ) {
$toEcho = trim( $toEcho );
$toEcho .= COLOR_YELLOW . " | Content: " . $node->textContent . "\n";
$js = trim( $js );
$js .= "\n{$lastNodeName}.innerHTML = \"" . $node->textContent . "\";\n";
$js .= "\n";
}
$currentLevel++;
if ( $node->hasChildNodes() ) {
iterateDOM( $node );
}
$currentLevel--;
}
}
// Multiline user input
echo COLOR_WHITE . "Paste HTML -- Write !q to continue -- : \n";
$stdin = fopen( 'php://stdin', 'r' );
$html = "";
while ( $f = fgets( STDIN ) ) {
$f = trim( $f );
if ( $f != "!q" ) {
$html .= $f;
} else {
break;
}
}
echo COLOR_RESET;
file_put_contents( "temp.html", $html );
$dom = new DOMDocument;
@$dom->loadHTMLFile( __DIR__ . "/temp.html" );
iterateDOM( $dom );
@unlink( "temp.html" );
echo $toEcho;
echo COLOR_YELLOW;
echo str_repeat( "-", 50 ) . "\n";
echo $js;
echo str_repeat( "-", 50 ) . "\n";
echo COLOR_RESET;
@file_put_contents( "dom.js", $js );