How can Chrome Extension talk to itself when installed by different users? - javascript

The Chrome browser supports multiple users (personas), so we can load a web page with different cookies and session data. This is working great, doing what I want. Now I wish for an extension installed on multiple user accounts to share information between users.
I cannot see how to do this, help please?
Details and Ideas:
By setting some cookies I can change some preferences of the target web page, to use some new features. Some features are different, but the core information should be the same. I wish to compare them via extension code.
By using the people feature of the chrome browser, ( personas ) I can load both old and new versions of our web page in chrome, and compare side by side.
I also have a chrome extension which scrapes a target web page, to pull out information like names, prices, information. This is also working great. I can manually check the scrape results of old or new versions of the page.
Now for the challenge : How can I compare scrape results between web pages loaded on different people (personas). Each "people" has the extension installed and running.
When I send an external message using the extension ID, only extensions on the same "people" receive it.
When I look at the background pages for each "people" extension, they are different. Setting a value for my Extension in one does not affect my Extension in the other.
// code in background page.
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse){
console.log('background page was hit');
});
// paste code in browser console.
chrome.runtime.sendMessage('id_here', {getTargetData: true},
function(response) {
console.log(response);
});

You can't share data between users on the client. This would allow your extension to potentially download all of a user's data and share it with another user.
That said, you can just push the data to a shared server and use that to compare (using HTTP or Websockets)

Related

Retrieve URL scheme with javascript

I was wondering if there is a way to retrieve the URL scheme of a browser application using javascript (on mobile)?
For example:
You're browsing a web page on your phone using Google Chrome. Would it be possible for that page to run some javascript and retrieve the googlechrome:// scheme?
Thanks!
Edit:
I just found out that the 'navigator' object has a method called 'registerProtocolHandler' which lets websites register themselves as possible handlers for particular protocols.
This comes pretty close to what I need. The only problem is that this requires permission from the person who is visiting the website, which doesn't compliment the flow I'm going for. Also, it doesn't support Safari on iOS..
By the time your JavaScript is running on the webpage, the user's phone has used the URI Scheme (say googlechrome://) to choose a web browser it has installed. The browser then requests your site using a web protocol like http://. This is what window.location.protocol will provide.
So, JavaScript isn't aware of 'schemes' in the sense of 'browser applications'.
However, you can figure out the user's browser from their User Agent and then deduce a possible URI scheme from this. Still, you can't be certain the user didn't just open up Chrome and navigate to your site themselves, without ever tapping a link.
Note that user agents are not always reliable:
users of a browser can change the value of this field if they want (UA spoofing).

Get localStorage from within extension without loading a page

I know how to get the localStorage from any open wep page by using content scripts. So I'm basically able to open a new tab with my own web page and read the storage data with a content script and message it to the background page.
But now I'd like to do this without loading an external page every time. Is there a way to access the localStorage of a page directly from within the extension? Maybe some query to chrome directly.
I don't see any API for that.
Your options are:
Make a native messaging host application that would read database files directly from Local Storage directory in the browser user profile. An example: What's the best way to read Sqlite3 directly in Browser using Javascript?
Put the other page into an iframe: Is it possible to use HTML5 local storage to share data between pages from different sites?
P.S. "Ironic side note" quoted from Cross-domain localStorage article by Nicholas C. Zakas.
Who knew cross-domain client-side data storage would be useful? Actually, the WHAT-WG did. In the first draft of the Web Storage specification (at that time, part of HTML5), there was an object called globalStorage that allowed you to specify which domains could access certain data. [...]
The globalStorage interface was implemented in Firefox 2 prematurely as the specification was still evolving. Due to security concerns, globalStorage was removed from the spec and replaced with the origin-specific localStorage.

HTML5 Local Storage VS App Cache Offline Website Browsing

After going through multiple articles,
I am still not clear on the difference between Local Storage and App Cache Manifest.
Also referred: Is AppCache = Application Cache = Web Storage's LocalStorage? (SO 10986026), Application Cache is a Douchebag (A List Apart)
My AIM is to build a website with specific pages be allowed offline to user on user demand.
Steps followed :
I opened a site on Chrome : http://www.spritecow.com/
And checked AppCache : chrome://appcache-internals/
And the site was cached.
I closed Chrome and reloaded it. The cache was still there. Exactly what I need for offline browsing
Now how is this different from local storage? Tried to find the difference but all sites answer in purpose, i.e. AppCache for templates' caching and Local Storage for content within the template.
Certain sites do not prefer AppCache as it reloads entire cache for a single line change. Certain sites prefer only local storage. While some go for the combo of AppCache(template) and Localstorage.
Now the doubt is :
Local storage stores on client machine. How does AppCache storage is different if I can still access it even browser is closed.
As clearing cache will clear AppCache then i'd go for only Local Storage.
What is the best practice to be followed for offline browsing? I am completely new to this and need a little clarity on the same
EDIT
The doubt is not answered by the link (Is AppCache = Application Cache = Web Storage's LocalStorage?) as this gives difference but not based on the purpose of Offline Browsing Practices (which is the aim for this doubt).
AppCache use a manifest file to define what files used by the app should be stored (You can cache files and ressources like HTML pages, JS scripts, CSS styles, images,...)
LocalStorage will store data but not pages. So every javascript object that you can stringify can be stored in the localStorage.
So AppCache and localStorage aren't the same, but they are complementary.
Example
Imagine a web calendar that you want to be available offline (note: for this example, we use here a static page and data are loaded with javascript. The same can be made from a dynamic page, but this example use static).
The appcache will store the html page and the ressources that it uses (javascripts, css, images) to render you page.
As you have put in your manifest file everything to be cached for the next offline access, the pages are stored and you'll be able to display your page offline at the next visit.
But problem, your calendar is displayed but is empty. All meetings and events of the month aren't there. This is because your page is stored, but you still need network to load the meetings in your calendar. And as you're offline, you have no network...
If you want to have all your meetings available offline too, you'll have to store them in the localstorage (not in the appCache, because it's not a page, it's data accessed by JavaScript.)
So you will need to change your Javascript function from this :
function initApp() {
var data = loadDataWithAjax();
renderPlanning(data);
}
to this
function initApp () {
var data;
if(offline) {
data = loadFromLocalStorage();
} else {
data = loadDataWithAjax();
storeDataInLocalStorage(data);
}
renderPlanning(data);
}
Appcache will even work if you are totally offline and your browser is closed and then you open your browser and type in the URL while still offline — the page loads! Check this site here … load it once while online and then disconnect from the Internet and close your browser … and then reopen browser and try to visit it while still offline.
localStorage needs connection first to load the js code needed to get the data from it.

Coding browser extensions, Addons, Firefox, Safari, Chrome etc… Is this possible?

I'm not very familiar with browser extensions and before I begin to deeply explore them I have a few questions.
Let's say the extension injects JavaScript in the current website the user is visiting (if that's even possible). That injected JavaScript code will get, let's say the current URL for example purposes, and send it and store it on a database. Next time the user visits the same website, the user will get an extension notification informing that is the second or third or X time he or she has visited the same website.
Now that I have gave you the scenario, is the following possible? Injecting JavaScript from a browser extension to the current visiting website. If so, can I make some AJAX communication with the JavaScript and a PHP server?
Yes, you can inject stuff. See e.g. Insert code into the page context using a content script and How to inject javascript into page, from a Firefox add-on, and run it? or one of the many dupes there likely are.
You can then use whatever communication would be available between the site and a server, e.g. XHR, WebSockets, JSONP.
Please also check the policies of the Chrome Web Store and Mozilla Add-ons site regarding content/code injection and privacy rules. E.g. the Mozilla Add-ons will reject your add-on if you injected remote scripts (meaning code that is not bundled, e.g. originating from e.g. http:) and may also reject your stuff if you track users without prior explicit user consent.

How to fetch javascript heavy pages from chrome extension

I am developing an extension that fetches pages that the user is likely to access on a website. My extension uses jQuery.get() to fetch a page. This works correctly for a site like amazon.com.
But if the user logs in to gmail and I try to fetch some other pages like "account settings", I get an incomplete page. Somewhere in that page, I get the message:
"Your browser does not support Javascript or Javascript has been disabled.As your browser does not support Javascript or has Javascript disabled, we are not able to display the requested page."
Is there some way to fetch complete page in such cases?
I ended up opening a new tab and fetching the page in that tab. Then using content script, I analyze the page data. Sure this is a problem in the sense that a user will see newly opened tab. But then it is also transparent to the user.
If you are developing an extension on Firefox using Jetpack, you can use page-worker which is an invisible page and gives access to the DOM.

Categories