URL replacement for Javascript code in ColdFusion - coldfusion

I have inherited a external page where I have no control:
I have javascript sorting on that page: like http://www.exampledomain.com/javascript:void(1);
Now it has many links like this, the 1 you see is dynamic, what I want to is: convert this code to ColdFusion URL like http://www.exampledomain.com/sor=1&sort=asc & desc. The 1 should work as it is, like it should keep its value as it is 1,2,3,4 etc. I tried to do this with jQuery.
How can I alter these links in ColdFusion?
I tried to come up with some of Javascript solution but it did not work
$('#container').find('a').attr('href', function(i, old) {
var col = decodeURIComponent(old).match(/javascript:\s*sort\((.*?)\)/)[1];
return hrefcall+data+'&sortBy='+col;
Thanks

Your question is unclear, the source of the data is unknown and there are a few typoes.
This JQuery (1.9.1)
var ihref = "";
var col = 'somedata';
$("a").click(function (i) {
ihref = $(this).attr('href');
if (ihref.match(/javascript:\s*sort\(\d+\).*/i)) {
ihref = ihref.replace(/javascript:\s*sort\((\d+)\).*/i,"http://www.w3schools.com/sor=$1&sortBy=" + col);
$(this).attr('href',ihref);
alert("As a demonstration, you\'ll see the link is rewritten when a javascript:sort url is clicked.");
}
});
Will convert all javascript:void links as you wanted. I did change the dummy url to w3schools.com because the good folks who own w3 permit their site to be loaded in Iframes which is necessary to easily demonstrate that this code works.
Of course, JQuery only works when JS is enabled. Still, since you started with JQuery, I thought I might show you a working demonstration.
(The links don't actually work, because w3schools doesn't have pages at those points, but you can see in the status bar, the links are rewritten).
If you're retrieving the page via cfhttp.filecontent, you can do something like this
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sort=asc","ALL")>
The ReReplaceNoCase() was tested against this sample code..
<cfset cfhttp = {} />
<cfset col = "somedata" />
<cfsavecontent variable="cfhttp.filecontent">
test - will not alter url<Br />
test - will alter url<Br />
test - will alter url<Br />
test - will not alter url<Br />
</cfsavecontent>
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sortBy=#col#","ALL")>
<cfdump var="#cfhttp#">

Related

too much pathinfo using buildURL

I try to get an impression how to build a REST-API using FW/1 version 4.
I set this in my Application.cfc:
<cfset variables.framework.routes = [
{ "$GET/persons/:id/$" = "/persons/show/id/:id" }
]>
My controller method
<cffunction name="show">
<cfargument name="rc" type="struct" required="true">
<cfset local.strURL = variables.fw.buildURL(
action = 'persons.show',
queryString = 'id=123'
)>
<cfset variables.fw.renderData().data( { strURL = local.strURL } ).type( "json" )>
</cffunction>
I call /persons/123 and get this output:
{"URL":"/persons/show/id/123"}
I don't like the /show/ pathinfo in this generated URL. It's not part of the URL I requested in the browser (or via AJAX etc.). Is there a way to get rid of this in the generated URL?
This is not a functionality in FW/1. This was considered in 2012, but was rejected.
See: https://github.com/framework-one/fw1/issues/145
The core issue was (and is) according to Sean Corfield:
Interesting idea but I think you'll find that, in general, routes =>
URLs is actually a many-to-one mapping so you can't reliably go
backwards from URLs (actions / params) to a unique route. I've talked
that over a few times with people and so far no one has managed to
come up with a mapping that works in all cases.

ColdFusion Dropbox - How to get the token from the response URI

I am trying to implement the OAuth with Dropbox from a ColdFusion application, and I managed how to call the Dropbox method to generate the access token, but... I don't know how to get the generated TOKEN from the response URI. I am getting something like this from Dropbox:
http://localhost/dropbox/generate_token.cfm#access_token=AAAAAAAAYVM_XdCYlbTz0gQOwQkWlg6TDXf84_5h4giikg6J-7Man&token_type=bearer&uid=267693&account_id=dbid%3AAABeDMm-BN0n1DofLZz9kPZAipnQ
How to I retrieve the URL variables in this case? I mean if I do a
<cfdump var="#URL#">
I am getting an empty struct. If I do a
<cfdump var="#CGI#">
I still don't see any of the URL retrieved parameters in the structure. How do I get the variables and their values from the Dropbox response?
UPDATED
At some point I thought I found a way to read the URL but now - for no reason - this doesn't work anymore! I didn't change anything but the solution below doesn't work anymore.
I can read the full URL with JavaScript using document.location but this means to do an extra submit to a ColdFusion page and I don't want to do this. I want to get the Dropbox token from the URL and save it to the database directly in this page...
Any new ideas please?
SOLUTION THAT SEEMED TO WORK AT SOME POINT ...
I found a way to get the URI string using this:
<cfset objRequest = GetPageContext().GetRequest().getParameterMap() />
<cfdump var="#objRequest#">
<cfoutput>
<cfloop collection="#objRequest#" item="i">
<p>
#i# - #objRequest[i][1]#
</p>
</cfloop>
</cfoutput>
From now on, I know how to get the values returned by Dropbox.
I found a way to get the returned parameters by reading the browser URL with JavaScript, so in two steps: first, parse and extract the full URL including the part after the # sign (I found this has a name and it is called the "URL fragment") and second, create a JavaScript form with parsed parameters and resubmitted to the server. Here is the code:
<cfparam name="FORM.action" default="">
<cfif FORM.action IS "save_token">
<cfdump var="#FORM#">
<cfelse>
<form name="main" id="main" method="post">
<input type="hidden" name="action" id="action" value="save_token">
</form>
<script type="text/javascript" language="javascript">
<!--
var parameters = window.location.hash.substr(1).split("&");
function addHidden(theForm, key, value) {
// Create a hidden input element, and append it to the form:
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = value;
theForm.appendChild(input);
}
// Form reference:
var theForm = document.forms["main"];
for (var i=0; i<parameters.length; i++) {
// Add data:
addHidden(theForm, parameters[i].split("=")[0], parameters[i].split("=")[1]);
}
theForm.submit();
//-->
</script>
</cfif>

remove some code from the url pagination

i have the following code, but i am very loose in the regular expression, i am using coldfusion
and i want to remove the code which is inbetween before every next page call
http://beta.mysite.com/?jobpage=2page=2#brands
what i am trying is if jobpage exists, it should remove the jobpage=2 from the URL, {2} is dynamic as it can be one or 2 or 3 and so on.
I tried with listfirst and listlast or gettoken but no help.
This should do it for you
<Cfset myurl = "http://beta.mysite.com/?jobpage=2page=2##brands" />
<cfoutput>#myurl#</cfoutput><br><Br>
<cfset myurl = ReReplaceNoCase(myurl,"(jobpage=[0-9]+[\&]?)","","ALL") />
<cfoutput>#myurl#</cfoutput>

Replace url in all hrefs inside body tags

I am using cfhttp to get a website . I want to replace all the links inside the body tags. Importantly I don't want to mess up the stylesheets etc in the head.
I want to do the following:
In the external web page body we may find a link:
External Link
I want to replace it with the following:
External Link
Its easy enough using Replace() but then I also replace all the linked stylesheets etc. I just want to edit the href's of clickable links.
I've modified an HTML document's DOM to add tracking parameters to links in outbound email messages using the jsoup library. (jsoup is an open source Java HTML Parser and can be download at http://jsoup.org/.) You'll note that it uses jQuery-like select methods, but all manipulations are performed on the server-side (I've also used it for removing ads from CFHTTTP-fetched HTML.)
Here's a quick sample of working ColdFusion code that will do exactly what you want on the server-side:
<CFSET TheHTML = CFHTTP.FileContent>
<CFSET jsoup = CreateObject("java", "org.jsoup.Jsoup")>
<CFSET TempHTML = jsoup.parse(TheHTML)>
<CFLOOP ARRAY="#TempHTML.select('a')#" INDEX="ThisLink">
<CFSET TheLink = thisLink.attr("href").toString()>
<CFSET TheHTML = replace(TheHTML, TheLink, "http://mywebsite.com/?u=" & URLEncodedFormat(TheLink))>
</CFLOOP>

Using Jsoup (the Java html parser) with Handlebarsjs

So, I'm using jsoup and when I display the content returned I Get:
{{#ifcond="" pagetitle="" this.name}}
I'm doing this like local.htmlObj.Body().Html()
When I need it to be return like:
{{#ifCond PAGETITLE this.NAME}}
Here what I'm doing
<cfset paths = [] />
<cfset paths[1] = expandPath("/javaloader/lib/jsoup-1.7.1.jar") />
<cfset loader = createObject("component", "javaloader.JavaLoader").init( paths ) />
<cfset obj = loader.create( "org.jsoup.Jsoup" ) />
<cfset local.htmlObj = local.jsoupObj.parse( local.template ) />
<cfloop array="#local.htmlObj.select('.sidebar_left')#" index="element">
<cfif element.attr('section') EQ "test">
<cfset element.append('HTML HERE') />
</cfif>
</cfloop>
local.template is my template that is made up of a ton of different handlebar files That im pulling for different places. I'm constructing one handlebar file that gets returned.
The problem is that JSoup is trying to parse invalid HTML before it lets you access it. A slight easier to understand example of this behavior can be seen if you fetch the following HTML (seen in this question):
<p>
<table>[...]</table>
</p>
It will return:
<p></p>
<table>[...]</table>
In your case the Handelbars code is seen as attributes which in valid html always have a value (think checked="checked"). As far as I can tell there is no way to disable this behavior. It's really the wrong tool for the job you are trying to do. A cleaner approach would be to just fetch the document as a stream and save it to a string.