Working around same origin policy - javascript

I want to make an iframe in which i load a web page that i have on other server and i would like to fill some inputs via jquery, i know i cant do this since both apps are on different servers and have different ips, but i tried the next thing.
I made an URL rewrite on my IIS so my Host app matches 192.168.25.17 and made an url for the child app match the next ip 192.168.25.17/mysecondapp so i tried to run a jquery and it failed, so i checked the document.domain and both says its 192.168.25.17 what am doing wrong? or how can i check what is troubling me for working around the policy?
<button class="btn btn-success" id="botondeMagia">Set UserName</button>
<div id="magicContainer">
<iframe id="foo" src="http://192.168.25.17/apex/anapp.faces" height="500px">
</iframe>
</div>
<script>
$("#botondeMagia").on('click', function() {
$("#magicContainer input").val("TEST!!");
$("#magicContainer input").text("TEST!!");
});</script>
I don't know if the next info is useful but, the second app doesnt have jquery, just the hosting app.
The guest app is hosted in tomcat
Thank you very much

Actually Quentin is right, i had to use
$("#magicContainer").contents()
for viewing the contents on it, URL rewrite actually is great workaround for this tho
thank you so much guys

Related

Loading a specific div from another website into own website

Ive tried using the js load function but as the external site does not allow CORS requests, my original GET request gets blocked.
<div id="test"></div>
<script>
$(document).ready(function () {
$("#test").load("https://mywebsite.com");
});
</script>
So it seems that my only approach is to use iframes?! Is there a way to only crawl a specific div with iframes? I dont want to display the whole website.
EDIT: Since I am using Django I was able to crawl the website with python in a view and then push the crawled and cleaned up code snippet in the html template. Nevertheless to answer my question -> There is no correct way of doing it as long as the website you are trying to access is blocking the content.
Work with the owner of the site you want to take content from.
They can set you up with an API. That avoids having to use hackey methods or risking copyright-related legal trouble.

Can't embed a badge in HTML

I have a certification and a badge provided by Acclaim. I want to embed it in my personal website but it's not working. here's the code they provided:
<div data-iframe-width="150" data-iframe-height="270" data-share-badge-id="60615e70-6409-4752-9d77-3553a43d13d2" data-share-badge-host="https://www.youracclaim.com"></div>
<script type="text/javascript" async src="//cdn.youracclaim.com/assets/utilities/embed.js"></script>
but even when simply put onto an empty html:5 page, I get the error: Loading failed for the <script> with source “file:///assets/utilities/embed.js”.
What's the problem here? I'm not sure how Acclaim can provide a ready-to-paste script that's just simply not working, nothing shows up on the website. I'm guessing the problem is at the src... part, but don't know how to fix it.
If you're loading your page via file:, then protocol-relative URLs aren't going to work. The script tag has:
src="//cdn.youracclaim.com/assets/utilities/embed.js"
This should be changed to:
src="https://cdn.youracclaim.com/assets/utilities/embed.js"
You'll find though that when you're using an actual web server, this is a non-issue. The reason for the protocol-relative URLs is so that HTTP pages would use the HTTP version, and HTTPS would use the HTTPS version. This method is outdated anyway. HTTPS should be used everywhere, even if you're loading HTTPS JavaScript from an HTTP page.

403 error when accessing an external image URL on an internal website

I've been working on this internal web app that allows the users to click on a button to see an image. Pretty straight forward.
<img id="swatchimage" width="600" height="600" src="" />
The image needs to be linked from an external website. The piece of javascript that does that is:
$("#swatchimage").attr("src", fileURL);
One of the URLs look like this:
If you try to access the image above in a browser it works without a problem.
When accessing it from the internal app ( IP: 192.168.110.15 ) then the server returns 403 error.
The same files are used in the main website, so I don't think it's about the hot linking protection (which i tried to disable too, to no avail).
Is there some restriction that I'm not aware of when linking these kind of resources?
I think I found the problem:
The server (cdn.palmcentre.co.uk) is not sending an Access-Control-Allow-Origin header in the response, so you can't use JavaScript to fetch it.
However, this is not a 403. Here's a screenshot of the error I get:
Also, what you're trying to do is not to fetch the image, but modify the src attribute of an <image> element, which should be allowed.
This makes me think that the error you're getting is because jQuery is trying to fetch the image, and failing, but I'm not sure.
Try doing this instead:
document.getElementById("#swatchimage").src = fileURL;
It seems I have found the solution.
It was puzzling indeed, because i knew that CORS would not be at play here. It was just another image URL that was requested from an HTML tag. So it shouldn't have behaved in such way.
Notwithstanding the help received, for which I am grateful, it seems that the issue was in fact a hot linking protection that was in place.
But it was not from the server itself or from the script but rather from the CloudFlare cache itself.

IE9 <a> works in directly loaded page but not working when coming via linked page

This question does not need a cross-browser solution as the pages will be exclusively used by IE9.
With that said, I have a page ( myPage ) being developed that accesses a page from a different domain ( linkedPage). Within the linkedPage there is a PDF attachment ( pdfAttachment ).
myPage URL: http://myDomain/files/myPage.html
linkedPage URL: https://na13.salesforce.com/article/x.html
pdfAttachment URL: https://c.na13.content.force.com/servlet/x.pdf
When a user triggers the opening of the linkedPage via window.open:
window.open('https://na13.salesforce.com/article/x.html', '_blank', 'toolbar=no, scrollbars=yes, resizable=yes, top=300, left=500, width=1000, height=600');
and then clicks on the link for the .pdf attachment,
Load x.pdf
it returns the error
Internet Explorer cannot display the webpage.
However, if the user loads the linkedPage directly (versus from a link on myPage), the pdfAttachment loads without issue.
I do not have control over the linkedPage or pdfAttachment pages but it may be possible to have the group that does control those host myPage as well as a last resort.
Is this a cross-domain issue? I'm not trying to run scripts or manipulate the content so with my less than robust understanding of that, it doesn't seem to fit.
Are there settings for IE9 that could be causing the issue?
I feel like it might be related to the window.open but was unable to find anything here or through other sites. Does window.open put any limitations on the page it loads?
What other possible things could be causing this issue?
Thanks to all!
ADDITIONAL INFORMATION
When troubleshooting this, I discovered that I was able to access linkedPage from myPage and open pdfAttachment successfully on a system with full internet access while the majority of its users will have limited internet access to whitelisted items only (of which both the linkedPage and pdfAttachment are).
Also, when users access the linkedPage by itself (not from myPage) they login through Citrix and then login to Salesforce. When the linkedPage is accessed from myPage they are only prompted to login to Salesforce. I'm not sure if that makes a difference, since systems with full internet access are still successful without first going through Citrix.
Could there be any permission issues?!
#jdu - here is the markup
<body>
<div class="knowledgePrintableView" id="knowledgePrintableView">
<div id="articlePrintableView:j_id3:j_id4:j_id24">
<div class="knowledgePrintableViewContent " id="knowledgePrintableViewContent">
<div class="sectionContent">
<table>
<tr>
<td class="fieldValue">
<span id="articleX">
<div>
<img src="/sfc/images/docicons/doctype_pdf_16.png" alt="PDF" title="PDF" />
<!-- Only the text node exists within the <a> tag -->
PDF Attachment.pdf
</div>
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</body>
When troubleshooting this, I discovered that I was able to access
linkedPage from myPage and open pdfAttachment successfully on a system
with full internet access while the majority of its users will have
limited internet access to whitelisted items only (of which both the
linkedPage and pdfAttachment are).
Hmmm! Then I would focus your search there. You're using an HTTPS link in that anchor tag, and trying to route through some other systems. Those other systems may not allow HTTPS traffic.
Try a non-secure link.

Javascript redirection / domain specify

I found an nice script while searching and inspecting the elements of some websites.
This is what I have found:
<script type="text/javascript">
//redirect browser to fullscreen preview
if (/^http:\/\/codecanyon\.net/.test(document.referrer))
window.top.location.href = 'http://www.gravitysign.com/backslider/';
</script>
So if I understood from this script it tells jquery if the website is opened over codecanyon redirect them to specifed website for preview.
Now... I was wondering if there is possibility to make something like this.
If we specify an website for example http://google.com and we input that into javascript... And then if that website is uploaded to any other domain, other then google.com ... It will redirect to specified site (google) ?
So to clear things out a little bit let me make an example.
If I made a website for "an-website.com" and then someone take their website and upload it to "another-website.com", it will automatically redirect all visitors from another-website.com to an-website.com.
Hope I was clear enough and hope that this is possible. Cheers!
You can of course redirect any user accessing your site from a domain not matching yours but using javascript. This should work just fine:
if (window.location.hostname !== 'yourdomain.com'){
window.top.location.href = 'http://yourdomain.com';
}
You can also use match, if you host your site on a subdomain, etc.
Keep in mind that any person with write access to the file on the server will be able to remove this "copy protection". Copy protecting client side content is impossible, as you need to serve the content in a way a browser understands, effectively making the content available to anyone.
If you are looking for solution for single domain protection, here you can see my
Redirect Website if its not specified domain in script - Protection using Javascript
I am looking for solution for multiple domain.

Categories