battery.js | A tiny wrapper for the HTML5 Battery Status API
kandi X-RAY | battery.js Summary
kandi X-RAY | battery.js Summary
A tiny JavaScript wrapper for the HTML5 Battery Status API. ~~As of October 2015 the Battery Status API is supported by Firefox, Chrome, Opera, Android Browser and Chrome for Android: Due to privacy concerns, support for the Battery Status API has been dropped from most browsers. See:
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of battery.js
battery.js Key Features
battery.js Examples and Code Snippets
Community Discussions
Trending Discussions on battery.js
QUESTION
I am creating a privacy extension that runs a content script on document_start.
The content script needs to inject a script with a dynamic value for each different origin e.g. google.com, twitter.com etc etc.
This is my content script:
...ANSWER
Answered 2017-Aug-21 at 05:39I fixed the issue. The main issue I was having was trying to inject an inline text script which had the following content:
QUESTION
Using my background script background.js, I need to inject a dynamic variable as a content script before injecting another file inject.js also as a content script. Inject.js will need to have access to this variable and run it's code BEFORE any scripts on the page run. I am having difficulties accessing the dynamic variable from the inject.js content script.
manifest.json
...ANSWER
Answered 2017-Jul-14 at 15:23Let's look at your requirements.
Inject.js will need to have access to this variable and run it's code BEFORE any scripts on the page run.
That's not how your code currently works. Your inject.js
is executed at document_end
- which happens after the whole DOM tree is parsed, which means after all page scripts have run (barring asynchronous parts and async
script loading).
Chrome has a solution to that - you can set your execution to document_start
. Then your code will truly run before everything else, while DOM is still not parsed (so document
is essentially empty). With what your code does, it should not create problems (it only relies on document.documentElement
, which will exist).
Problem is, all your code has to be synchronous to still enjoy "runs before everything else" property. Chrome will pause DOM parsing as long as the synchronous part of your code runs, but then all bets are off as it merrily continues to parse (and run code from) the document.
This, for example, disqualifies chrome.storage
and Messaging as access to that is necessarily asynchronous.
I need to inject a dynamic variable [on a page load]
Meaning that you cannot store this in advance in some synchronously-available storage (e.g. in localStorage
or cookies of the website), which would be problematic anyway considering you don't know domains in advance.
Note, for your code in particular, this may not be that much of a factor; your "dynamic" value is in fact fixed per domain. You still don't know in advance which domain will be visited, but you can at least guarantee that on a second load it will be there.
Using my background script background.js, I need to inject a dynamic variable as a content script before injecting another file [that still needs to run before everything else on the page]
That's the tricky part. In fact, as stated, it's simply impossible. You're trying to catch, from the background, the exact moment between the navigation being committed, so that Chrome switched the page to the new domain, and the execution of your document_start
script.
There is no detectable gap there, and no way to tell Chrome to wait. It's a race condition you have no hopes to resolve.
You're trying to use webNavigation.onBeforeNavigate
- before even the navigation is committed. So your injectScript
probably goes to the previous page even, rendering it useless. If you try some other event, e.g . onCommitted
, there's still no telling as to when exactly injectScript
will be treated. Likely after your script.
Fortunately, there is some synchronous storage that's available to the content script that you can push some information to right before the earliest of scripts executes.
Cookies.
However, using the chrome.cookies
API won't help. You need to actively inject the cookie value into the request on webRequest.onHeadersReceived
.
You have to have the value ready synchronously to process it with a blocking handler to onHeadersReceived
, but then you can simply add one Set-Cookie
header and have it immediately available in document.cookies
in your inject.js
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install battery.js
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page