localStorage
mechanism, they had an easy way to save and request data on the client side. javascript:(function(d, scrT){ scrT = d.documentElement.scrollTop || d.body.scrollTop; if (scrT) { localStorage['bmk_' + d.location.href] = scrT; } else { scrollTo(0, localStorage['bmk_' + d.location.href] || 0); } })(document)
Home
key was pressed), the bookmarklet assumes that we need to go to the previously saved place, tries to extract it from localStorage
and scroll to the page. If the page is already scrolled, the bookmarklet retains the current scrolling state.localStorage
. Some do not use it at all. Some use actively, but only change or delete keys that they themselves create (for example, Twitter). But there are absolute monarchs: let's say, Facebook periodically completely clears its localStorage
, most likely localStorage.clear()
method. Of course, there is nothing to reproach the latter type of sites - they are sovereign owners in this area and do not have to reckon with the fact that someone else may want to take advantage of their property.IndexedDB
(if the site doesn’t clear it again) or write an extension (Google Chrome allocates its localStorage
to extensions, Firefox doesn’t allow it, but provides its own way of storing data in the general settings database) simple cases to use such complex tools is not very handy.localStorage
belonging to internal frames of the page, if such are found: this will not allow us to make the principle of the same source .Window.postMessage
technology will help us. We just need to create a tiny web page, like this: <!doctype html> <html> <head><meta charset="UTF-8"><title></title> <script> function processMessage(event) { if (event.data.action == 'get') { event.source.postMessage(localStorage[event.data.key], event.origin); } else { localStorage[event.data.key] = event.data.value; } } window.addEventListener('message', processMessage, false); </script> </head> <body></body> </html>
http:
and https:
protocols — for example, on GitHub Pages , which are accessible to all registered users. After that, we can embed this page as an internal frame into any document, establish communication with it from the main document window, in the context of which the bookmarklet works, and use it as a proxy server between the bookmarklet and localStorage
this very page, that is, almost like a small server Database. javascript:(function(d, mySt, scrT){ scrT = d.documentElement.scrollTop || d.body.scrollTop; mySt.origin = d.location.protocol + '//user.imtqy.com'; mySt.URL = mySt.origin + '/storage/storage.html'; mySt.iframe = d.querySelector('iframe#myStorageIframe'); if (mySt.iframe) { mySt.iframe.contentWindow.postMessage( scrT ? {'action': 'set', 'key': 'bmk_' + d.location.href, 'value': scrT} : {'action': 'get', 'key': 'bmk_' + d.location.href}, mySt.origin ); } else { function processMessage(event) { if (event.origin == mySt.origin) { scrollTo(0, event.data || 0); } } addEventListener('message', processMessage, false); mySt.iframe = d.body.appendChild(d.createElement('iframe')); mySt.iframe.style.display = 'none'; mySt.iframe.id = 'myStorageIframe'; mySt.iframe.src = mySt.URL; mySt.iframe.addEventListener('load', function() { mySt.iframe.contentWindow.postMessage( scrT ? {'action': 'set', 'key': 'bmk_' + d.location.href, 'value': scrT} : {'action': 'get', 'key': 'bmk_' + d.location.href}, mySt.origin ); }); } })(document, {})
mySt
(myStorage) service object with three properties: origin
- to provide the interface with the necessary information about the source of the addressee, URL
(based on the previous property) - to set the full address for the frame, and iframe
- to reference the frame itself. Since the pages with the https:
protocol do not allow to embed a page with the usual protocol, at the first stage we determine the current protocol and, depending on it, create the origin
and URL
properties (in the example, an invalid conditional URL is given, the “user” will need to be replaced by real username if the page will be posted on this site).Source: https://habr.com/ru/post/249949/
All Articles