How to catch clickable links clicks in Chrome extension? - google-chrome-extension

I use content_scripts in chrome extension to catch the event of opening new site. But when i click on this site a link that redirects me to subsite the event is not fired.
It's not usual site that uses reload to handle link clicks. After clicking link i'm redirected to something like (AJAX?):
http://somesite.com/page#something
So i suppose it's dynamically loaded. How to handle all events of reloading of this page?
How to catch every event of loading page not only by entering into URL field but also by clicking links?

You are going to have to hook the mousedown event. With JQuery it looks like this:
$('a').mousedown(function(){
alert($(this).attr('href'));
});
You'll have to examine the value of the href for $(this) and then do whatever you need to do.
Sounds like the page you are working with has frames so you need "all_frames": true in your manifest under the content_scripts section.

Related

Browser Console - How to include javascript code on every page redirect?

Is is possible to load special javascript code (created by me) to my browser console (mozilla firefox) on every redirect page? I want to create a special script-robot, which will be do special actions on some website. For example:
Click button: "Add new advert"
... NOW IS REDIRECT
Write to input[name='title'] advert title
Write to input[name='description'] advert description
Click button: "Add now!"
... NOW IS REDIRECT
Click button: "Logout".
So I want to include my own javascript code to my console and I want automatically reinclude this file on every page redirect. Is is possible?
Thanks.
yes, it is possible. What you would do is create your own google extension with links to a js file to run. Here are some instructions on how to make said extension.

Substitute Chrome tab content without connecting to server

I am writing a Chrome extension and for certain tabs I need:
Prevent a tab from connecting to its URL
Show custom text within the tab
(UPDATE) Tab's URL to stay the same preferably
Please suggest a direction how to achieve both the goals.
What I have tried... I can prevent original content from loading:
chrome.webRequest.onBeforeRequest.addListener(...
return {cancel: true};
A technical page is shown then:
Requests to the server have been blocked by an extension.
Try disabling your extensions.
ERR_BLOCKED_BY_CLIENT
(Maybe it's possible to have a custom technical page?)
But this also prevents my content script from loading and thus I lose ability to alter tab's DOM.
Without onBeforeRequest I can show custom text in the tab via a content script, but Chrome still gonna request the original URL.
Instead of cancelling the request by returning {cancel: true}, redirect the request to a URL which contains your custom content by returning {redirectUrl: "..."}.
The easiest way of doing this will be to include a "request blocked" HTML document within your extension as a web-accessible resource. You can pass parameters to the document using query string arguments -- they are ignored when fetching web-accessible resources, but can be accessed from content scripts using window.location.search.
A notable example of an extension that uses this approach is uBlock Origin, which uses a web-accessible HTML page document-blocked.html as a substitute for blocked web pages.

apache nutch file download after button clicking

Is it possible for apache nutch to download a file from a url after button clicking and index it?
Example - Suppose the url is http://example.com
File is downloaded after clicking a button on http://example.com and getting saved. How can we do it in apache nutch.
This really deppends on how the button is implementend, if the download action is just a link to the direct file it should work fine. If the download action happens through some javascript event or in a form with a <button> element then Nutch would not detect this. Perhaps using the protocol-selenium could help if the link is generated using some javascript.
EDIT
Since the button is triggered on something like the onclick event, then your best bet is to use protocol-interactiveselenium (https://github.com/apache/nutch/blob/master/src/plugin/protocol-interactiveselenium/README.md) and perhaps implement a custom handler if you need to. I haven't tested this personally but should work.

chrome extension options change default popup.html

I have a chrome extension where the popup.html simply has an iframe that loads a page.
I want to allow users to select their language, and as I'm helping a charity do this each language page is very different as they need very different content based on the country!
Therefore, I can't just replace some of the fields like the google developer page example does; I need to change the page that is loaded in the iframe.
e.g. In my directory where the pages are stored I have english.html, german.html, spanish.html all of which are completely different pages.
By default the english.html page loads in the iframe, but the user should be able to go into the options file and select german so when they click on the extension the german.html loads by default every time.
Here is the jsfiddle showing what I currently have in the popup.html, and the popup.js:
http://jsfiddle.net/hemang2/EDV82/
You'll see the flickr API being called, but that's only there because I wasn't sure how to get rid of it!
So essentially my question is how to link the options file to change the default url loaded in the iframe.
Use the setPopup method: http://developer.chrome.com/extensions/browserAction.html#method-setPopup
It allows you to set any html file as your popup.

How can I inject CSS and Javascript into an iframe loaded in a chrome extension

I have an iframe in my chrome extension that loads a website, I'd like to inject CSS and Javascript into the website but only when its loaded in my extensions iframe (so it doesn't get affected when the user browses to the site normally).
I was surprised that Chrome extensions have cross-domain security policies applied to them when they try to access the internals of an iframe even though they can make cross-domain XHR requests.
Is there an easy way to do this? I though I might be able to do it using executeScript but it requires a tab ID.
The only way to know if you are in an ifrmae is the window is not the top.
if (window == top) {
// Not in iframe
}
else {
// In an iframe
}
You can only do that for content scripts, not for stylesheets. What I usually do is within the content script, I check if it isn't in the top window, and then continue the script, otherwise I just end it.
Recap
Inject the Content Script for that URL
In the content script, check if the window property, if (window != top) { loadContentScript() }
Create the CSS in the JavaScript if you are too worried about it affecting. You shouldn't though if you use unique ids.
Hope that helped, I do that for a few extensions of mine.

Resources