javascript - Inject content script on extension reload programmatically (chrome extension) -


i creating chrome extension want able enable/disable. have made popup that. trouble is, if reload extension (or if user downloads initially) content scripts default being off. could inject content script in manifest.json results in content script being injected new tab--which not want. behavior should if download/reload extension, on default, can enable it/disable , applies every new tab. have tried put initialization in background.js not called @ startup apparently.

manifest.json

{   "manifest_version": 2,   "name": "rotten tomatoes search",   "description": "this extension searches rotten tomatoes highlighted text",   "version": "1.0",   "browser_action": {     "default_icon": "./icons/icon_on.png",     "default_popup": "popup.html"   },   "permissions": [       "activetab",       "<all_urls>",       "background"   ],    "background": {     "scripts": ["background.js"],     "persistent": true   },   "content_scripts": [{      "js": ["jquery-1.12.3.min.js"],       "matches": ["<all_urls>"]    }] } 

background.js

var isextensionon = true; chrome.tabs.executescript({code: "console.log('backgournd hit...')"});  turniton();  chrome.extension.onmessage.addlistener( function (request, sender, sendresponse) {     if (request.cmd == "setonoffstate") {         isextensionon = request.data.value;     }      if (request.cmd == "getonoffstate") {         sendresponse(isextensionon);     } });  function turniton() {     chrome.browseraction.seticon({path: "./icons/icon_on.png"});     chrome.tabs.executescript({file:"opentooltipmenu.js"});     //$('#toggle').text('disable'); } 

popup.js

document.addeventlistener('domcontentloaded', function() { // show different text depending on on/off state (for icon, handled having default icon)  chrome.extension.sendmessage({ cmd: "getonoffstate" }, function(currentstate){     if (currentstate) $('#toggle').text('disable');     else $('#toggle').text('enable');  }); // allow user toggle state of extension var toggle = document.getelementbyid('toggle') toggle.addeventlistener('click', function() {     //chrome.tabs.executescript({code: "console.log('toggled...')"});     chrome.extension.sendmessage({ cmd: "getonoffstate" }, function(currentstate){         var newstate = !currentstate;         // toggle new state in background         chrome.extension.sendmessage({ cmd: "setonoffstate", data: { value: newstate } }, function(){             // after toggling, stuff based on new state             if (newstate) turnon();             else turnoff();         });     }); }) });  function turnon() {     chrome.browseraction.seticon({path: "./icons/icon_on.png"});     chrome.tabs.executescript({file:"opentooltipmenu.js"});     $('#toggle').text('disable'); }  function turnoff() {     chrome.browseraction.seticon({path: "./icons/icon_off.png"});     chrome.tabs.executescript({code: "$('body').off();"});     $('#toggle').text('enable'); } 

popup.html

<some code>     <script src="./jquery-1.12.3.min.js"></script>     <script src="./popup.js"></script><style type="text/css"></style>   </head>   <body>     <div class="popupmenu" style="list-style-type:none">       <div class="header">rotten tomatoes search</div>       <hr>       <div class="menuentry" id="toggle"></div>     </div>   </body> </html> 

i have figured out issue. architectural approach wrong. 1 should inject content_script globally check script whether or not should done. clearer, in script status background page , based on that. previously, injecting script once popup loaded or once when background initialized. additionally, 1 must loop through tabs in windows update state in tabs (if that's 1 wants).


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -