Skip to content

Instantly share code, notes, and snippets.

@rsoury
Last active August 10, 2016 05:35
Show Gist options
  • Save rsoury/c608ea85f02b7dc30cdeb19bf9d7510e to your computer and use it in GitHub Desktop.
Save rsoury/c608ea85f02b7dc30cdeb19bf9d7510e to your computer and use it in GitHub Desktop.
Node Code Injection for HTML5/FTP website. Requires Backup of original for resets. Made for ZipMoney.
import fs from 'fs';
import finder from 'fs-finder';
import cheerio from 'cheerio';
import colors from 'colors';
import S from 'string';
//To refresh target folder, rm -rf target, mkdir target, cp -a backup/. target
// npm Script: "reset": "rm -rf target && mkdir target && cp -a backup/. target" -> npm run reset
console.log('Starting node Widgeter Script...'.green);
finder.from('./target/').findFiles('*.html', files => {
files.forEach(file => {
fs.readFile(file, 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
//console.log(file);
let $ = cheerio.load(data);
if($('#mainmenu').length){
$('<li></li>')
.attr({'class': 'menu-item current-menu-ancestor'})
.html('<a href="zippay.html">Interest Free</a>')
.insertAfter('#mainmenu > li:nth-child(4)');
}
//Go through all pages but only do the below if they are the product pages.
if(file.indexOf('sup-range-') > -1){
const price = () => {
const ele = $('.sc_toggles_content').find(' > p > strong:nth-child(1)');
const price = S(ele.text()).between('$', 'P').s;
console.log(`Price: ${price || 20} | File: ${file}`);
return price || '20';
};
$('head').append(`
<!-- Zipmoney inclusion. -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="//d3k1w8lx8mqizo.cloudfront.net/lib/js/zm-widget-js/dist/zipmoney-widgets-v1.min.js"></script>
<style>
.zipmoney-widget-wrapper{
margin: -20px 0 20px 0
}
.zip-hover{
display: inline-block !important;
margin-left:5px;
vertical-align:0px;
}
</style>
`);
$('<div></div>').attr({class: 'zipmoney-widget-wrapper'}).html(`
<div data-zm-merchant="*YOUR_ZIPMONEY_UNIQUE_ID*" data-env="production"></div>
<a
id="shopify-prod-calc"
data-zm-widget="repaycalc"
data-zm-amount="${price()}"
data-zm-logo="https://d3k1w8lx8mqizo.cloudfront.net/INTEGRATIONS/2016/zippay/logos/zippay-grey-black.png"></a>
<a id="zip-learnmore" class="zip-hover" zm-widget="popup" zm-popup-asset="termsdialog" style="display:inline-block;cursor: pointer;">Learn More</a>
`).insertAfter('.post_author_title');
}
//Perma save changes.
fs.writeFile (file, $.html(), function(err) {
if(err){
console.log(err);
return;
}
console.log(`${file}`.green);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment