Issues with custom object injection to the `window` object

I'm facing an issue in dev mode, which is kind of pain in the neck as I'm developing a crypto-wallet, that I want to be able to connect with the Dapp (decentalized application). So in order to do it I need an RPC-like communication between my extension and the app, which was adviced to me to use the content.ts as the solution, because And now by folllowing the instructions in the docs https://docs.plasmo.com/framework/content-scripts. When I type in the window.hackerWallet then it returns undefined. For some more details of the env and what I did to solve it: I use Brave nightly as web-browser, and also get an weird warning in the console. I checked out if my content.ts file is correctly set in root directory and it is. I included in the permissions array in the manifest object in package.json the "scripting" I gone through the https://github.com/PlasmoHQ/plasmo/issues/15 request on github and adjusted the code to my needs, to firstly checkout if the object gets injected into the window or not. My content.ts file contains such code:
import type {PlasmoCSConfig} from 'plasmo';

export const config: PlasmoCSConfig={
all_frames:true,
world:'MAIN',
}

const loadRpcIncject= async (tabId:number)=>{
chrome.scripting.executeScript(
{
target: {
tabId // the tab you want to inject into
},
world: "MAIN", // MAIN to access the window object
func: ()=>{
if(window){
(window).hackerWallet= {'hacker':'wallet'}
}
} // function to inject
},
() => {
console.log("Background script got callback after injection")
}
);
}

chrome.tabs.onActivated.addListener((activeInfo)=>{
console.log('Activated console log', activeInfo);
loadRpcIncject(activeInfo.tabId);
})
import type {PlasmoCSConfig} from 'plasmo';

export const config: PlasmoCSConfig={
all_frames:true,
world:'MAIN',
}

const loadRpcIncject= async (tabId:number)=>{
chrome.scripting.executeScript(
{
target: {
tabId // the tab you want to inject into
},
world: "MAIN", // MAIN to access the window object
func: ()=>{
if(window){
(window).hackerWallet= {'hacker':'wallet'}
}
} // function to inject
},
() => {
console.log("Background script got callback after injection")
}
);
}

chrome.tabs.onActivated.addListener((activeInfo)=>{
console.log('Activated console log', activeInfo);
loadRpcIncject(activeInfo.tabId);
})
Now the questions are: - Has something changed in case of handling of the injection into window ? - Can anybody poinout where might be the potential issue that is causing the error ? I'll be glad for some answer, cheers 🙂
GitHub
EXP | Inject to window · Issue #15 · PlasmoHQ/plasmo
What is the example you wish to see? Sometimes extension might need to inject own functions or objects directly to users window instance. For example Metamask attaches their provider to window.ethe...
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?