Skip to content

Instantly share code, notes, and snippets.

@nmz787
Forked from kanzure/bookmarklet.js
Last active July 25, 2016 02:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmz787/5652391 to your computer and use it in GitHub Desktop.
Save nmz787/5652391 to your computer and use it in GitHub Desktop.
This bookmarklet downloads the PDF currently being viewed in a browser window then uploads it with a filename of your choice to a different web server. The included Flask-based web server takes the uploaded file and saves it to disk, then returns the resulting URL so the user can copy it with CTRL-C.
function copyToClipboard (text) {
window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);
}
var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
if (this.status == 200) {
var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function() {
switch (xhr2.readyState) {
case 0: // uninitialized
case 1: // loading
case 2: // loading
case 3: // interactivev
break;
case 4: // completed
if (xhr2.status == 200) {
copyToClipboard(xhr2.responseText);
} else if (xhr2.status != 200) {
alert('upload failure, status code ' + xhr2.status);
}
break;
default:
alert('bad readyState value');
}
};
xhr2.open('POST', 'http://myserver.com:5000/~nmz787/addpdf', true);
xhr2.setRequestHeader('crossDomain','true');
var formData = new FormData();
formData.append('file', xhr.response );
var filename = prompt('Please enter a different filename if you like',window.location.pathname.split('/')[window.location.pathname.split('/').length-1]);
if(filename==null) {
return;
}
if(filename.indexOf('.pdf')==-1) {
filename+='.pdf';
}
formData.append('filename', filename);
formData.append('url', window.location);
formData.append('contentType', xhr.getResponseHeader('content-type'));
xhr2.send(formData);
}
};
xhr.open('GET', window.location, true);
xhr.responseType = 'blob';
xhr.send(null);
Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
argparse==1.2.1
stevedore==0.7.2
virtualenv==1.8.4
virtualenv-clone==0.2.4
virtualenvwrapper==3.6
wsgiref==0.1.2
import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
f.required_methods = ['OPTIONS']
return update_wrapper(wrapped_function, f)
return decorator
UPLOAD_FOLDER = '/home/nmz787/public_html/pdf'
ALLOWED_EXTENSIONS = set(['pdf'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# useful debugging tricks
app.config["TRAP_BAD_REQUEST_KEY_ERRORS"] = True
app.config["TRAP_HTTP_EXCEPTIONS"] = True
app.config["DEBUG"] = True
@app.route('/~nmz787/addpdf', methods=['POST', 'PUT', 'OPTIONS'])
@crossdomain(origin='*', headers='crossDomain, Content-Type')
def upload_file():
if request.method == 'POST':
# original url of the document
#url = request.form["url"]
print 'request:\n'
print request
print '***********'
print request.files["file"]
# what sort of content type did the server claim for that file?
content_type = request.form["contentType"]
print 'content type is: ' + content_type
# actual file content
content = request.files["file"]
print 'content is:\n'
print content
filename = request.form["filename"]
print 'filename: ' + filename
file = content
print allowed_file(filename)
if file and allowed_file(filename):
filename = secure_filename(filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print 'saving to filepath: ' + filepath
file = open(filepath, "wb")
file.write(content.read())
file.close()
return 'http://myserver.com/~nmz787/pdf/'+filename
# TODO: save file
return "hello world, the file contents are: " + content
else:
return "dunno what to do with your request"
if __name__ == "__main__":
app.run(host='0.0.0.0')
javascript:(function(){function copyToClipboard(text){window.prompt("Copy to clipboard: Ctrl+C, Enter",text);}var xhr=new XMLHttpRequest();xhr.onload=function(e){if(this.status==200){var xhr2=new XMLHttpRequest();xhr2.onreadystatechange=function(){switch(xhr2.readyState){case 0:case 1:case 2:case 3:break;case 4:if(xhr2.status==200){copyToClipboard(xhr2.responseText);}else if(xhr2.status!=200){alert('upload failure, status code '+xhr2.status);}break;default:alert('bad readyState value');}};xhr2.open('POST','http://myserver.com:5000/~nmz787/addpdf',true);xhr2.setRequestHeader('crossDomain','true');var formData=new FormData();formData.append('file',xhr.response);var filename=prompt('Please enter a different filename if you like',window.location.pathname.split('/')[window.location.pathname.split('/').length-1]);if(filename==null){return;}if(filename.indexOf('.pdf')==-1){filename+='.pdf';}formData.append('filename',filename);formData.append('url',window.location);formData.append('contentType',xhr.getResponseHeader('content-type'));xhr2.send(formData);}};xhr.open('GET',window.location,true);xhr.responseType='blob';xhr.send(null);})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment