Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a context menu #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/context-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Checking if context menu is enabled in options
chrome.storage.sync.get({turnOffContextMenu: false}, results => {
if (results.turnOffContextMenu) {
return;
}
// Set up onclick listener for context menu
chrome.contextMenus.onClicked.addListener(tab => {
chrome.tabs.executeScript({file: 'script.js', allFrames: true});
});

// Set up context menu at install time
chrome.runtime.onInstalled.addListener(function () {
// Create context menu for `page` and `video` type
let contexts = ["page", "video"];
for (let i = 0; i < contexts.length; i++) {
let context = contexts[i];
let title = "Picture-in-picture";
chrome.contextMenus.create({"title": title, "contexts": [context], "id": "context" + context});
}
});
});
5 changes: 3 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"background": {
"persistent": false,
"scripts": ["background.js"]
"scripts": ["background.js", "context-menu.js"]
},
"browser_action": {
"default_icon": {
Expand All @@ -32,7 +32,8 @@
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
"permissions": [
"<all_urls>",
"storage"
"storage",
"contextMenus"
],
"minimum_chrome_version": "69.0.3483.0",
"manifest_version": 2
Expand Down
11 changes: 8 additions & 3 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
<head>
</head>
<body>
<label>
<label>
<input type="checkbox" id="optOutAnalytics">
<span>Opt-out of Google Analytics</span>
</label>
<script src="options.js"></script>
</label>
<br>
<label>
<input type="checkbox" id="turnOffContextMenu">
<span>Turn off context menu</span>
</label>
<script src="options.js"></script>
</body>
</html>
18 changes: 16 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

chrome.storage.sync.get({ optOutAnalytics: false }, results => {
chrome.storage.sync.get({ optOutAnalytics: false, turnOffContextMenu: false}, results => {
const optOutAnalyticsCheckbox = document.querySelector('#optOutAnalytics');

optOutAnalyticsCheckbox.checked = results.optOutAnalytics;
optOutAnalyticsCheckbox.onchange = _ => {
chrome.storage.sync.set({
optOutAnalytics: optOutAnalyticsCheckbox.checked
}, _ => {
// Reload extension to make opt-out change immediate.
// Reload extension to make opt-out change immediate.
chrome.runtime.reload();
window.close();
});
};

// Settings for turn off context menu
const turnOffContextMenuCheckbox = document.querySelector('#turnOffContextMenu');

turnOffContextMenuCheckbox.checked = results.turnOffContextMenu;
turnOffContextMenuCheckbox.onchange = _ => {
chrome.storage.sync.set({
turnOffContextMenu: turnOffContextMenuCheckbox.checked
}, _ => {
// Reload extension to make opt-out change immediate.
chrome.runtime.reload();
window.close();
});
Expand Down