If the code does not always work, then this indicates that the element is being created / loaded on the javascript page. So you need to use standard AJAX methods to move this div.
The methods are: timers, mutation observers, mutation events, and AJAX XHR interception (if applicable). All of them are discussed in other issues, but: mutation events are outdated / erroneous; XHR interception becomes erratic and may not apply here; and mutation observers can be complicated.
I would recommend downloading and adding the waitForKeyElements () utility to your manifest.
Then the code for moving that <div> becomes simple:
waitForKeyElements ( "jQuery selector for div(s) you want to move", moveSelectDivs ); function moveSelectDivs (jNode) { jNode.insertBefore ("APPROPRIATE JQUERY SELECTOR"); }
Save your manifest in run document_end . This is equivalent to DOMContentLoaded - that is, when $(document).ready() fired. By default, run_at fires too late and at unpredictable times. run_at document_start will not do any good, because: timers, mutation observers, etc. all will not fire until the DOMContentLoaded event is for current versions of Chrome anyway (other browsers do not have the same limits).
An alternative method for quick action but a slower web page:
Regarding
(the page loads, and then you can see the movement of the div), which I want to avoid.
There are only two ways to do anything before DOMContentLoaded that work in Chrome:
- You can set
mycss.css to hide the div until you move it. This may be the most cost-effective method in terms of performance. - Or you can override
document.createElement() . This will respond to the new element as quickly as possible (provided that it is created before DOMContentLoaded ), but it can slow down the overall page by a noticeable amount.
Set the contents of the script to run in document_start , and the override code must be entered. So the script will look like this:
addJS_Node (null, null, scriptMain); function scriptMain () { var _orig_createElement = document.createElement; document.createElement = function (tag) { var element = _orig_createElement.call (document, tag); var movingNode = document.querySelector ("MOVING DIV SELECTOR"); if (movingNode) { var target = document.querySelector ("TARGET DIV SELECTOR"); if (target) { target.parentNode.insertBefore (movingNode, target); document.createElement = _orig_createElement; } } return element; }; } function addJS_Node (text, s_URL, funcToRun) { var D = document; var scriptNode = D.createElement ('script'); scriptNode.type = "text/javascript"; if (text) scriptNode.textContent = text; if (s_URL) scriptNode.src = s_URL; if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()'; var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement; targ.appendChild (scriptNode); }
Do not try to use jQuery for this.