You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// manifest.json{"manifest_version": 3,"name": "Behavior Extension","version": "1.0","permissions": ["tabs","activeTab","storage","debugger","webNavigation","webRequest","scripting"],"host_permissions": ["<all_urls>"],"background": {"service_worker": "background.js","type": "module"},"content_scripts": [{"matches": ["<all_urls>"],"js": ["behavior_bus.js","content_script.js"],"run_at": "document_start"}]}// background.js (Service Worker Context)import{ServiceWorkerBehaviorBus,BehaviorEvent}from'./behavior_bus.js';// Create a BehaviorBus instance for the service worker contextconstbackgroundBus=newServiceWorkerBehaviorBus();// Load behaviors from extension storage or a remote sourceletBEHAVIORS=[];chrome.storage.local.get('behaviors',({behaviors})=>{BEHAVIORS=behaviors||[];backgroundBus.attachBehaviors(BEHAVIORS);});// Listen for tab lifecycle events and forward them to the BehaviorBuschrome.webNavigation.onCommitted.addListener((details)=>{if(details.frameId===0){// main frame onlybackgroundBus.emit({type: 'PAGE_SETUP',url: details.url,tabId: details.tabId});}});chrome.webNavigation.onCompleted.addListener((details)=>{if(details.frameId===0){backgroundBus.emit({type: 'PAGE_LOAD',url: details.url,tabId: details.tabId});}});// Handle messages from content scriptschrome.runtime.onMessage.addListener((message,sender,sendResponse)=>{if(message.type==='BEHAVIOR_EVENT'){// Forward events from content script to background BehaviorBusbackgroundBus.emit(message.event);}elseif(message.type==='FS_WRITE_FILE'){// Handle file system operations using chrome.downloads APIchrome.downloads.download({url: URL.createObjectURL(newBlob([message.content])),filename: message.path,saveAs: false});}});// content_script.js (Page Context)import{WindowBehaviorBus,BehaviorEvent}from'./behavior_bus.js';// Create a BehaviorBus instance for the page contextconstpageBus=newWindowBehaviorBus();// Load behaviors from extension storagechrome.storage.local.get('behaviors',({behaviors})=>{pageBus.attachBehaviors(behaviors||[]);pageBus.attachContext(window);});// Forward all events from page BehaviorBus to background service workerpageBus.on('*',(event)=>{chrome.runtime.sendMessage({type: 'BEHAVIOR_EVENT',event: event.detail});});// Listen for messages from background scriptchrome.runtime.onMessage.addListener((message,sender,sendResponse)=>{if(message.type==='BEHAVIOR_EVENT'){// Forward events from background to page BehaviorBuspageBus.emit(message.event);}});// Example implementation of a behaviorconstExtractArticleTextBehavior={name: 'ExtractArticleTextBehavior',schema: '[email protected]',hooks: {window: {PAGE_CAPTURE: async(event,BehaviorBus,window)=>{consttext=window.document.body.innerText;BehaviorBus.emit({type: 'DISCOVERED_TEXT',selector: 'body',
text
});// This will be handled by the background script's download APIBehaviorBus.emit({type: 'FS_WRITE_FILE',path: 'article.txt',content: text});}},serviceworker: {// Hooks that run in extension background contextPAGE_SETUP: async(event,BehaviorBus,serviceWorkerGlobal)=>{// Setup debugger APIs or other extension-specific functionalityif(event.tabId){awaitchrome.debugger.attach({tabId: event.tabId},'1.3');// ... setup other debugger listeners}}}}};// behavior_bus.js - Core implementationexportclassBehaviorEventextendsCustomEvent{constructor(type,detail={}){super(type,{detail: {
type,metadata: {id: crypto.randomUUID(),timestamp: Date.now(),path: []},
...detail}});}}exportclassBaseBehaviorBusextendsEventTarget{constructor(behaviors=[],context=null){super();this.behaviors=[];this.context=null;if(behaviors)this.attachBehaviors(behaviors);if(context)this.attachContext(context);}attachContext(context){this.context=context;}attachBehaviors(behaviors){for(constbehaviorofbehaviors){this.behaviors.push(behavior);// Attach event listeners based on behavior hooks// ... implementation details}}emit(event){if(!(eventinstanceofBehaviorEvent)){event=newBehaviorEvent(event.type,event);}returnthis.dispatchEvent(event);}}exportclassWindowBehaviorBusextendsBaseBehaviorBus{// Implementation for page context}exportclassServiceWorkerBehaviorBusextendsBaseBehaviorBus{// Implementation for service worker context}
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: