From 40f05489191d08577587ceb6d5636f99f5df7fc7 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sat, 15 Oct 2016 12:13:09 +0100 Subject: [PATCH] Use apis where it is possible for the online diff --- docs/url.js | 87 ++++++++++++++++-------------- docs/url.min.js | 2 +- website/templates/pages/url/url.js | 87 ++++++++++++++++-------------- 3 files changed, 97 insertions(+), 79 deletions(-) diff --git a/docs/url.js b/docs/url.js index 679eb15..a417d87 100644 --- a/docs/url.js +++ b/docs/url.js @@ -399,15 +399,12 @@ /* * Example URLs: * - * github/rtfpessoa/diff2html/7d02e67f3b3386ac5d804f974d025cd7a1165839 * https://github.com/rtfpessoa/diff2html/commit/7d02e67f3b3386ac5d804f974d025cd7a1165839 * https://github.com/rtfpessoa/diff2html/pull/106 * - * gitlab/gitlab-org/gitlab-ce/4e963fed42ad518caa7353d361a38a1250c99c41 * https://gitlab.com/gitlab-org/gitlab-ce/commit/4e963fed42ad518caa7353d361a38a1250c99c41 * https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6763 * - * bitbucket/atlassian/amps/52c38116f12475f75af4a147b7a7685478b83eca * https://bitbucket.org/atlassian/amps/commits/52c38116f12475f75af4a147b7a7685478b83eca * https://bitbucket.org/atlassian/amps/pull-requests/236 */ @@ -431,7 +428,7 @@ $(document).ready(function() { if (hash) { $url.val(hash); - draw(prepareUrl(hash)); + smartDraw(hash); } else if (search) { try { var url = search @@ -439,12 +436,12 @@ $(document).ready(function() { .split('diff=')[1] .split('&')[0]; $url.val(url); - draw(prepareUrl(url)); + smartDraw(url); } catch (_ignore) { } } - bind(prepareUrl); + bind(); $outputFormat .add($showFiles) @@ -452,90 +449,102 @@ $(document).ready(function() { .add($wordThreshold) .add($matchingMaxComparisons) .change(function() { - fastDraw(); + smartDraw(); }); - function fastDraw() { - var url = $url.val(); - var preparedUrl = prepareUrl(url); - draw(preparedUrl); + function smartDraw(urlOpt) { + var url = urlOpt | $url.val(); + var req = prepareUrl(url); + draw(req); + } + + function updateHash(url) { + window.location.hash = '#!' + url; } function bind() { $('#url-btn').click(function(e) { e.preventDefault(); - fastDraw(); + var url = $url.val(); + smartDraw(url); + updateHash(url); }); $url.on('paste', function(e) { - fastDraw(); + var url = e.originalEvent.clipboardData.getData('Text'); + smartDraw(url); + updateHash(url); }); } function prepareUrl(url) { - var githubPath = /^github\/(.*?)\/(.*?)\/(.*?)$/; + var fetchUrl; + var headers = new Headers(); + var githubCommitUrl = /^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; var githubPrUrl = /^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/pull\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; - var gitlabPath = /^gitlab\/(.*?)\/(.*?)\/(.*?)$/; var gitlabCommitUrl = /^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; var gitlabPrUrl = /^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/merge_requests\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; - var bitbucketPath = /^bitbucket\/(.*?)\/(.*?)\/(.*?)$/; var bitbucketCommitUrl = /^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/commits\/(.*?)(?:\/raw)?(?:\/.*)?$/; var bitbucketPrUrl = /^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/pull-requests\/(.*?)(?:\/.*)?$/; - function genericUrlGen(provider, userName, projectName, type, value) { - return 'https://' + provider + '.com/' + userName + '/' + projectName + '/' + type + '/' + value + '.diff'; + + function gitLabUrlGen(userName, projectName, type, value) { + return 'https://crossorigin.me/https://gitlab.com/' + userName + '/' + projectName + '/' + type + '/' + value + '.diff'; + } + + function gitHubUrlGen(userName, projectName, type, value) { + headers.append('Accept', 'application/vnd.github.v3.diff'); + return 'https://api.github.com/repos/' + userName + '/' + projectName + '/' + type + '/' + value; } function bitbucketUrlGen(userName, projectName, type, value) { var baseUrl = 'https://bitbucket.org/api/2.0/repositories/'; - if (type === 'pullrequests') { return baseUrl + userName + '/' + projectName + '/pullrequests/' + value + '/diff'; } - return baseUrl + userName + '/' + projectName + '/diff/' + value; } var values; - var finalUrl; - if ((values = githubPath.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'commit', values[3]); - } else if ((values = githubCommitUrl.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'commit', values[3]); + if ((values = githubCommitUrl.exec(url))) { + fetchUrl = gitHubUrlGen(values[1], values[2], 'commits', values[3]); } else if ((values = githubPrUrl.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'pull', values[3]); - } else if ((values = gitlabPath.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'commit', values[3]); + fetchUrl = gitHubUrlGen(values[1], values[2], 'pulls', values[3]); } else if ((values = gitlabCommitUrl.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'commit', values[3]); + fetchUrl = gitLabUrlGen(values[1], values[2], 'commit', values[3]); } else if ((values = gitlabPrUrl.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'merge_requests', values[3]); - } else if ((values = bitbucketPath.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); + fetchUrl = gitLabUrlGen(values[1], values[2], 'merge_requests', values[3]); } else if ((values = bitbucketCommitUrl.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); + fetchUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); } else if ((values = bitbucketPrUrl.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'pullrequests', values[3]); + fetchUrl = bitbucketUrlGen(values[1], values[2], 'pullrequests', values[3]); } else { console.info('Could not parse url, using the provided url.'); - finalUrl = url; + fetchUrl = url; } - return finalUrl; + return { + url: fetchUrl, + headers: headers + }; } - function draw(url) { + function draw(req) { var outputFormat = $outputFormat.val(); var showFiles = $showFiles.is(':checked'); var matching = $matching.val(); var wordThreshold = $wordThreshold.val(); var matchingMaxComparisons = $matchingMaxComparisons.val(); - var fullUrl = 'https://crossorigin.me/' + url; - fetch(fullUrl) + fetch(req.url, { + method: 'GET', + headers: req.headers, + mode: 'cors', + cache: 'default' + }) .then(function(res) { return res.text(); }) diff --git a/docs/url.min.js b/docs/url.min.js index 50294c4..d497ffe 100644 --- a/docs/url.min.js +++ b/docs/url.min.js @@ -1 +1 @@ -!function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o-1?upcased:method}function Request(input,options){options=options||{};var body=options.body;if(Request.prototype.isPrototypeOf(input)){if(input.bodyUsed)throw new TypeError("Already read");this.url=input.url,this.credentials=input.credentials,options.headers||(this.headers=new Headers(input.headers)),this.method=input.method,this.mode=input.mode,body||(body=input._bodyInit,input.bodyUsed=!0)}else this.url=input;if(this.credentials=options.credentials||this.credentials||"omit",(options.headers||!this.headers)&&(this.headers=new Headers(options.headers)),this.method=normalizeMethod(options.method||this.method||"GET"),this.mode=options.mode||this.mode||null,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&body)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(body)}function decode(body){var form=new FormData;return body.trim().split("&").forEach(function(bytes){if(bytes){var split=bytes.split("="),name=split.shift().replace(/\+/g," "),value=split.join("=").replace(/\+/g," ");form.append(decodeURIComponent(name),decodeURIComponent(value))}}),form}function headers(xhr){var head=new Headers,pairs=(xhr.getAllResponseHeaders()||"").trim().split("\n");return pairs.forEach(function(header){var split=header.trim().split(":"),key=split.shift().trim(),value=split.join(":").trim();head.append(key,value)}),head}function Response(bodyInit,options){options||(options={}),this.type="default",this.status=options.status,this.ok=this.status>=200&&this.status<300,this.statusText=options.statusText,this.headers=options.headers instanceof Headers?options.headers:new Headers(options.headers),this.url=options.url||"",this._initBody(bodyInit)}if(!self.fetch){Headers.prototype.append=function(name,value){name=normalizeName(name),value=normalizeValue(value);var list=this.map[name];list||(list=[],this.map[name]=list),list.push(value)},Headers.prototype["delete"]=function(name){delete this.map[normalizeName(name)]},Headers.prototype.get=function(name){var values=this.map[normalizeName(name)];return values?values[0]:null},Headers.prototype.getAll=function(name){return this.map[normalizeName(name)]||[]},Headers.prototype.has=function(name){return this.map.hasOwnProperty(normalizeName(name))},Headers.prototype.set=function(name,value){this.map[normalizeName(name)]=[normalizeValue(value)]},Headers.prototype.forEach=function(callback,thisArg){Object.getOwnPropertyNames(this.map).forEach(function(name){this.map[name].forEach(function(value){callback.call(thisArg,value,name,this)},this)},this)};var support={blob:"FileReader"in self&&"Blob"in self&&function(){try{return new Blob,!0}catch(e){return!1}}(),formData:"FormData"in self,arrayBuffer:"ArrayBuffer"in self},methods=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];Request.prototype.clone=function(){return new Request(this)},Body.call(Request.prototype),Body.call(Response.prototype),Response.prototype.clone=function(){return new Response(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new Headers(this.headers),url:this.url})},Response.error=function(){var response=new Response(null,{status:0,statusText:""});return response.type="error",response};var redirectStatuses=[301,302,303,307,308];Response.redirect=function(url,status){if(-1===redirectStatuses.indexOf(status))throw new RangeError("Invalid status code");return new Response(null,{status:status,headers:{location:url}})},self.Headers=Headers,self.Request=Request,self.Response=Response,self.fetch=function(input,init){return new Promise(function(resolve,reject){function responseURL(){return"responseURL"in xhr?xhr.responseURL:/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())?xhr.getResponseHeader("X-Request-URL"):void 0}var request;request=Request.prototype.isPrototypeOf(input)&&!init?input:new Request(input,init);var xhr=new XMLHttpRequest;xhr.onload=function(){var status=1223===xhr.status?204:xhr.status;if(100>status||status>599)return void reject(new TypeError("Network request failed"));var options={status:status,statusText:xhr.statusText,headers:headers(xhr),url:responseURL()},body="response"in xhr?xhr.response:xhr.responseText;resolve(new Response(body,options))},xhr.onerror=function(){reject(new TypeError("Network request failed"))},xhr.ontimeout=function(){reject(new TypeError("Network request failed"))},xhr.open(request.method,request.url,!0),"include"===request.credentials&&(xhr.withCredentials=!0),"responseType"in xhr&&support.blob&&(xhr.responseType="blob"),request.headers.forEach(function(value,name){xhr.setRequestHeader(name,value)}),xhr.send("undefined"==typeof request._bodyInit?null:request._bodyInit)})},self.fetch.polyfill=!0}}("undefined"!=typeof self?self:this)},{}],2:[function(require){$(document).ready(function(){function fastDraw(){var url=$url.val(),preparedUrl=prepareUrl(url);draw(preparedUrl)}function bind(){$("#url-btn").click(function(e){e.preventDefault(),fastDraw()}),$url.on("paste",function(){fastDraw()})}function prepareUrl(url){function genericUrlGen(provider,userName,projectName,type,value){return"https://"+provider+".com/"+userName+"/"+projectName+"/"+type+"/"+value+".diff"}function bitbucketUrlGen(userName,projectName,type,value){var baseUrl="https://bitbucket.org/api/2.0/repositories/";return"pullrequests"===type?baseUrl+userName+"/"+projectName+"/pullrequests/"+value+"/diff":baseUrl+userName+"/"+projectName+"/diff/"+value}var values,finalUrl,githubPath=/^github\/(.*?)\/(.*?)\/(.*?)$/,githubCommitUrl=/^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,githubPrUrl=/^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/pull\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,gitlabPath=/^gitlab\/(.*?)\/(.*?)\/(.*?)$/,gitlabCommitUrl=/^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,gitlabPrUrl=/^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/merge_requests\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,bitbucketPath=/^bitbucket\/(.*?)\/(.*?)\/(.*?)$/,bitbucketCommitUrl=/^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/commits\/(.*?)(?:\/raw)?(?:\/.*)?$/,bitbucketPrUrl=/^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/pull-requests\/(.*?)(?:\/.*)?$/;return(values=githubPath.exec(url))?finalUrl=genericUrlGen("github",values[1],values[2],"commit",values[3]):(values=githubCommitUrl.exec(url))?finalUrl=genericUrlGen("github",values[1],values[2],"commit",values[3]):(values=githubPrUrl.exec(url))?finalUrl=genericUrlGen("github",values[1],values[2],"pull",values[3]):(values=gitlabPath.exec(url))?finalUrl=genericUrlGen("gitlab",values[1],values[2],"commit",values[3]):(values=gitlabCommitUrl.exec(url))?finalUrl=genericUrlGen("gitlab",values[1],values[2],"commit",values[3]):(values=gitlabPrUrl.exec(url))?finalUrl=genericUrlGen("gitlab",values[1],values[2],"merge_requests",values[3]):(values=bitbucketPath.exec(url))?finalUrl=bitbucketUrlGen(values[1],values[2],"commit",values[3]):(values=bitbucketCommitUrl.exec(url))?finalUrl=bitbucketUrlGen(values[1],values[2],"commit",values[3]):(values=bitbucketPrUrl.exec(url))?finalUrl=bitbucketUrlGen(values[1],values[2],"pullrequests",values[3]):(console.info("Could not parse url, using the provided url."),finalUrl=url),finalUrl}function draw(url){var outputFormat=$outputFormat.val(),showFiles=$showFiles.is(":checked"),matching=$matching.val(),wordThreshold=$wordThreshold.val(),matchingMaxComparisons=$matchingMaxComparisons.val(),fullUrl="https://crossorigin.me/"+url;fetch(fullUrl).then(function(res){return res.text()}).then(function(data){var container="#url-diff-container",diff2htmlUi=new Diff2HtmlUI({diff:data});$container.css("side-by-side"===outputFormat?{width:"1400px"}:{width:""}),diff2htmlUi.draw(container,{outputFormat:outputFormat,showFiles:showFiles,matching:matching,matchWordsThreshold:wordThreshold,matchingMaxComparisons:matchingMaxComparisons,synchronisedScroll:!0}),diff2htmlUi.fileListCloseable(container,!1),diff2htmlUi.highlightCode(container)})}require("whatwg-fetch");var $container=$(".container"),$url=$("#url"),$outputFormat=$("#diff-url-options-output-format"),$showFiles=$("#diff-url-options-show-files"),$matching=$("#diff-url-options-matching"),$wordThreshold=$("#diff-url-options-match-words-threshold"),$matchingMaxComparisons=$("#diff-url-options-matching-max-comparisons"),hash=window.location.hash.replace(/^(#!?\/?)/,""),search=window.location.search;if(hash)$url.val(hash),draw(prepareUrl(hash));else if(search)try{var url=search.split("?")[1].split("diff=")[1].split("&")[0];$url.val(url),draw(prepareUrl(url))}catch(_ignore){}bind(prepareUrl),$outputFormat.add($showFiles).add($matching).add($wordThreshold).add($matchingMaxComparisons).change(function(){fastDraw()})})},{"whatwg-fetch":1}]},{},[2]); \ No newline at end of file +!function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o-1?upcased:method}function Request(input,options){options=options||{};var body=options.body;if(Request.prototype.isPrototypeOf(input)){if(input.bodyUsed)throw new TypeError("Already read");this.url=input.url,this.credentials=input.credentials,options.headers||(this.headers=new Headers(input.headers)),this.method=input.method,this.mode=input.mode,body||(body=input._bodyInit,input.bodyUsed=!0)}else this.url=input;if(this.credentials=options.credentials||this.credentials||"omit",(options.headers||!this.headers)&&(this.headers=new Headers(options.headers)),this.method=normalizeMethod(options.method||this.method||"GET"),this.mode=options.mode||this.mode||null,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&body)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(body)}function decode(body){var form=new FormData;return body.trim().split("&").forEach(function(bytes){if(bytes){var split=bytes.split("="),name=split.shift().replace(/\+/g," "),value=split.join("=").replace(/\+/g," ");form.append(decodeURIComponent(name),decodeURIComponent(value))}}),form}function headers(xhr){var head=new Headers,pairs=(xhr.getAllResponseHeaders()||"").trim().split("\n");return pairs.forEach(function(header){var split=header.trim().split(":"),key=split.shift().trim(),value=split.join(":").trim();head.append(key,value)}),head}function Response(bodyInit,options){options||(options={}),this.type="default",this.status=options.status,this.ok=this.status>=200&&this.status<300,this.statusText=options.statusText,this.headers=options.headers instanceof Headers?options.headers:new Headers(options.headers),this.url=options.url||"",this._initBody(bodyInit)}if(!self.fetch){Headers.prototype.append=function(name,value){name=normalizeName(name),value=normalizeValue(value);var list=this.map[name];list||(list=[],this.map[name]=list),list.push(value)},Headers.prototype["delete"]=function(name){delete this.map[normalizeName(name)]},Headers.prototype.get=function(name){var values=this.map[normalizeName(name)];return values?values[0]:null},Headers.prototype.getAll=function(name){return this.map[normalizeName(name)]||[]},Headers.prototype.has=function(name){return this.map.hasOwnProperty(normalizeName(name))},Headers.prototype.set=function(name,value){this.map[normalizeName(name)]=[normalizeValue(value)]},Headers.prototype.forEach=function(callback,thisArg){Object.getOwnPropertyNames(this.map).forEach(function(name){this.map[name].forEach(function(value){callback.call(thisArg,value,name,this)},this)},this)};var support={blob:"FileReader"in self&&"Blob"in self&&function(){try{return new Blob,!0}catch(e){return!1}}(),formData:"FormData"in self,arrayBuffer:"ArrayBuffer"in self},methods=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];Request.prototype.clone=function(){return new Request(this)},Body.call(Request.prototype),Body.call(Response.prototype),Response.prototype.clone=function(){return new Response(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new Headers(this.headers),url:this.url})},Response.error=function(){var response=new Response(null,{status:0,statusText:""});return response.type="error",response};var redirectStatuses=[301,302,303,307,308];Response.redirect=function(url,status){if(-1===redirectStatuses.indexOf(status))throw new RangeError("Invalid status code");return new Response(null,{status:status,headers:{location:url}})},self.Headers=Headers,self.Request=Request,self.Response=Response,self.fetch=function(input,init){return new Promise(function(resolve,reject){function responseURL(){return"responseURL"in xhr?xhr.responseURL:/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())?xhr.getResponseHeader("X-Request-URL"):void 0}var request;request=Request.prototype.isPrototypeOf(input)&&!init?input:new Request(input,init);var xhr=new XMLHttpRequest;xhr.onload=function(){var status=1223===xhr.status?204:xhr.status;if(100>status||status>599)return void reject(new TypeError("Network request failed"));var options={status:status,statusText:xhr.statusText,headers:headers(xhr),url:responseURL()},body="response"in xhr?xhr.response:xhr.responseText;resolve(new Response(body,options))},xhr.onerror=function(){reject(new TypeError("Network request failed"))},xhr.ontimeout=function(){reject(new TypeError("Network request failed"))},xhr.open(request.method,request.url,!0),"include"===request.credentials&&(xhr.withCredentials=!0),"responseType"in xhr&&support.blob&&(xhr.responseType="blob"),request.headers.forEach(function(value,name){xhr.setRequestHeader(name,value)}),xhr.send("undefined"==typeof request._bodyInit?null:request._bodyInit)})},self.fetch.polyfill=!0}}("undefined"!=typeof self?self:this)},{}],2:[function(require){$(document).ready(function(){function smartDraw(urlOpt){var url=urlOpt|$url.val(),req=prepareUrl(url);draw(req)}function updateHash(url){window.location.hash="#!"+url}function bind(){$("#url-btn").click(function(e){e.preventDefault();var url=$url.val();smartDraw(url),updateHash(url)}),$url.on("paste",function(e){var url=e.originalEvent.clipboardData.getData("Text");smartDraw(url),updateHash(url)})}function prepareUrl(url){function gitLabUrlGen(userName,projectName,type,value){return"https://crossorigin.me/https://gitlab.com/"+userName+"/"+projectName+"/"+type+"/"+value+".diff"}function gitHubUrlGen(userName,projectName,type,value){return headers.append("Accept","application/vnd.github.v3.diff"),"https://api.github.com/repos/"+userName+"/"+projectName+"/"+type+"/"+value}function bitbucketUrlGen(userName,projectName,type,value){var baseUrl="https://bitbucket.org/api/2.0/repositories/";return"pullrequests"===type?baseUrl+userName+"/"+projectName+"/pullrequests/"+value+"/diff":baseUrl+userName+"/"+projectName+"/diff/"+value}var fetchUrl,values,headers=new Headers,githubCommitUrl=/^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,githubPrUrl=/^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/pull\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,gitlabCommitUrl=/^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,gitlabPrUrl=/^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/merge_requests\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/,bitbucketCommitUrl=/^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/commits\/(.*?)(?:\/raw)?(?:\/.*)?$/,bitbucketPrUrl=/^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/pull-requests\/(.*?)(?:\/.*)?$/;return(values=githubCommitUrl.exec(url))?fetchUrl=gitHubUrlGen(values[1],values[2],"commits",values[3]):(values=githubPrUrl.exec(url))?fetchUrl=gitHubUrlGen(values[1],values[2],"pulls",values[3]):(values=gitlabCommitUrl.exec(url))?fetchUrl=gitLabUrlGen(values[1],values[2],"commit",values[3]):(values=gitlabPrUrl.exec(url))?fetchUrl=gitLabUrlGen(values[1],values[2],"merge_requests",values[3]):(values=bitbucketCommitUrl.exec(url))?fetchUrl=bitbucketUrlGen(values[1],values[2],"commit",values[3]):(values=bitbucketPrUrl.exec(url))?fetchUrl=bitbucketUrlGen(values[1],values[2],"pullrequests",values[3]):(console.info("Could not parse url, using the provided url."),fetchUrl=url),{url:fetchUrl,headers:headers}}function draw(req){var outputFormat=$outputFormat.val(),showFiles=$showFiles.is(":checked"),matching=$matching.val(),wordThreshold=$wordThreshold.val(),matchingMaxComparisons=$matchingMaxComparisons.val();fetch(req.url,{method:"GET",headers:req.headers,mode:"cors",cache:"default"}).then(function(res){return res.text()}).then(function(data){var container="#url-diff-container",diff2htmlUi=new Diff2HtmlUI({diff:data});$container.css("side-by-side"===outputFormat?{width:"1400px"}:{width:""}),diff2htmlUi.draw(container,{outputFormat:outputFormat,showFiles:showFiles,matching:matching,matchWordsThreshold:wordThreshold,matchingMaxComparisons:matchingMaxComparisons,synchronisedScroll:!0}),diff2htmlUi.fileListCloseable(container,!1),diff2htmlUi.highlightCode(container)})}require("whatwg-fetch");var $container=$(".container"),$url=$("#url"),$outputFormat=$("#diff-url-options-output-format"),$showFiles=$("#diff-url-options-show-files"),$matching=$("#diff-url-options-matching"),$wordThreshold=$("#diff-url-options-match-words-threshold"),$matchingMaxComparisons=$("#diff-url-options-matching-max-comparisons"),hash=window.location.hash.replace(/^(#!?\/?)/,""),search=window.location.search;if(hash)$url.val(hash),smartDraw(hash);else if(search)try{var url=search.split("?")[1].split("diff=")[1].split("&")[0];$url.val(url),smartDraw(url)}catch(_ignore){}bind(),$outputFormat.add($showFiles).add($matching).add($wordThreshold).add($matchingMaxComparisons).change(function(){smartDraw()})})},{"whatwg-fetch":1}]},{},[2]); \ No newline at end of file diff --git a/website/templates/pages/url/url.js b/website/templates/pages/url/url.js index b5763ed..c314bd3 100644 --- a/website/templates/pages/url/url.js +++ b/website/templates/pages/url/url.js @@ -3,15 +3,12 @@ /* * Example URLs: * - * github/rtfpessoa/diff2html/7d02e67f3b3386ac5d804f974d025cd7a1165839 * https://github.com/rtfpessoa/diff2html/commit/7d02e67f3b3386ac5d804f974d025cd7a1165839 * https://github.com/rtfpessoa/diff2html/pull/106 * - * gitlab/gitlab-org/gitlab-ce/4e963fed42ad518caa7353d361a38a1250c99c41 * https://gitlab.com/gitlab-org/gitlab-ce/commit/4e963fed42ad518caa7353d361a38a1250c99c41 * https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6763 * - * bitbucket/atlassian/amps/52c38116f12475f75af4a147b7a7685478b83eca * https://bitbucket.org/atlassian/amps/commits/52c38116f12475f75af4a147b7a7685478b83eca * https://bitbucket.org/atlassian/amps/pull-requests/236 */ @@ -35,7 +32,7 @@ $(document).ready(function() { if (hash) { $url.val(hash); - draw(prepareUrl(hash)); + smartDraw(hash); } else if (search) { try { var url = search @@ -43,12 +40,12 @@ $(document).ready(function() { .split('diff=')[1] .split('&')[0]; $url.val(url); - draw(prepareUrl(url)); + smartDraw(url); } catch (_ignore) { } } - bind(prepareUrl); + bind(); $outputFormat .add($showFiles) @@ -56,90 +53,102 @@ $(document).ready(function() { .add($wordThreshold) .add($matchingMaxComparisons) .change(function() { - fastDraw(); + smartDraw(); }); - function fastDraw() { - var url = $url.val(); - var preparedUrl = prepareUrl(url); - draw(preparedUrl); + function smartDraw(urlOpt) { + var url = urlOpt | $url.val(); + var req = prepareUrl(url); + draw(req); + } + + function updateHash(url) { + window.location.hash = '#!' + url; } function bind() { $('#url-btn').click(function(e) { e.preventDefault(); - fastDraw(); + var url = $url.val(); + smartDraw(url); + updateHash(url); }); $url.on('paste', function(e) { - fastDraw(); + var url = e.originalEvent.clipboardData.getData('Text'); + smartDraw(url); + updateHash(url); }); } function prepareUrl(url) { - var githubPath = /^github\/(.*?)\/(.*?)\/(.*?)$/; + var fetchUrl; + var headers = new Headers(); + var githubCommitUrl = /^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; var githubPrUrl = /^https?:\/\/(?:www\.)?github\.com\/(.*?)\/(.*?)\/pull\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; - var gitlabPath = /^gitlab\/(.*?)\/(.*?)\/(.*?)$/; var gitlabCommitUrl = /^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/commit\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; var gitlabPrUrl = /^https?:\/\/(?:www\.)?gitlab\.com\/(.*?)\/(.*?)\/merge_requests\/(.*?)(?:\.diff)?(?:\.patch)?(?:\/.*)?$/; - var bitbucketPath = /^bitbucket\/(.*?)\/(.*?)\/(.*?)$/; var bitbucketCommitUrl = /^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/commits\/(.*?)(?:\/raw)?(?:\/.*)?$/; var bitbucketPrUrl = /^https?:\/\/(?:www\.)?bitbucket\.org\/(.*?)\/(.*?)\/pull-requests\/(.*?)(?:\/.*)?$/; - function genericUrlGen(provider, userName, projectName, type, value) { - return 'https://' + provider + '.com/' + userName + '/' + projectName + '/' + type + '/' + value + '.diff'; + + function gitLabUrlGen(userName, projectName, type, value) { + return 'https://crossorigin.me/https://gitlab.com/' + userName + '/' + projectName + '/' + type + '/' + value + '.diff'; + } + + function gitHubUrlGen(userName, projectName, type, value) { + headers.append('Accept', 'application/vnd.github.v3.diff'); + return 'https://api.github.com/repos/' + userName + '/' + projectName + '/' + type + '/' + value; } function bitbucketUrlGen(userName, projectName, type, value) { var baseUrl = 'https://bitbucket.org/api/2.0/repositories/'; - if (type === 'pullrequests') { return baseUrl + userName + '/' + projectName + '/pullrequests/' + value + '/diff'; } - return baseUrl + userName + '/' + projectName + '/diff/' + value; } var values; - var finalUrl; - if ((values = githubPath.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'commit', values[3]); - } else if ((values = githubCommitUrl.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'commit', values[3]); + if ((values = githubCommitUrl.exec(url))) { + fetchUrl = gitHubUrlGen(values[1], values[2], 'commits', values[3]); } else if ((values = githubPrUrl.exec(url))) { - finalUrl = genericUrlGen('github', values[1], values[2], 'pull', values[3]); - } else if ((values = gitlabPath.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'commit', values[3]); + fetchUrl = gitHubUrlGen(values[1], values[2], 'pulls', values[3]); } else if ((values = gitlabCommitUrl.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'commit', values[3]); + fetchUrl = gitLabUrlGen(values[1], values[2], 'commit', values[3]); } else if ((values = gitlabPrUrl.exec(url))) { - finalUrl = genericUrlGen('gitlab', values[1], values[2], 'merge_requests', values[3]); - } else if ((values = bitbucketPath.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); + fetchUrl = gitLabUrlGen(values[1], values[2], 'merge_requests', values[3]); } else if ((values = bitbucketCommitUrl.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); + fetchUrl = bitbucketUrlGen(values[1], values[2], 'commit', values[3]); } else if ((values = bitbucketPrUrl.exec(url))) { - finalUrl = bitbucketUrlGen(values[1], values[2], 'pullrequests', values[3]); + fetchUrl = bitbucketUrlGen(values[1], values[2], 'pullrequests', values[3]); } else { console.info('Could not parse url, using the provided url.'); - finalUrl = url; + fetchUrl = url; } - return finalUrl; + return { + url: fetchUrl, + headers: headers + }; } - function draw(url) { + function draw(req) { var outputFormat = $outputFormat.val(); var showFiles = $showFiles.is(':checked'); var matching = $matching.val(); var wordThreshold = $wordThreshold.val(); var matchingMaxComparisons = $matchingMaxComparisons.val(); - var fullUrl = 'https://crossorigin.me/' + url; - fetch(fullUrl) + fetch(req.url, { + method: 'GET', + headers: req.headers, + mode: 'cors', + cache: 'default' + }) .then(function(res) { return res.text(); })