Basic toggle for a Chrome Exstension -


i cannot believe still have basic programming problems... know basics, still have hard time implementing logic think of. anyways

i building basic chrome extension has 1 javascript file , work! issue once click icon forever on, until remove it. want add basic toggle functionality, having difficulty getting working prototype. here couple of ideas:

var toggle == 1; // or true, i.e. clicked if (functionname() == 1) {     function functionname() {         whatever when clicked;         blah blah blah;         } else if (functionname() == 0) {             turn off;         } else {}; }  switch(toggle) {     case 1:         whatever when clicked;         blah blah blah;         break; case 2:         turn off;          break; default:         error;         break; } 

if both if statement , switch statement had different order, case 1 , 2 swapped, not think difference. not think switch statement best way because there no more 2 options, on or off.

what while loop change conditions of extension? know modulo operator, , code written like:

  • 1 % 2 = false,
  • 2 % 2 = true,
  • 3 % 2 = false, etc

    then basic if-statement work....

    something like:

    var = 1; while (i % 2 == 1) {      whatever when clicked;      blah blah blah;      i++;      } 

    does have idea of best way this? have played jquery .toggle() event, not think make since. have nothing in html document , javascript file. makes no since loading library , using jquery selector$("chrome.browseraction.onclicked.addlistener(function)") when simple javascript can used. plus not know if right selector...

any great, in advance.

for record found sample extensions useless when comes should not complicated.

thanks!

update code function in background.js:

function trigger() {     chrome.browseraction.onclicked.addlistener(function(tab) { chrome.windows.onfocuschanged.addlistener(function(windowid) {     if (windowid != chrome.windows.window_id_none) {         chrome.tabs.query({ active:true, windowid:windowid }, function(tabs) {             if (tabs.length == 1) {                 var tab = tabs[0];                 chrome.tabs.reload(tab.id);             }         });     } }); }); } var functionon = false; chrome.browseraction.onclicked.addlistener(function() {     if (functionon === false) {                     document.addeventlistener('domcontentloaded', function() {                     trigger();                     });                 functionon = true;             } else if (functionon === true) {                 document.addeventlistener('domcontentloaded', function() {                     //nothing...                 });                 functionon = false;             } 

the if statement not work @ moment, exstension works call instead of if statement @ end:

document.addeventlistener('domcontentloaded', function() {     trigger(); }); 

it's hard give super detailed answer without knowing supposed toggled, in nutshell need add background script:

var functionon = false; chrome.browseraction.onclicked.addlistener(function() {     if (functionon === false) {         dosomething();         functionon = true;     } else {         functionon = false;         // don't anything.     } }); 

by putting variable "functionon" in background page state persistent. may aware of this, create background script add manifest file:

"background": {   "scripts": ["background.js"] }, 

it's hard tell you're trying toggle, dosomething() whatever action you're trying perform, whether it's injecting code via content script or showing popup, etc.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -