Referencing image resources in addon data folder - firefox

I have an image in the data folder: data\img\myimage.jpg. I want to reference it in a content script. More over, I want to alter the DOM of the host page (the page where the content script is injected to) by putting that image there.
I tried to follow what Jeff says here: http://blog.mozilla.org/addons/2012/01/11/sdk-1-4-known-issue-with-hard-coding-resource-uris/ (because I didn't find any other references to that issue), but nothing worked.
What is the URL I need to use in the page in order to reference an image from the add-on's folders?

The data folder is available in main.js and you can pass that url as a content script option.
To preview the url in the console from main.js:
var data = require("sdk/self").data;
console.log(data.url('img/myimage.jpg'));
To pass the url to the content script in main.js:
var image_url = data.url('img/myimage.jpg');
...
contentScriptFile: data.url('myScript.js'),
contentScriptOptions: {"image_url" : image_url}
...
To view the url from the content script:
alert(self.options.image_url);

Like Jeff mentioned in that post, you can use the self module to get a URL to your images in the data directory. To get that information in a content script, you can either pass it in via messages(page-mod example here, but similar to all content scripts) to communicate with the script, or if you're inlining your content script, can just 'bake' it in. The self module won't be available in the content script, but passing in a string is fine.
let url = self.data.url('img/myimage.png');
pageMod({
contentScript: 'var url = ' + url + ';' + 'console.log(url);',
include: '*.mozilla.org'
})

Related

Can't parse html correctly / empty body after parsing

I am facing an odd problem. I im trying to parse the following html:
The problem is that when I do
response.xpath('//div//section//div[#id="hiring-candidate-app"]')[0].extract()
I only get
'<div id="hiring-candidate-app"></div>'
instead of all the content under hiring-candidate-app.
I would like to get, for instance, inside-content, but it looks like I am not even getting that in the response. This webpage requires to be logged in, which I am.
Thanks in advance!
It looks like your Xpath is grabbing the right thing. But your issue might have to do with the '[0]' part of the call. I would remove that to get the full content of the div.
It looks like the elements in question sit on an <iframe>, and therefore live in a different context. You need to activate or switch to the context of the iframe, eg. using JavaScript to interact with an iframe and the document inside of it, e.g.
//Note: Assigning document.domain is forbidden for sandboxed iframes, i.e. on stacksnippets
//document.domain = "https://stacksnippets.net";
var ifrm = document.getElementById("myFrame");
// reference to iframe's window
//var win = ifrm.contentWindow;
// reference to document in iframe
var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
// reference an element via css selector in iframe
//var form = doc.getElementById('body > div > div.message');
// reference an element via xpat in iframe
var xpathResult = doc.evaluate("/html/body/div/div[1]", doc, null, XPathResult.ANY_TYPE, null);
<iframe id="myFrame" src="https://stacksnippets.net" style="height:380px;width:100%"></iframe>
However, as you can see when you run the snipped, cross-document interactions are only possible if the documents have the same origin. There are other, more involved methods like the postMessage method that provide the means of interacting cross-domain.

How to store current URL of the page in a variable for validation purpose on TestComplete

I am working on testcomplete automation tool. I want to know how can I store current URL in a variable for validation. Example,
I click a link that takes me to a particular page. I want to validate against the URL of that page. how can I do that?
You can use the URL property of the Page object. Information on this property along with a script sample can be found in this document:
URL Property (Page Objects)
If it's not opening in a tab/window then this should work fine:
function PageSample()
{
Browsers.Item(btIExplorer).Run("http://smartbear.com/");
// Obtains the browser process
var browser = Sys.Browser("iexplore");
// Obtains the page currently opened in Internet Explorer
var page = browser.Page("*");
// Use the page object property and add a checkpoint to validate
…
}

Content-Disposition inline filename issue with IE

I am displaying a pdf in browser with inline from API using an aspx page.
While saving the pdf using Chrome/Firefox, takes the filename from header("Content-Disposition", "inline;filename=xyz.pdf")
But while saving the pdf using IE it does not reads the filename from header("Content-Disposition", "inline;filename=xyz.pdf"). instead it takes the aspx name.
Technical details
I have an xyz.aspx page.
The xyz.aspx page will invoke an API for a document.
Then the downloaded document from API will transferred to browser with inline to display the pdf document.
Am setting the response header as below and writing the file bytes.
HttpContext.Current.Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=\"" + Name + "\"");
HttpContext.Current.Response.ContentType = "application/pdf";
Issue:
While saving the opened pdf in IE it takes xyz.aspx instead of the name from header.
Requirement:
While saving the pdf using IE, it need to save with the name of pdf.
I googled so much, as every one tells its IE behavior. I hope some one knows a solution.
Note: I have to display the pdf in browser and then save. Not to download using "attachment"
It is true some versions of IE can't handle ("Content-Disposition", "inline;filename=...")
This is because filename=... was originally intended for the attachment disposition. Not all browser-based PDF viewers can handle it.
The only solution I see is to allow access via a different url.
Suppose you have a route to the pdf like: /pdf/view. If you change it to /pdf/view/filename and you configure your application to handle this route in the same way as /pdf/view your problem is solved.
You can also re-write the download url on the webserver.
Depending on your webserver you have various ways of doing this.
I have also tried with all kind of headers and methods.
In the end, my solution was
private FileResult ReturnStreamAsPdf(string fileName, Stream stream)
{
ContentDisposition cd = new ContentDisposition
{
FileName = fileName,
Inline = true // false = prompt the user for downloading; true = browser to try to show the file inline
};
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");
return new FileStreamResult(stream, MediaTypeNames.Application.Pdf);
}
and the Route Attribute on the method:
[Route("api/getpdfticket/{ticketnumber}")]
public async Task<FileResult> GetPdfTicket(string ticketnumber)
And the href:
href="#($"/api/getpdfticket/{product.TicketNumber}.pdf")"
It seems like Microsloft is still inventing their own standards:
http://test.greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf
PDF Handler : content-disposition filename

best way to show images stored in the app from a webview in SWIFT

I have a webview and would like to show an image in the webview (html)
My HTML :
hello !img src="myimage.png" alt="myimage" height="42" width="42"!
(I used ! as tagend and tagstart, because I don't know how to add this here without be interpreted as HTML, even I pasted as code)
The myimage.png should be stored in app itself and not be loaded from a websource.
I don't know how to do that in a best practice way. Any help ?
UPDATE
I tried with referenced Article, but still not succeeded:
My Code for this:
let path:NSString = NSBundle.mainBundle().bundlePath;
var baseURL:NSURL=NSURL(fileURLWithPath: path as String)!;
var htmlString:String! = texttemp
myWebView.loadHTMLString(htmlString, baseURL: baseURL)
The same Image I can already load like the following -> works:
var image = UIImage(named: "myimage.png");
Your updated code isn't right. You are creating a path to the bundle, not to the specific file. You need to use the NSBundle method pathForResource:ofType (or one of its variants) to build a path to your file. Then use that to create the URL.
The pathForResource:ofType family of methods return nil if the file can't be found, so you should check that you are getting back a path.
EDIT:
Looking at it more closely, I see that you are using the URL as the base URL for a call to loadHTMLString. This does look like a sound approach. What is your HTML string, and where is the image in your bundle?

Firefox addon sdk tab script talk to page mod script

In main.js I am opening a tab and attaching a script with page-mod. The html file that is being opened it has a bunch of regular includes.
Sort of have two issues.
The script from page mod does not get attached until after those other scripts are loaded, and also
the regular scripts can't access variables defined in the script that is attached with page mod.
You have to send messages.
Directly
In your page-mod, send a message:
page-mod.js
window.postMessage(projectUniqueId + '|' + message, domain);
If your page needs to work with all domains (being a plug-in), you might need '*' as a domain.
tab-attach.js
window.addEventListener('message',function(event){
var words = event.data.split('|');
if (words[0] == projectUniqueId){
handle(words[1]);
}
});
The script from page mod does not get attached until after those other scripts are loaded,
Specifying contentScriptWhen: 'ready' in the page-mod constructor should "[l]oad content scripts once DOM content has been loaded, corresponding to the DOMContentLoaded event"
the regular scripts can't access variables defined in the script that is attached with page mod.
Have a look at Expose objects to page scripts. You need to use
var contentScriptObject = {"greeting" : "hello from add-on"};
unsafeWindow.clonedContentScriptObject = cloneInto(contentScriptObject, unsafeWindow);
in the content script to make the object accessible.

Resources