Note that there are some explanatory texts on larger screens.

plurals
  1. POBasic toggle for a Chrome Exstension
    primarykey
    data
    text
    <p>I cannot believe I am still have basic programming problems... I know the basics, but still have a hard time implementing the logic I think of. Anyways</p> <p>I am building this basic Chrome Extension that has one JavaScript file and it does work! The only issue is that once I click the icon it is forever on, that is until I remove it. I want to add a basic toggle functionality, but I am having difficulty getting a working prototype. Here is a couple of my ideas:</p> <pre><code>var toggle == 1; // or true, i.e. clicked if (functionName() == 1) { function functionName() { Do whatever it is when clicked; blah blah blah; } else if (functionName() == 0) { Turn off; } else {}; } switch(toggle) { case 1: Do whatever it is when clicked; blah blah blah; break; case 2: Turn off; break; default: error; break; } </code></pre> <p>If both if statement and switch statement had a different order, say case 1 and 2 were swapped, I do not think it would be a difference. I do not think a switch statement would be the best way because there is no more than two options, on or off.</p> <p>What about a while loop to change the conditions of the extension? I do know the modulo operator, and code could be written like:</p> <ul> <li>1 % 2 = False,</li> <li>2 % 2 = True,</li> <li><p>3 % 2 = False, etc</p> <p>Then a basic if-statement could work....</p> <p>something like: </p> <pre><code>var i = 1; while (i % 2 == 1) { Do whatever it is when clicked; blah blah blah; i++; } </code></pre> <p>Does anybody have an idea of the best way to do this? I have played with the jQuery .toggle() event, but I do not think this would make since. I have nothing in the html document and only a JavaScript file. It makes no since loading the library and then using the jQuery selector<code>$("chrome.browserAction.onClicked.addListener(function)")</code> when simple JavaScript can be used. Plus I do not even know if that would be the right selector...</p></li> </ul> <p>Any help would be great, thanks in advance.</p> <p>For the record I found the <a href="http://developer.chrome.com/extensions/samples.html" rel="nofollow">sample</a> extensions useless when it comes to something that should not be complicated.</p> <p>Thanks!</p> <p>UPDATE code with my function in background.js:</p> <pre><code>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; } </code></pre> <p>The if statement does not work at the moment, my exstension works with this call instead of the if statement at the end:</p> <pre><code>document.addEventListener('DOMContentLoaded', function() { trigger(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload