-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcosdna.js
210 lines (166 loc) · 6.12 KB
/
cosdna.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
/*!
* CosDNA Compare - Tool to compare product ingredients on cosdna.com
*/
var cosDnaCompare = (function ($) {
// Variables
// -------------------------------------------------------------------
var loaded = false;
var message = {
prompt: 'Enter the URL for the CosDNA product you wish to compare',
matches: '{#} exact matching ingredients found!',
noMatches: 'No exact matching ingredients found!',
domain: 'Sorry! This tool currently only works for cosDNA.com!',
nullInput: 'You need to enter a product to compare this to!',
other: 'Oops! Something went wrong! :('
};
// Init - Anything you want to happen onLoad (usually event bindings)
// -------------------------------------------------------------------
var init = function () {
var hostname = window.location.hostname;
// Make sure we're on CosDNA
if( hostname.indexOf('cosdna.com') >= 0 ){
// Only load custom styles if this is our first run on the page
if( !loaded ) addStyles();
// Get product 2 from user
var product2 = prompt( message.prompt );
// Make sure the user filled out the prompt
if(product2){
hostname = $('<a>').attr('href', product2).attr('hostname');
// Make sure they entered a CosDNA link into the prompt
if (hostname.indexOf('cosdna.com') >= 0 && product2 != null) {
compareProducts(product2);
} else {
alert( message.domain );
}
} else {
alert( message.nullInput );
}
} else {
alert( message.domain );
}
};
// FUNCTIONS
// ===================================================================
// Add Styles - Adds custom CSS classes for display purposes
// -------------------------------------------------------------------
var addStyles = function(){
var customCSS = $("<style>").attr("type", "text/css").appendTo("head");
customCSS.append(
".cc-match{ \
background:#FFEDCB; \
} \
\
.cc-overview{ \
clear:both; \
font-family:Verdana, Arial, Helvetica, sans-serif; \
padding:5px; \
background:#FFEDCB; \
border:1px solid #E3BC74; \
text-align:center; \
} \
"
);
};
// Compare Products - Kickoff function for the comparison
// -------------------------------------------------------------------
var compareProducts = function( url ){
// Get the ingredients on this page
var product1 = getIngredients( $('html') );
$('.iStuffTable tr').removeClass('cc-match');
// Fetch the page we're comparing
$.ajax({
type: 'GET',
url: url,
error: function(xhr,status,error) {
alert( message.other );
},
success: function(data, status, xhr) {
var vDom = document.createElement( 'div' );
vDom.innerHTML = data;
// Get the ingredients from the other page
var product2 = getIngredients( $(vDom) );
// Find matches
var output = matchArrays( product1.sort(), product2.sort() );
// Show the comparison overview
showOverview( $(vDom), url, output.matching );
// Show matches
showMatches( output.matching );
loaded = true;
}
});
};
// Show Overview - Show the product comparison from the AJAX fetched page
// -------------------------------------------------------------------
var showOverview = function( $dom, url, matches ){
var overview = $dom.find('.ProdTitle').text();
// Add overview block if this is our first time running on the page
if( !loaded ) $('<div class="cc-overview">').insertBefore('.iStuffTable');
// Build link to second product
var link = $('<a>').attr('target', '_blank').attr('href', url).text( overview );
// Add the number of matches with the link to the other product
$('.cc-overview').empty().html( "<strong>" + matches.length + "</strong> matches with: " ).append( link );
};
// Get Ingredients - Takes DOM node containing ingredients table and spits out an array
// -------------------------------------------------------------------
var getIngredients = function( $dom ){
var results = [];
$dom.find('.iStuffTable .iStuffETitle').each(function(){
results.push($(this).text())
});
return results;
};
// Splice Value - Remove item from array by value
// -------------------------------------------------------------------
var spliceValue = function( arr, val ){
var index = arr.indexOf( val );
if (index >= 0) {
arr.splice( index, 1 );
}
return arr;
};
// Match Arrays - Compares the ingredient arrays and returns an object of matches
// -------------------------------------------------------------------
var matchArrays = function(list1, list2){
var match = [],
diffList1 = list1.slice(),
diffList2 = list2.slice();
for(var i = 0; i < list1.length; i++){
var product = list1[i];
var matched = list2.indexOf( product );
if( matched >= 0 ){
match.push(product);
// Remove it from the other arrays so we have more diff data
spliceValue(diffList1, product);
spliceValue(diffList2, product);
}
}
return {
matching: match,
list1: diffList1,
list2: diffList2
};
};
// Show Matches - Highlights matching ingredients in the table on the page
// -------------------------------------------------------------------
var showMatches = function(matches){
if( matches.length > 0 ){
$('.iStuffTable .iStuffETitle').each(function(){
var matched = matches.indexOf( $(this).text() );
if( matched >= 0 ){
$(this).parent().addClass('cc-match');
}
});
//alert( message.matches.replace('{#}', matches.length) );
} else {
//alert( message.noMatches );
}
};
// CLEANUP
// ===================================================================
// Return - Which variables and objects to make available publicly
// -------------------------------------------------------------------
return {
init : init
};
})(jQuery);
cosDnaCompare.init();