-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access element properties after page load #301
Comments
var el = document.querySelector('thead') // or whatever you want to find
getComputedStyle(el).backgroundColor |
var el = document.querySelector('thead');
alert(getComputedStyle(el).backgroundColor); It still shows rgba(0, 0, 0, 0)... |
Yes, that's black. The DOM always stores colors in rgba format.
|
Correction. It's transparent. Black would be rgba(0, 0, 0, 1). |
Well, the color was used just for example purposes. And it seems it was a bad idea. Actually, I need to get the element height. This is required to finish the ad-hoc implementation of the idea I have posted yesterday: asciidoctor/asciidoctor#3391. This is what I have tried: alert($('thead').outerHeight()); var el = document.querySelector('thead');
var height = window.getComputedStyle(el).getPropertyValue('height');
alert(height); var el = document.querySelector('thead');
alert(el.offsetHeight); However, all these attempts, as well as attempts to use |
I don't recommend using jQuery. It's just not needed and it makes it harder to understand what's going on. To get the bounds of an element, I recommend using getBoundingClientRect(). It's very accurate, and works based on where the element is on the page, not where it is in the DOM. var el = document.querySelector('thead');
var height = el.getBoundingClientRect().height |
Thanks, I will try to use The only way which works for me currently is the setTimeout(function() {
var el = document.querySelector('thead');
var height = el.getBoundingClientRect().height;
alert(height);
}, 500); It is interesting that $(window).on('load', function() {
alert('Window loaded'); // Will not be shown. No error messages as well.
}); |
The browser will load the content (ie. a plain text AsciiDoc document) and then the extension will convert the document to HTML5 and replace the content on the page. |
@Mogztter So, it seems that |
We could try to use the executeScript API but I'm not sure when the code will be executed.
Maybe the extension could send a message to notify the page is "ready" (ie. the content of the page has been replaced) using: https://developer.chrome.com/extensions/messaging (not sure |
I understand what the underlying issue is now. Guillaume, could the
extension fire a custom event once it's done?
|
Maybe via the observer api? var targetNode = document.querySelector('body')
var observerOptions = {
childList: true,
attributes: false,
subtree: true, //Omit or set to false to observe only changes to the parent node.
}
function callback(ml, observer) {
let c = document.getElementById('content')
if (window.mutated || !c) return
window.mutated = true
c.firstChild.innerText = 'New Title'
}
var observer = new MutationObserver(callback)
observer.observe(targetNode, observerOptions) You could also e.g. wait for the toc and change it around. Edit: Problem still: You don't know without polling when the rendering is finished. => Maybe useful - but only for a restricted number of use cases, like the one in the example. |
Is it possible to get the element properties (background-color, height, and so on) after page load?
I have tried multiple ways, but for some reason none of them doesn't work:
If you made the changes in already opened file, these alerts will be correct: they will shos
rgb(247, 248, 247)
. So, the only problem is that I can't understand why it does't work right after page load.The text was updated successfully, but these errors were encountered: