Skip to content

Commit

Permalink
1. add direct download btn
Browse files Browse the repository at this point in the history
2. fix README.rst
  • Loading branch information
git-hiroki committed Dec 8, 2017
1 parent d266eee commit b889562
Show file tree
Hide file tree
Showing 6 changed files with 2,828 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Usage

Click on the Resurrectio extension icon.

Enter the start URL, and click Go.
Enter the start URL, and click Start Recording.

Then execute your usage scenario, all the events will be recorded.

Expand Down
1 change: 1 addition & 0 deletions casper.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<head>
<title>CasperJS Renderer</title>
<script src="casper.js"></script>
<script src="js-beautify.js"></script>
</head>
<body></body>
</html>
60 changes: 45 additions & 15 deletions casper.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ function CasperRenderer(document) {
this.unamed_element_id = 1;
}

CasperRenderer.prototype.download = function(fileName, content){
var inst = document.createElement("a"),
blob = new Blob([js_beautify(content)], {"type" : "text/javascript"}),
evt = document.createEvent("HTMLEvents");
document.body.innerText = "";
evt.initEvent("click", false, false);
inst.download = fileName || "resurrectio.test.js";
inst.href = URL.createObjectURL(blob);
inst.dispatchEvent(evt);
inst.click();
setTimeout(function(){
window.close();
}, 300);
}

CasperRenderer.prototype.text = function(txt) {
// todo: long lines
this.document.writeln(txt);
Expand Down Expand Up @@ -128,13 +143,15 @@ CasperRenderer.prototype.dispatch = d;

var cc = EventTypes;

CasperRenderer.prototype.render = function(with_xy) {
CasperRenderer.prototype.render = function(with_xy, download) {
this.with_xy = with_xy;
var etypes = EventTypes;
this.document.open();
if(!download){
this.document.writeln('<button id="casperbox-button">Run it on Casperbox</button>');
this.document.write("<" + "pre" + ">");
this.writeHeader();
this.document.write("<" + "pre" + ">");
}
this.writeHeader(download);
var last_down = null;
var forget_click = false;

Expand Down Expand Up @@ -186,17 +203,22 @@ CasperRenderer.prototype.render = function(with_xy) {
if (item.type == etypes.Comment)
this.space();
}
this.writeFooter();
this.document.write("<" + "/" + "pre" + ">");
this.writeFooter();
if(!download){
this.document.write("<" + "/" + "pre" + ">");
}
download && download(document.body.innerText);
this.document.close();
}

CasperRenderer.prototype.writeHeader = function() {
CasperRenderer.prototype.writeHeader = function(download) {
var date = new Date();
this.text("/*==============================================================================*/", 0);
this.text("/* Casper generated " + date + " */", 0);
this.text("/*==============================================================================*/", 0);
this.space();
if(!download){
this.text("/*==============================================================================*/", 0);
this.text("/* Casper generated " + date + " */", 0);
this.text("/*==============================================================================*/", 0);
this.space();
}
this.stmt("var x = require('casper').selectXPath;", 0);
}
CasperRenderer.prototype.writeFooter = function() {
Expand Down Expand Up @@ -502,15 +524,23 @@ CasperRenderer.prototype.postToCasperbox = function() {

var dt = new CasperRenderer(document);
window.onload = function onpageload() {
var with_xy = false;
var with_xy = false,
download = window.location.search=="?download=true";
if(window.location.search=="?xy=true") {
with_xy = true;
}
chrome.runtime.sendMessage({action: "get_items"}, function(response) {
dt.items = response.items;
dt.render(with_xy);
document.getElementById("casperbox-button").onclick = function() {
dt.postToCasperbox();
};
dt.render(with_xy,
download ?
function(content){
dt.download(false, content);
} : false
);
if(!download){
document.getElementById("casperbox-button").onclick = function() {
dt.postToCasperbox();
};
}
});
};
1 change: 1 addition & 0 deletions control.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<input type="submit" class="btn btn-success btn-lg btn-block" id="bgo" value="Start Recording" />
<input type="submit" class="btn btn-primary btn-lg btn-block hide" id="bstop" name="bstop" value="Stop Recording" />
<input type="submit" class="btn btn-default btn-lg btn-block hide" id="bcomment" name="bcomment" value="Add Comment" />
<input type="submit" class="btn btn-primary btn-md btn-block hide" id="bdownload" name="download" value="Download CasperJS" />
<input type="submit" class="btn btn-default btn-md btn-block hide" id="bexport" name="bexport" value="Export CasperJS" />
<input type="submit" class="btn btn-default btn-md btn-block hide" id="bexportxy" name="bexportxy" value="Export CasperJS with (x,y) coords" />
<input type="submit" class="btn btn-default btn-md btn-block hide" id="bdoc" name="bdoc" value="Export doc" />
Expand Down
13 changes: 13 additions & 0 deletions control.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ RecorderUI.prototype.set_started = function() {
e.className += " hide";
e = document.getElementById("recording");
e.className = e.className.replace(/ hide|hide/ig, "");
e = document.getElementById("turl");
e.disabled = true;
e = document.getElementById("bdownload");
e.className += " hide";
chrome.browserAction.setBadgeText({
"text": "REC"
});
Expand Down Expand Up @@ -107,6 +111,10 @@ RecorderUI.prototype.set_stopped = function() {
e.className = e.className.replace(/ hide|hide/ig, "");
e = document.getElementById("recording");
e.className += " hide";
e = document.getElementById("turl");
e.disabled = false;
e = document.getElementById("bdownload");
e.className = e.className.replace(/ hide|hide/ig, "");
chrome.browserAction.setBadgeText({
"text": ""
});
Expand Down Expand Up @@ -153,6 +161,10 @@ RecorderUI.prototype.exportdoc = function(bexport) {
chrome.tabs.create({url: "./doc.html"});
}

RecorderUI.prototype.download = function(){
chrome.tabs.create({url: "./casper.html?download=true"});
}

RecorderUI.prototype.setBtnGoState = function(){
chrome.tabs.getSelected(null, function (tab) {
if(/(chrome|chrome\-extension)\:/.test(tab.url)){
Expand All @@ -176,6 +188,7 @@ window.onload = function(){
document.querySelector('input#bgo').onclick=function() {ui.start(); return false;};
document.querySelector('input#bstop').onclick=function() {ui.stop(); return false;};
document.querySelector('input#bcomment').onclick=function() {ui.showcomment(); return false;};
document.querySelector('input#bdownload').onclick=function() {ui.download(); return false;};
document.querySelector('input#bexport').onclick=function() {ui.export(); return false;};
document.querySelector('input#bexportxy').onclick=function() {ui.export({xy: true}); return false;};
document.querySelector('input#bdoc').onclick=function() {ui.exportdoc(); return false;};
Expand Down
Loading

0 comments on commit b889562

Please sign in to comment.