I am puzzling my way through my first 'putting it all together' Chrome extension, I'll describe what I am trying to do and then how I have been going about it with some script excerpts:
- I have an options.html page and an options.js script that lets the user set a url in a textfield -- this gets stored using localStorage.
function load_options() { var repl_adurl = localStorage["repl_adurl"]; default_img.src = repl_adurl; tf_default_ad.value = repl_adurl; } function save_options() { var tf_ad = document.getElementById("tf_default_ad"); localStorage["repl_adurl"] = tf_ad.value; } document.addEventListener('DOMContentLoaded', function () { document.querySelector('button').addEventListener('click', save_options); }); document.addEventListener('DOMContentLoaded', load_options );
- My contentscript injects a script 'myscript' into the page ( so it can have access to the img elements from the page's html )
var s = document.createElement('script'); s.src = chrome.extension.getURL("myscript.js"); console.log( s.src ); (document.head||document.documentElement).appendChild(s); s.parentNode.removeChild(s);
- myscript.js is supposed to somehow grab the local storage data and that determines how the image elements are manipulated.
I don't have any trouble grabbing the images from the html source, but I cannot seem to access the localStorage data. I realize it must have to do with the two scripts having different environments but I am unsure of how to overcome this issue -- as far as I know I need to have myscript.js injected from contentscript.js because contentscript.js doesn't have access to the html source.
Hopefully somebody here can suggest something I am missing.
Thank you, I appreciate any help you can offer!
-Andy