I am trying to create an basic chrome extension that adds a toolbar to the left hand side of any webpage.
I am wanting to create space on the left hand side which I can then fill with DOM elements.
I create space using style.setProperty():
document.body.style.removeProperty("transform");
document.body.style.removeProperty("-webkit-transform");
document.body.style.setProperty("width", "90%", "important");
document.body.style.setProperty("margin-left", "10%", "important");
This creates space on the left hand side of most webpages but not all. I then try to add an element to the webpage:
var toolbar = document.createElement("div");
toolbar.style.setProperty("color", "red");
toolbar.style.setProperty("left", "-10%");
toolbar.style.setProperty("top", "0px", "important");
var testText = document.createTextNode("Buttons will go here");
toolbar.appendChild(testText);
document.body.appendChild(toolbar);
This seems to add an element to the bottom of the webpage but has a few strange errors as well. In some webpages it will not show at the bottom, in some webpages it will create multiple times. I have almost not figured out how to move this into the left margin that I created for it.
I am currently calling the script to alter the page from an eventPage.js file:
chrome.webNavigation.onCompleted.addListener(function (details) {
chrome.tabs.executeScript(details.tabId, {
file: "createSpace.js"
});
});
And to finally get to my questions:
- Can you create objects inside a margin?
- How can you apply the CSS (moving the page to create left hand space) to the entirety of any webpage?
- How do you chose where to put the DOM object if you do not know what elements the page will have (because it is supposed to work on all)?
Forcing the entire layout to move in order to make space for your toolbar may prove unpredictable. Instead, I suggest you go with a sliding toolbar which auto hides when you are not hovering on it.
EDIT:
However, if you must insert the sidebar to the page, you could try the following:
This would move all the content of the page into a new div, which would give you flexibility in positioning it next to the sidebar. However, be warned that on some edge cases, you may result in breaking the content's look, since you can't promise that it would behave nicely once you force it to smaller width.