@cooby/crx-load-script-webpack-plugin
Version:
A webpack plugin overrides the default load script mechanism for Chrome Extension. Useful for Chrome Extension developers that are trying to lazyload scripts or using HMR when working with the Webpack dev server.
27 lines (19 loc) • 724 B
JavaScript
const { MESSAGE_TYPES } = require('./constants')
const DEBUG = __resourceQuery.includes('debug=true');
const handleMessage = (request, sender, sendResponse) => {
if(request.type !== MESSAGE_TYPES.LOAD_SCRIPT) return
const { file } = request.payload;
DEBUG && console.log('executeScript:', file);
if (chrome.runtime.getManifest().manifest_version === 2){
chrome.tabs.executeScript(sender.tab.id, { file });
} else {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
injectImmediately: true,
files: [file],
});
}
// Return true to indicate you want to send a response asynchronously.
return true;
};
chrome.runtime.onMessage.addListener(handleMessage);