Embed a webpage and run a javascript file on it - javascript

I am basically trying to show end users a demonstration of how their webpage will look after they use my service[This would be a javascript file to run on their webpage.]
I have tried the following:
Used iframe to embed the webpage - The problem here is that i cant access the iframe content and run my js functions on them.
used jquery load(), html embed, and object but the same problem persists. i am unable to run my javascript on the embedded webpage.
Basically what i want to do can be seen here http://www.luminate.com/publisher/ . Just type in a website URL in the Preview Luminate section and see what happens. The page loads in a new tab and they have loaded a javascript on pageload.
Can someone suggest any way to do this or what these guys have done[ http://www.luminate.com/publisher/ ]?

here is the script which they used to call the page
<script type="text/javascript">
function pop_demo(event) {
event.preventDefault();
var url = $("#preview-site-url").val();
if (!url) {
alert("Please enter a URL to preview.");
return false;
}
if (!/^(http|https):\/\//.test(url)) {
url = "http://" + url;
}
url = "/publisher/demo/?url=" + encodeURIComponent(url);
window.open(url, "");
}
Here your calling url : "/publisher/demo/?url
and internally it get load frame and pass the result to next page.

Related

why does index.html keep opening as https://www.google.com/index.html?

Im trying to check for a keyword in the url and launch an html file from javascript
here is my code:
window.setInterval(function doTheTing() {
if (window.location.href.indexOf("keyword") > -1) {
window.location.replace("index.html")
}
}, 1000)
what im trying to do is open the file when the keyword is in url, and it does work, but for some reason when i try to open index.html it opens https://www.google.com/index.html instead
*if the keyword is in the url of a youtube video than it opens as https://www.youtube.com/index.html (which isnt a real domain so it just shows an error page)
*if its from a tiktok than https://www.tiktok.com/index.html (which also returns an error page)
and so on...
how can i just open index.html?
thanks for the help

java - edit url parameter of a page and show editing as a hyperlink in same page

i need java code to get a part from URL when reload a page and put it in hyperlink and show it in same page.
Example
get google drive file id from page url while reload:
http//example.com#https//drive.google.com/open?id=fileid
.
and add it to this hyperlink with new parameter:
http//docs.google.com/uc?id=fileid&export=download
you cant try javascript regular expressions
var url = window.location.href;
var parts = url.match(/id=(\w+)/);
console.log(parts[1]);

What is the correct way to load a linked page, execute an event, and then unload it in a Chrome extension?

I have a Chrome extension that allows you to download data from certain sites.
For the past few days, I have attempted to add a feature that will download data from the sites when they are linked from other sites without having to visit the page and click the addon created download button IF the link to the site ends with #ndslink.
I figured out a solution, but it is INCREDIBLY sloppy and I am looking for a better way to implement this.
Here's the behavior:
Site A links Site B with an href ending in #nddownload. The link is clicked.
The extension disables the default action (open a new window), and instead creates an iframe on Site A and loads the linked URL into the frame.
The extension now runs Site B's content script, which specifically looks for #nddownload in the url, and, when found, proceeds to download some data that would normally be downloaded through a manual "Download" button added onto the page via the extension.
Here is my code.
Site B's content script:
var decklist = [];
$('.col-name').each(function(i, el) {
// IRRELEVANT CODE TRIMMMED
}
});
var data = decklist.join("\r\n");
var saveData = (function () {
// IRRELEVANT CODE TRIMMMED
}());
$(document).ready(function(){
var html = $('.t-deck-title').html();
fileName = $('.t-deck-title').text() + '.txt';
//html = html.replace(/hearthstonedeckdl/, '</br><a class="download" href="#download">DOWNLOAD</a>');
$('.t-deck-title').html(html);
if (window.location.href.indexOf("#ndslink") > -1) {
saveData(data, fileName);
}
$(document).on('click', 'a[href="#download"]', function(){
saveData(data, fileName);
});
});
Script loaded on all URLs (incl Site A):
var $jg = jQuery.noConflict();
$jg(document).ready(function(){
$jg('a[href$="#ndslink').click(function(e) {
e.preventDefault();
});
$jg(document).on('click', 'a[href$="#ndslink"]', function(){
//saveData(data, fileName);
var frameurl = $jg(this).text();
$jg('body').prepend('<iframe id="nddownload" />');
$jg("#nddownload").attr("src", frameurl);
//e.preventDefault();
// Send link to background and download.
});
});
I feel as if I am committing a horrible sin by going this route, but being new to Chrome extensions and generally being inexperienced I could not find a proper way to handle this. I've probably also broken my extension 10 different ways while attempting to do this so a real solution would be much appreciated.
I'm also actually unsure how to go about properly destroying the iframe once the saveData function completes.

Click url (With a parameter on it), New Page Loads and Javascript sets Iframe src on Page load

Page A : Any page.
Page B : Page that has Iframe on it
What I would to do is something like this.
There is a link on Page A to Page B, Something like: "abcd.com/thewall?parameter"
When they click the url to "abcd.com/thewall" the site loads like it always does with a set src for the iframe. But when they click the url with the parameter like "abcd.com/thewall?parameter" the Iframe will automatically load a different src into that iframe.
I've found many references to do stuff LIKE this on here but none of them do exactly what is described above.
Also I don't know if its possible but would it be able to also when that new url is loaded into the src of the iframe via the parameter call have it auto scroll to the iframe?
I basically know 0 about JavaScript, so please keep it simple so i can understand.
Thank you.
Parse the query string like this and set the iframe's src onDOMReady. Since jQuery is what I'm most familiar with, that's what I'll use:
function getQuerystring(key, default_) {
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"), qs = regex.exec(window.location.href);
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
if(qs == null) return default_;
return qs[1];
}
$(document).ready( function() {
$('iframe').attr('src', getQuerystring('iframesrc', 'virool'));
});
This uses a modified version of the getQuerystring function from the linked page.

How to detect URL and redirect to 404 if it contains script tag

I have a webpage for example www.testing.example.com/home, now if someone manually add script tag onto the url bar like www.testing.example.com/home/<Script>alert('asd')</script> then it should not give alert, it should redirect to 404 page.
I want to say if my url has script tag then it should redirect to 404 page apart form giving the alert message.
How can this be achieved using Javascript?
var regex = new RegExp("%3c.*%3e","i");
var script = regex.exec(window.location.href);
if (script) {
window.location.href = "/404";
}

Categories