dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.69k stars 641 forks source link

Dexie returns empty array #179

Closed panther99 closed 8 years ago

panther99 commented 8 years ago

I have Google Chrome extension which get data to IndexedDB with Dexie and it works okay, however, when I try to get that data from another HTML page I get an empty array.

<head>
    <meta charset="utf-8">

    <script src="js/jquery.min.js"></script>
    <script src="js/Dexie.min.js"></script>

</head>

<body>

    <div class="container">

    </div>

    <script>
        var db = new Dexie("myDB");
        db.version(1).stores({
            likes: 'url, date, time'
        });
        db.open();

        db.likes.toArray(function(like) {
            console.log(like);
        }).catch(function(error) {
            alert("Error: " + error);
        });
    </script>

</body>

`

Everything is loaded up, I checked database few times with my extension but nothing. Console doesn't show any errors.

dfahlander commented 8 years ago

I've personally never built a chrome extension, but if you would run the same code as a plain html page and put it on different hosts, it won't share the same db between different origins because indexedDB will have to respect the same-origin policy.

I know there are people that have built chrome extensions that can use indexedDB and share db across all URLs but I guess you need to find out how to invoke the extension on its own origin no matter the page being visited.

jimmywarting commented 8 years ago

I have built a client side tool for inspecting client side storage As a bookmarklet: https://clientmyadmin.github.io and as a chrome extension

dfahlander commented 8 years ago

I'm not sure I understand the issue. As I wrote previously, different URLs will never share the same indexedDB databases unless they are hosted from the same hostname. But I may have misunderstood the issue. ?

panther99 commented 8 years ago

So, if I add data to IndexedDB from one url I can't get it from another url? This code is from local file stored on my PC. Point of extension is to get some DOM data from a website, store it in IndexedDB and get same data with page stored on user's PC. Is this possible? Everything is done on client-side in same browser.

@jimmywarting Thanks, that would be helpful for some projects.

dfahlander commented 8 years ago

No, it's not possible even though you are on the same browser and never touch the server. The reason is that it would otherwise be a security issue of your client-data generated by site A could be read from a script at site B. The same rules that apply to cookies, even though cookies can be shared between hosts in the same domain, while indexedDB is databases are always bound to the exact hostname.

But there are techniques to post messages between different origins. If your extension runs on one origin, you could possibly post a message from another origin to your extension's origin and use that data as a command to store/read from DB, but this is another topic, and as I haven't done any chrome extension, I'm the wrong person to ask. Check if the links below make you wiser or not:

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

http://stackoverflow.com/questions/23874290/can-we-use-indexddb-objects-stores-between-two-pages

http://stackoverflow.com/questions/13453418/is-it-possible-for-a-chrome-extension-to-access-an-indexeddb-database-created-by

panther99 commented 8 years ago

Thanks for informations.