summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/popup.html
blob: 729be9f3d4be6ee425ed1c7ce7f55185045631e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html>
<head>
  <script>
var cs = chrome.experimental.contentSettings;
var pref = cs.misc.enableReferrers;

/**
 * Returns whether the |levelOfControl| means that the extension can change the
 * preference value.
 *
 * @param levelOfControl{string}
 */
function settingIsControllable(levelOfControl) {
  return (levelOfControl == "ControllableByThisExtension" ||
          levelOfControl == "ControlledByThisExtension");
}

/**
 * Updates the UI to reflect the state of the preference.
 *
 * @param settings{object} A settings object, as returned from |get()| or the
 * |onchange| event.
 */
function updateUI(settings) {
  var disableUI = !settingIsControllable(settings.levelOfControl);
  document.getElementById("regularValue").disabled = disableUI;
  document.getElementById("useSeparateIncognitoSettings").disabled = disableUI;
  if (settings.hasOwnProperty('incognitoSpecific')) {
    var hasIncognitoValue = settings.incognitoSpecific;
    document.getElementById("useSeparateIncognitoSettings").checked =
        hasIncognitoValue;
    document.getElementById("incognitoValue").disabled =
        disableUI || !hasIncognitoValue;
    document.getElementById("incognitoValue").checked = settings.value;
  } else {
    document.getElementById("regularValue").checked = settings.value;
  }
}

/**
 * Wrapper for |updateUI| which is used as callback for the |get()| method and
 * which logs the result.
 * If there was an error getting the preference, does nothing.
 *
 * @param settings{object} A settings object, as returned from |get()|.
 */
function updateUIFromGet(settings) {
  if (settings) {
    console.log('pref.get result:' + JSON.stringify(settings));
    updateUI(settings);
  }
}

/**
 * Wrapper for |updateUI| which is used as handler for the |onchange| event
 * and which logs the result.
 *
 * @param settings{object} A settings object, as returned from the |onchange|
 * event.
 */
function updateUIFromOnChange(settings) {
  console.log('pref.onChange event:' + JSON.stringify(settings));
  updateUI(settings);
}

/*
 * Initializes the UI.
 */
function init() {
  chrome.extension.isAllowedIncognitoAccess(function(allowed) {
    if (allowed) {
      pref.get({'incognito': true}, updateUIFromGet);
      document.getElementById("incognito").style.display = "block";
      document.getElementById("incognito-forbidden").style.display = "none";
    }
  });
  pref.get({}, updateUIFromGet);
  pref.onChange.addListener(updateUIFromOnChange);
}

/**
 * Called from the UI to change the preference value.
 *
 * @param enabled{boolean} The new preference value.
 * @param incognito{boolean} Whether the value is specific to incognito mode.
 */
function setPrefValue(enabled, incognito) {
  pref.set({'value':enabled, 'incognito': incognito});
}

/**
 * Called from the UI to change whether to use separate settings for
 * incognito mode.
 *
 * @param value{boolean} whether to use separate settings for
 * incognito mode.
 */
function setUseSeparateIncognitoSettings(value) {
  if (!value) {
    pref.clear({'incognito': true});
  } else {
    // Explicitly set the value for incognito mode.
    pref.get({'incognito': true}, function(settings) {
      pref.set({'incognito': true, 'value': settings.value});
    });
  }
  document.getElementById("incognitoValue").disabled = !value;
}

  </script>
</head>
<body onload="init()">

<div style="width: 300px">
<input type="checkbox" onclick="setPrefValue(this.checked)" id="regularValue" /> Enable referrers

<div id="incognito" style="display:none">
<input type="checkbox" onclick="setUseSeparateIncognitoSettings(this.checked)" id="useSeparateIncognitoSettings" /> Use separate setting for incognito mode:
<br>
<input type="checkbox" onclick="setPrefValue(this.checked, true)" id="incognitoValue" disabled="disabled"/> Enable referrers in incognito sessions
</div>
<div id="incognito-forbidden">
Select "Allow in incognito" to access incognito preferences
</div>
</div>

</body>
</html>