Skip to content

Instantly share code, notes, and snippets.

@yeungon
Created December 4, 2022 07:05
Show Gist options
  • Save yeungon/e425cc3603b2048697a714ceb6a322fd to your computer and use it in GitHub Desktop.
Save yeungon/e425cc3603b2048697a714ceb6a322fd to your computer and use it in GitHub Desktop.
inject css and js to chrome extension
/**
* injectScript - Inject internal script to available access to the `window`
*
* @param {type} file_path Local path of the internal script.
* @param {type} tag The tag as string, where the script will be append (default: 'body').
* @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script}
* @reference https://gist.github.com/devjin0617/3e8d72d94c1b9e69690717a219644c7a?permalink_comment_id=3766826#gistcomment-3766826
*/
function injectScript(file_path, tag) {
var node = document.getElementsByTagName(tag)[0];
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file_path);
node.appendChild(script);
}
injectScript(chrome.runtime.getURL('content.js'), 'body');
// Inject CSS
function injectStyle(file_path, tag) {
var node = document.getElementsByTagName(tag)[0];
var style = document.createElement('link');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('href', file_path);
node.appendChild(style);
}
injectStyle(chrome.runtime.getURL('style_dictionary.css'), 'head');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment