Inserting js into iframe not working as expected - javascript

I am trying to copy jsfiddle's feature on my web page - a user can submit js on a page and it will be executed within an iframe. I ran some test code in jsfiddle and on my page. It works in jsfiddle, but not on my page. Any help is appreciated!
My page renders the css and html, but the js is not executing (the div background should be blue):
Here is the html in the iframe of the fiddle (which works):
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<script type="text/javascript" src="/js/lib/dummy.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type="text/css">
.test { display:inline-flex; padding:40px; background-color:#cccccc }
</style>
<title></title>
<script type="text/javascript">//<![CDATA[
window.onload=function(){
document.querySelector('.test').style.backgroundColor="rgb(21, 160, 249)";
}//]]>
</script>
</head>
<body>
<div class="test" style="background-color: rgb(21, 160, 249);">test</div>
</body></html>
Here is the output to my web page:
<html><head><style>
.test { display:inline-flex; padding:40px; background-color:#cccccc }
</style><script type="text/javascript">//<![CDATA[
window.onload=function(){
document.querySelector('.test').style.backgroundColor="rgb(21, 160, 249)";
}//]]>
</script></head><body><div class="test">test</div></body></html>
The code that inserts the html, css, and js into the iframe:
$(document).ready(function() {
var codeContainer = document.querySelector('.executedCode'); //submitted code passed as values in attributes to hidden div on the page in node/express environment
var html = codeContainer.getAttribute('html');
var css = codeContainer.getAttribute('css');
var js = codeContainer.getAttribute('js');
var sandbox = $('.sandboxed');
sandbox.ready(function() {
var htmlContainer = document.createElement('div');
var cssContainer = document.createElement('style');
var jsContainer = document.createElement('script');
jsContainer.setAttribute('type', 'text/javascript');
var head = sandbox.contents().find('head');
var body = sandbox.contents().find('body');
$(head).append(cssContainer);
$(head).append(jsContainer);
$(html).append(htmlContainer);
$(cssContainer).append('\n\t'+css+'\n');
$(jsContainer).text('//<![CDATA[\nwindow.onload=function(){\n'+js+'\n}//]]>\n');
body.prepend(html);
});
});
window.onload seems to be the breaking point:
I ran a test by simply inserting an alert. Here's what worked and what didn't.
$(jsContainer).text("alert('hi')"); //--works
$(jsContainer).text('//<![CDATA[\nalert("hi");\n//]]>\n'); //-- works
$(jsContainer).text('window.onload=function(){\nalert("hi");\n}\n'); //-- doesn't work

The execution occurs on the line $(jsContainer).text(js) but the html isn't inserted yet.
You just need to move the line $(jsContainer).text(js) after the body.prepend(html) (without the onload event).

Related

I was not able to see images while converting html page to pdf using javascript

here is my code while i download pdf images attributes of html are missing.
suppose in cases like generating invoices we should print tables containing details along with logo.
but images are not displaying in downloaded pdf using this code.Provide me with possible resolution and reason for this.thanks in advance
$(document).on('click', '#btn', function() {
let pdf = new jsPDF();
let section = $('body');
let page = function() {
pdf.save('pagename.pdf');
};
pdf.addHTML(section, page);
})
html,
body {
overflow-x: hidden;
}
#btn {
padding: 10px;
border: 0px;
margin: 50px;
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<title>HTML with Image</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<style type="text/css">
</style>
</head>
<body>
<button id="btn">Convert to PDF</button>
<div id="text">
<h2>HTML Page with Image to PDF</h2>
<img src="http://photojournal.jpl.nasa.gov/jpeg/PIA17555.jpg" width="300px">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
<script src="custom.js"></script>
<script type="text/javascript">
</script>
</body>
</html>
all the html elements are working fine except images . kindly help me with resolving this.
jsPdf does not support adding images the way you are trying to add them, because addtHtml function uses the html2canvas module, to convert your Html to canvas, so jsPdf can render it into pdf. Please check this link below.
https://weihui-guo.medium.com/save-html-page-and-online-images-as-a-pdf-attachment-with-one-click-from-client-side-21d65656e764

String in function call treated differently than string defined in function - strange error

I am trying to make a function that can edit the dom on click, that I can pass different elements into, too reduce duplicate code. However when i pass an id in as string in a function it will not grab the dom element. However if I use the same text in a string declared in the function it will. I have tried console logging the output and using that same string defined by me, in the dom request and that works however variable defined in the function prototype do not it seens unexplainable to me.
e.x will work var x = document.getElementById('button1').style.display="block";
but var x = document.getElementById(str1).style.display="block"; will not, where str is taken from funtion header, even tho str console.logs("button1");
however if i declare a variable r = "button1" inside the function
var x = document.getElementById(r).style.display="block"; - this will work
why are string passed into the function treated differently
example fiddle illustrating the problem
https://jsfiddle.net/peacefulgoldfish/wpvyu5fr/20/
html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
type="text/javascript"
src="/js/lib/dummy.js"
></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type="text/css">
#div1 {
width: 100px;
height: 30px;
background-color: red;
}
#button1{
display: none;
width: 100px;
height: 30px;
background-color:blue;
}
</style>
<!-- TODO: Missing CoffeeScript 2 -->
<script type="text/javascript">
function switchv(){
switchbutton("'block'", "'button1'");
}
function switchbutton(str, str2){
console.log(str2)
r = str2;
var x = document.getElementById(r).style.display="block";
}
</script>
</head>
<body>
<button id="div1" onclick="switchv();">
</button>
<div id="button1">
</div>
<script>
// tell the embed parent frame the height of the content
if (window.parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame", {
height: document.body.getBoundingClientRect().height,
slug: ""
}], "*")
}
</script>
</body>
</html>
Remove the single quotes around "'buttton1'".
This function will work:
function switchv(){
switchbutton("block", "button1");
}
Edit: It seems jsfiddle doesn't like the onclick=switchv(), although inside a standalone html page it works.
Setting the onclick function inside the JavaScript portion seems to work, though. Here's the modified version: https://jsfiddle.net/wpvyu5fr/40/
Going off the link to the fiddle that you sent it seems that you called the str argument in the switchbutton function str1, they need to be the same. Also the double quotes are not necessary.
function switchbutton(str, str2){
var x = document.getElementById(str2);
x.style.display = str;
}

Set width of HR tag using only Javascript

The fluff: This is an assignment, I'm required to create and manipulate the hr tag using javascript. I am required to insert the .js file directly after the opening body tag.
The problem: I cannot get the width property of hr to apply.
I have tried both setAttribute() and .style.width = blah. Neither have worked for me. Where am I going wrong?
function headerFunction() {
var h1 = document.getElementById('h1');
h1.innerHTML = "L. Name";
h1.style.color = "red";
h1.style.fontFamily = "Tahoma";
h1.setAttribute("align", "center");
var h2 = document.getElementById('h2');
h2.innerHTML = "WEB 101.0001";
h2.style.color = "red";
h2.style.fontFamily = "Garamond";
h2.setAttribute("align", "center");
h2.style.fontStyle = "italic";
var h3 = document.getElementById('h3');
h3.innerHTML = "Build Your Resume";
h3.setAttribute("align", "center");
var newElem = document.createElement("HR");
var hR = document.getElementById('header');
hR.insertBefore(newElem, hR.childNodes[4]);
var hrule = document.getElementsByTagName("HR");
hrule.setAttribute("width", "60%");
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" src="finalCSS.css">
<title>Web 115 Final Project</title>
</head>
<body onload="headerFunction()">
<script src="projectJS.js"></script>
<header id="header">
<h1 id="h1"></h1>
<h2 id="h2"></h2>
<h3 id="h3"></h3>
</header>
<form action="" name="form1">
</body>
</html>
You should use your browser's developer tools to check the error console and see that you're getting the following error:
Uncaught TypeError: hrule.setAttribute is not a function
That implies that hurle isn't what you think it is. You're treating it like an element, but really you're getting back an array of elements. You probably want the first one, so you would reference that as hrule[0].
From there, you can set the width with .style as you tried earlier.

Simple javascript change image not working

I want to change an image's src on click of a link. I got a javascript snippet and tried to integrate it but it doesn't work.Im
Here's my HTML:
<html>
<head>
<meta charset="utf-8">
<link href="styles/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<script src="styles/script.js"></script>
<img id="bgimage" src="images/1.jpg"/>
<nav id="nav">A | B |
</nav>
</body>
</html>
Here is my script.js:
var imageId = document.getElementById("bgimage");
function changeImage() {
if (imageId.src == "images/1.jpg")
{
imageId.setAttribute("src","images/2.jpg");
}
else
{
imageId.setAttribute("Src","images/1.jpg");
}
}
This issue is occurring because the script appears in the html before the <img> element. Therefore, the code tries to find the img element, but it can't because the js code executes before the rest of the html is parsed. Correct it by putting the js include tag just before </body>:
<html>
<head>
<meta charset="utf-8">
<link href="styles/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<img id="bgimage" src="images/1.jpg"/>
<nav id="nav">A | B |
</nav>
<script src="styles/script.js"></script>
</body>
</html>
Or, you might want to use DOMContentLoaded to wait until the html has been parsed. Change the js to this, in that case:
var changeImage;
document.addEventListener('DOMContentLoaded',function(){
var imageId = document.getElementById("bgimage");
changeImage=function() {
if (imageId.src == "images/1.jpg")
{
imageId.setAttribute("src","images/2.jpg");
}
else
{
imageId.setAttribute("Src","images/1.jpg");
}
}
},false);
Or you could call document.getElementById() every time changeImage is called
You must place your script just before </body>, or run it at onload.
If not, you run
var imageId = document.getElementById("bgimage");
before loading the image to the DOM, so imageId is null.
Anyway, you could improve your function to
var images = ["images/1.jpg", "images/2.jpg" /*, ... */];
function changeImage() {
imageId.src = images[(images.indexOf(imageId.src)+1) % images.length];
}

Firefox SDK using JQuery Draggable UI

i want to use this: http://jqueryui.com/draggable/ in my Firefox SDK Addon.
This is the source code for a simple example:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#draggable { width: 150px; height: 150px; padding: 0.5em; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me around</p>
</div>
</body>
</html>
My Problem is, that i dont know how to handle the function
$(function() {
$( "#draggable" ).draggable();
});
in my content script, by using port communication between my main and content script.
Unfortunately, you placed the draggable function in a page script, not a content script, making communication way too complicated.
Instead, place all of your jQuery files and create a javascript file (let's call it draggable.js) in the data folder of your add-on (this is the content script) and place the draggable function there. Then use tabs.attach like so.
main.js
var tabs = require("sdk/tabs");
var data = require('sdk/self').data;
var worker;
var widget = require("sdk/widget").Widget({
id: "mozilla-icon",
label: "My Mozilla Widget",
contentURL: "http://www.mozilla.org/favicon.ico"
onClick: function() {
worker = tabs.activeTab.attach({
contentScriptFile: [data.url('jquery.js'),
data.url('jquery-ui.js'),
data.url('draggable.js')]
});
worker.port.on("anything", function(variable) {
console.log("Got variable: "+variable);
}
}
});
Then you can communicate with the script by using self.port.emit in draggable.js and worker.on in main.js
Update: Edited main.js and draggable.js will look like this:
draggable.js
$("#draggable").draggable();
self.port.emit("anything", variable);
You should do anything that pertains to the page in this draggable.js and only send variables out if you need access to any Firefox sdk modules (which are only accessible in main.js).

Categories

Resources