I've this page and in my background page I'm blocking a url which defines stationName variable.
As explained in
Insert code into the page context using a content script I'm able to inject stationName variable( using first technique) but unfortunately it gets injected too late, after execution of all downloaded javascript code from website which don't find stationName variable.
How to define this variable before the javascript execution of page starts?
I've placed this file at resources/irctc containing stationName variable and in chrome.webRequest.onBeforeRequest.addListener I'm redirecting it :
return {redirectUrl: chrome.extension.getURL('resources/irctc/stationnames.js')
};
And it's working but I'm still looking for the right way.
Related
i am working on a progress, but it always crashes when i use the HTML spy. the reason i found out is that the iframe is changing. Is there a way to make it dynamic, so it reads and gets the iframe. so that i can add it into a dataitem and add it into the dynamic spy.
1 time it runs : /HTML/BODY(1)/DIV(6)/DIV(1)/IFRAME(2)/HTML/BODY(1)/DIV(1)/DIV(3)/DIV(1)/DIV(1)/DIV(1)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/TABLE(1)/TBODY(1)/TR(1)/TD(1)
2 time:
/HTML/BODY(1)/DIV(6)/DIV(1)/IFRAME(5)/HTML/BODY(1)/DIV(1)/DIV(3)/DIV(1)/DIV(1)/DIV(1)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/TABLE(1)/TBODY(1)/TR(1)/TD(1)
3 time: /HTML/BODY(1)/DIV(6)/DIV(1)/IFRAME(3)/HTML/BODY(1)/DIV(1)/DIV(3)/DIV(1)/DIV(1)/DIV(1)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/DIV(2)/DIV(1)/TABLE(1)/TBODY(1)/TR(1)/TD(1)
Spy the iFrame if possible, capture the path in a variable,
use Dynamic path for all elements by prefixing it with the iFrame Path.
if this does not work let me know and I can give you another option.
I have an iframe on a page. My requirement is to inject content script to all frame within a page, only when the Main Frame(URL on Omni bar) matches given URL.
developer.chrome.com/extensions/content_scripts
I don't see a way. Can you suggest me a way to achieve my target?
I'm currently using jQuery's $.getScript within my content script to import more Javascript files into my content script. This works very well for me to get all my Javascript files imported, but I am running into an issue where I can't use chrome.runtime.sendMessage inside the imported javascript files to communicate with my background scripts, presumably because the function isn't recognized within a script that's been processed by $.getScript (please do correct me if I'm wrong).
In content.js (injected directly via the manifest file), I have the following code:
$.getScript(chrome.extension.getURL('js/angular-1.2.26-min.js'), function(data) {
$.getScript(chrome.extension.getURL('app/app.js'), function(data) {
$.getScript(chrome.extension.getURL('app/overview/overview-controller.js'), function(data) {
$.getScript(chrome.extension.getURL('js/angular-bootstrap.js'), function(data) {
})
})
})
})
And inside app/app.js (or any of the injected files), I try putting a sendMessage call anywhere, but nothing gets sent. (I log the onMessage event listener in the background)
chrome.runtime.sendMessage({msg: 'test'}, function(response) { alert('done') })
Note: I have also tried importing the Javascript files by sending a message to the background script to use chrome.tabs.executeScrip instead, but I need to be able to inject the javascript files only at a specific time and in a specific frame, so that doesn't help. I've also tried using the 3rd party executeScriptInFrame library but that doesn't seem to be working either. I run into "Blocked script execution in '{{URL}}' because the document's frame is sandboxed and the 'allow-scripts' permission is not set"
My questions:
Is there an effective solution to using chrome.runtime.sendMessage inside a script that's been injected using $.getScript?
Is there a way to use executeScript inside a content script?
Is there an effective way to inject content scripts into a particular frame? Again, from above -- I tried a third party library but ran into an issue regarding the frame's sandboxing. But this is strange since I am able to successfully inject content scripts to that frame when using the manifest to do it directly.
Thanks!
Well, that's an interesting question.
Most methods rely <script> injection, which adds code to the wrong (page) context that has no access to Chrome APIs. I assume this is how $.getScript works. So, this will not work as intended.
Another method is using eval(). According to the documentation, eval() is allowed (but discouraged) in Content Scripts. So you can, in principle, load the script file in a XHR / jQuery AJAX request and then eval() its contents. This should work.
Lastly, you could modify your content scripts only to execute if some condition is met (say, a variable is set), and so injecting into all frames of a tab should be less of a problem. This could potentially be messy though. Note that a content script can find itself in the iframe hierarchy, which may be useful.
I am writing a chrome extension that is a 'content script'
I want to inject a google map on to a webpage.
Problem:
It appears that i have no way to add functions on to the window object, thus i cannot define a callback function for googlemaps to call when it loads.
How do people usually go about mucking with the window?
--
someone on the interwebs suggested i do this:
You can do this easily with a JavaScript URL: window.location =
"javascript:obj.funcvar = function() {}; void(0);"
but when i did this i got an access denied error. it seems like a lot of search results about this problem are outdated.
Content scripts have a separate JavaScript execution ennvironment from the page they run on, so they cannot alter JS variables in the page itself. However, the content script shares the DOM with the page, so you can inject a <script> tag into the DOM which will be loaded and run in the actual page's execution environment.
If I load a string containing HTML into a UIWebView, and that string contains objects (hyperlinks) that are relative to that string, i.e. , where there is some object with id "something," then the link works - click on it and the web view jumps to the referenced object.
What I want is to get navigation to a different file in my project, in other words as though the path to the different file were a URL.
I have found that if the href IS a URL, such as href="http://www.amazon.com", then the link works.
If I put the name of a file, OR the [NSBundle mainBundle] pathForResource: ] of that name, in the href, then the link does not work.
Is there some way I can generate the equivalent of a URL pointing to an HTML file that is in the project, so that an can link to that HTML file?
I found a solution at this link:
How to use Javascript to communicate with Objective-c code?
Essentially, the solution is to implement the UIWebViewDelegate protocol's shouldStartLoadWithRequest method, and "trap" a particular value of scheme. So my links, instead of saying something like:
<a href="http://someplace.location">
are like:
<a href="mylink://#filename.ext">
By catching attempts to load anything with scheme "mylink," I can use:
[[request URL] fragment]
within shouldStartLoadWithRequest, and get the filename.ext. I then release my previous UIWebView, load in the contents of the specified file, and make that the contents of a new UIWebView. The effect is that the links work with normal appearance, even though they are being implemented with my code. I return NO because I don't want the usual loading to take place. If the scheme is NOT mylink, I can return YES to allow normal operation.
Regrettably, I still have no way to jump TO a fragment within a web view. In linking to a real URL, you can say something like "www.foo.org#page50" and jump straight to wherever an object on the new page has an id of "page50." With my method, I can only go to the top of the page.
This is also not going to give me a "go-back" function unless I record the filenames and implement it myself.