inject.js 1.0 KB

123456789101112131415161718192021222324
  1. /**
  2. * injectScript - Inject internal script to available access to the `window`
  3. *
  4. * @param {type} file_path Local path of the internal script.
  5. * @param {type} tag The tag as string, where the script will be append (default: "body").
  6. * @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script}
  7. */
  8. function injectScript(file_path, tag) {
  9. var node = document.getElementsByTagName(tag)[0];
  10. var script = document.createElement("script");
  11. script.setAttribute("type", "text/javascript");
  12. script.setAttribute("src", file_path);
  13. node.appendChild(script);
  14. }
  15. function injectData(property, value, tag) {
  16. var node = document.getElementsByTagName(tag)[0];
  17. var span = document.createElement("span");
  18. span.setAttribute("id", `property-${property}`);
  19. span.setAttribute("data-value", value);
  20. console.log(property, value, tag, node);
  21. node.appendChild(span);
  22. }
  23. injectData("extension-id", chrome.runtime.id, "body");
  24. injectScript(chrome.extension.getURL("js/content.js"), "body");