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 ability to disable all the features! #877

Merged
merged 10 commits into from
Dec 17, 2017
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"to-semver": "^1.1.0",
"webext-domain-permission-toggle": "0.0.2",
"webext-dynamic-content-scripts": "^5.0.0-2",
"webext-options-sync": "^0.12.0",
"webext-options-sync": "^0.13.0",
"webextension-polyfill": "^0.2.1"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import dynamicContentScripts from 'webext-dynamic-content-scripts';
// Define defaults
new OptionsSync().define({
defaults: {
hideStarsOwnRepos: true
disabledFeatures: '',
logging: false
},
migrations: [
options => {
if (options.hideStarsOwnRepos === false) {
options.disabledFeatures += '\nhide-own-stars';
}
delete options.hideStarsOwnRepos;
},
OptionsSync.migrations.removeUnused
]
});
Expand Down
105 changes: 52 additions & 53 deletions source/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import embedGistInline from './features/embed-gist-inline';
import expandCollapseOutdatedComments from './features/expand-collapse-outdated-comments';

import * as pageDetect from './libs/page-detect';
import {observeEl, safeElementReady, safely} from './libs/utils';
import {observeEl, safeElementReady, enableFeature} from './libs/utils';

// Add globals for easier debugging
window.select = select;
Expand All @@ -73,26 +73,26 @@ async function init() {
document.documentElement.classList.add('refined-github');

if (!pageDetect.isGist()) {
safely(addTrendingMenuItem);
enableFeature(addTrendingMenuItem);
}

if (pageDetect.isDashboard()) {
safely(moveAccountSwitcherToSidebar);
enableFeature(moveAccountSwitcherToSidebar);
}

if (pageDetect.isRepo()) {
onAjaxedPages(async () => {
// Wait for the tab bar to be loaded
await safeElementReady('.pagehead + *');
safely(addMoreDropdown);
safely(addReleasesTab);
safely(removeProjectsTab);
enableFeature(addMoreDropdown);
enableFeature(addReleasesTab);
enableFeature(removeProjectsTab);
});
}

safely(focusConfirmationButtons);
safely(addKeyboardShortcutsToCommentFields);
safely(addConfirmationToCommentCancellation);
enableFeature(focusConfirmationButtons);
enableFeature(addKeyboardShortcutsToCommentFields);
enableFeature(addConfirmationToCommentCancellation);

// TODO: Enable this when we've improved how copying Markdown works
// See #522
Expand All @@ -103,110 +103,109 @@ async function init() {
}

function onDomReady() {
safely(markUnread.setup);
safely(addProfileHotkey);
enableFeature(markUnread);
enableFeature(enableCopyOnY);
enableFeature(addProfileHotkey);

if (!pageDetect.isGist()) {
safely(moveMarketplaceLinkToProfileDropdown);
enableFeature(moveMarketplaceLinkToProfileDropdown);
}

if (pageDetect.isGist()) {
safely(addFileCopyButton);
enableFeature(addFileCopyButton);
}

if (pageDetect.isDashboard()) {
safely(hideOwnStars);
safely(autoLoadMoreNews);
enableFeature(hideOwnStars);
enableFeature(autoLoadMoreNews);
}

onAjaxedPages(ajaxedPagesHandler);
}

function ajaxedPagesHandler() {
safely(addOpenAllNotificationsButton);
safely(hideEmptyMeta);
safely(removeUploadFilesButton);
safely(addTitleToEmojis);
safely(enableCopyOnY.destroy);
enableFeature(addOpenAllNotificationsButton);
enableFeature(hideEmptyMeta);
enableFeature(removeUploadFilesButton);
enableFeature(addTitleToEmojis);

safely(() => {
enableFeature(() => {
for (const a of select.all('a[href]')) {
shortenLink(a, location.href);
}
});
}, 'shorten-links');

safely(linkifyCode); // Must be after link shortening #789
enableFeature(linkifyCode); // Must be after link shortening #789

if (pageDetect.isIssueSearch() || pageDetect.isPRSearch()) {
safely(addYoursMenuItem);
enableFeature(addYoursMenuItem);
}

if (pageDetect.isMilestone()) {
safely(addMilestoneNavigation); // Needs to be before sortMilestonesByClosestDueDate
enableFeature(addMilestoneNavigation); // Needs to be before sortMilestonesByClosestDueDate
}

if (pageDetect.isRepo()) {
safely(addReadmeButtons);
safely(addDiffViewWithoutWhitespaceOption);
safely(removeDiffSigns);
safely(addCILink);
safely(sortMilestonesByClosestDueDate); // Needs to be after addMilestoneNavigation
enableFeature(addReadmeButtons);
enableFeature(addDiffViewWithoutWhitespaceOption);
enableFeature(removeDiffSigns);
enableFeature(addCILink);
enableFeature(sortMilestonesByClosestDueDate); // Needs to be after addMilestoneNavigation
}

if (pageDetect.isPR()) {
safely(scrollToTopOnCollapse);
safely(linkifyBranchRefs.inPR);
safely(addDeleteForkLink);
safely(fixSquashAndMergeTitle);
safely(openCIDetailsInNewTab);
safely(expandCollapseOutdatedComments);
enableFeature(scrollToTopOnCollapse);
enableFeature(linkifyBranchRefs.inPR, 'linkify_branch_refs');
enableFeature(addDeleteForkLink);
enableFeature(fixSquashAndMergeTitle);
enableFeature(openCIDetailsInNewTab);
enableFeature(expandCollapseOutdatedComments);
}

if (pageDetect.isQuickPR()) {
safely(linkifyBranchRefs.inQuickPR);
enableFeature(linkifyBranchRefs.inQuickPR, 'linkify_branch_refs');
}

if (pageDetect.isPR() || pageDetect.isIssue()) {
safely(linkifyIssuesInTitles);
safely(addUploadBtn);
safely(embedGistInline);
enableFeature(linkifyIssuesInTitles);
enableFeature(addUploadBtn);
enableFeature(embedGistInline);

observeEl('.new-discussion-timeline', () => {
safely(addOPLabels);
safely(addTimeMachineLinksToComments);
enableFeature(addOPLabels);
enableFeature(addTimeMachineLinksToComments);
});
}

if (pageDetect.isPRList() || pageDetect.isIssueList()) {
safely(addFilterCommentsByYou);
safely(showRecentlyPushedBranches);
enableFeature(addFilterCommentsByYou);
enableFeature(showRecentlyPushedBranches);
}

if (pageDetect.isCommit()) {
safely(addPatchDiffLinks);
enableFeature(addPatchDiffLinks);
}

if (pageDetect.isPR() || pageDetect.isIssue() || pageDetect.isCommit() || pageDetect.isDiscussion()) {
safely(addReactionParticipants);
safely(showRealNames);
enableFeature(addReactionParticipants);
enableFeature(showRealNames);
}

if (pageDetect.isCommitList()) {
safely(markMergeCommitsInList);
enableFeature(markMergeCommitsInList);
}

if (pageDetect.isPRFiles() || pageDetect.isPRCommit()) {
safely(addCopyFilePathToPRs);
safely(preserveWhitespaceOptionInNav);
enableFeature(addCopyFilePathToPRs);
enableFeature(preserveWhitespaceOptionInNav);
}

if (pageDetect.isSingleFile()) {
safely(addFileCopyButton);
safely(enableCopyOnY.setup);
enableFeature(addFileCopyButton);
}

if (pageDetect.isRepoSettings()) {
safely(addProjectNewLink);
enableFeature(addProjectNewLink);
}
}

Expand Down
25 changes: 12 additions & 13 deletions source/features/copy-on-y.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import copyToClipboard from 'copy-text-to-clipboard';
import select from 'select-dom';
import onAjaxedPages from 'github-injection';
import copyToClipboard from 'copy-text-to-clipboard';
import * as pageDetect from '../libs/page-detect';

const handler = ({key, target}) => {
if (key === 'y' && target.nodeName !== 'INPUT') {
Expand All @@ -8,15 +10,12 @@ const handler = ({key, target}) => {
}
};

const setup = () => {
window.addEventListener('keyup', handler);
};

const destroy = () => {
window.removeEventListener('keyup', handler);
};

export default {
setup,
destroy
};
export default function () {
onAjaxedPages(() => {
if (pageDetect.isSingleFile()) {
window.addEventListener('keyup', handler);
} else {
window.removeEventListener('keyup', handler);
}
});
}
21 changes: 7 additions & 14 deletions source/features/hide-own-stars.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import select from 'select-dom';
import OptionsSync from 'webext-options-sync';
import {getUsername, observeEl} from '../libs/utils';

const options = new OptionsSync();

// Hide other users starring/forking your repos
export default async function () {
const {hideStarsOwnRepos} = await options.getAll();

if (hideStarsOwnRepos) {
const username = getUsername();
observeEl('#dashboard .news', () => {
for (const item of select.all('#dashboard .news .watch_started, #dashboard .news .fork')) {
if (select(`a[href^="/${username}"]`, item)) {
item.style.display = 'none';
}
const username = getUsername();
observeEl('#dashboard .news', () => {
for (const item of select.all('#dashboard .news .watch_started, #dashboard .news .fork')) {
if (select(`a[href^="/${username}"]`, item)) {
item.style.display = 'none';
}
});
}
}
});
}
11 changes: 3 additions & 8 deletions source/features/mark-unread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import gitHubInjection from 'github-injection';
import SynchronousStorage from '../libs/synchronous-storage';
import * as icons from '../libs/icons';
import * as pageDetect from '../libs/page-detect';
import {getUsername, safely} from '../libs/utils';
import {getUsername, enableFeature} from '../libs/utils';
import addOpenAllNotificationsButton from './open-all-notifications';

let storage;
Expand Down Expand Up @@ -337,7 +337,7 @@ function updateLocalParticipatingCount() {
}
}

async function setup() {
export default async function () {
storage = await new SynchronousStorage(
async () => {
const storage = await browser.storage.local.get({
Expand Down Expand Up @@ -370,7 +370,7 @@ async function setup() {
storage.set([]);
})
);
safely(addOpenAllNotificationsButton);
enableFeature(addOpenAllNotificationsButton);
} else if (pageDetect.isPR() || pageDetect.isIssue()) {
markRead(location.href);
addMarkUnreadButton();
Expand All @@ -389,8 +389,3 @@ function destroy() {
button.remove();
}
}

export default {
setup,
destroy
};
24 changes: 21 additions & 3 deletions source/libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import {h} from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import elementReady from 'element-ready';
import domLoaded from 'dom-loaded';
import elementReady from 'element-ready';
import OptionsSync from 'webext-options-sync';

const options = new OptionsSync().getAll();

/**
* Enable toggling each feature via options.
* Prevent fn's errors from blocking the remaining tasks.
* https://github.com/sindresorhus/refined-github/issues/678
* The code looks weird but it's synchronous and fn is called without args.
*/
export const safely = async fn => fn();
export const enableFeature = async (fn, filename) => {
const {disabledFeatures, logging} = await options;
const log = logging ? console.log : () => {};

filename = filename || fn.name.replace(/_/g, '-');
if (/^$|^anonymous$/.test(filename)) {
console.warn('This feature is nameless', fn);
} else {
log('✅', filename); // Testing only
if (disabledFeatures.includes(filename)) {
log('↩️', 'Skipping', filename); // Testing only
return;
}
}
fn();
};

export const getUsername = onetime(() => select('meta[name="user-login"]').getAttribute('content'));

Expand Down
24 changes: 16 additions & 8 deletions source/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@
form ~ form {
margin-top: 2em;
}
textarea {
width: 100%;
}
</style>
<form id="options-form">
<h2>Information</h2>
<h2>GitHub Enterprise</h2>
<p>
GitHub Enterprise user? While on your site, right-click on Refined GitHub's icon in the toolbar and select <strong>Enable Refined GitHub on this domain</strong>.
While on your GitHub, right-click on Refined GitHub's icon in the toolbar and select <strong>Enable Refined GitHub on this domain</strong>.
<a href="https://user-images.githubusercontent.com/1402241/32874388-e0c64150-cacc-11e7-9a50-eae3727fd3c2.png" target="_blank">Example</a>. This is <a href="https://github.com/sindresorhus/refined-github/issues/840#issuecomment-349936195" target="_blank">not yet supported by Firefox</a>.
</p>

<h2>News feed</h2>

<h2>Disable features</h2>
<p>
<strong>Notice:</strong> All JavaScript-based features can be disabled here, however the site might not work correctly since CSS changes are still applied. This is mostly untested and feature names might change.
</p>
<p>
List the features to disable, by <a href="https://github.com/sindresorhus/refined-github/tree/master/source/features" target="_blank">filename</a>:<br>
<textarea name="disabledFeatures" rows="5" placeholder="For example:&#10;mark-unread&#10;hide-own-stars"></textarea>
</p>
<p>
<label>
<input type="checkbox" name="hideStarsOwnRepos">
Hide other users starring/forking your repos
</label>
<input type="checkbox" name="logging">
Enable logging (Show in the console what features are enabled on each page)
</p>
</form>
<script src="browser-polyfill.min.js"></script>
Expand Down