Skip to content

Commit d734afa

Browse files
committed
Fixed issues with navigation and raw content copying
1 parent b1e3afd commit d734afa

3 files changed

Lines changed: 52 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 1.0.7 (23-11-2018)
22

33
* Updated discovery and jora
4+
* Fixed issues with raw content copying
5+
* Another navigation issues fix
46

57
## 1.0.6 (22-11-2018)
68

src/content/index.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ function initDiscovery(settings) {
5959
'Copy raw',
6060
function() {
6161
const { raw } = discovery.context;
62+
const div = document.createElement('div');
63+
div.innerHTML = raw;
64+
const rawText = div.textContent;
6265
const el = document.createElement('textarea');
63-
el.value = raw;
66+
el.value = rawText;
6467
document.body.appendChild(el);
6568
el.select();
6669
document.execCommand('copy');
@@ -82,11 +85,33 @@ function initDiscovery(settings) {
8285
}
8386
);
8487

88+
// TODO fix this
89+
let replace = null;
90+
const originalSetPageParams = discovery.setPageParams.bind(discovery);
91+
92+
discovery.setPageParams = (...args) => {
93+
originalSetPageParams(...args);
94+
replace = args[1];
95+
};
96+
97+
const originalRenderPage = discovery.renderPage.bind(discovery);
98+
99+
discovery.renderPage = (...args) => {
100+
originalRenderPage(...args);
101+
102+
parent.postMessage({
103+
hash: window.location.hash,
104+
replace
105+
}, '*');
106+
};
107+
85108
return discovery;
86109
}
87110

88111
window.addEventListener('message', function(event) {
89-
window.location.hash = event.data.hash;
112+
if (window.location.hash !== event.data.hash) {
113+
window.location.hash = event.data.hash;
114+
}
90115

91116
if (event.data && event.data.json) {
92117
const { data } = event;
@@ -104,8 +129,8 @@ window.addEventListener('message', function(event) {
104129
}
105130
}, false);
106131

107-
window.addEventListener('hashchange', () => {
108-
parent.postMessage({
109-
hash: window.location.hash
110-
}, '*');
111-
});
132+
// window.addEventListener('hashchange', () => {
133+
// parent.postMessage({
134+
// hash: window.location.hash
135+
// }, '*');
136+
// });

src/content/inject.js

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,23 @@ if (json) {
5858
});
5959
});
6060

61-
const setHash = debounce(hash => {
62-
if (hash) {
63-
window.location.hash = hash;
64-
} else if (window.location.hash) {
61+
let iframeHash = null;
62+
63+
const setHash = (hash, replace) => {
64+
if (hash && window.location.hash !== hash) {
65+
if (replace) {
66+
history.replaceState('', document.title, window.location.pathname + window.location.search + hash);
67+
} else {
68+
window.location.hash = hash;
69+
}
70+
} else if (!hash && window.location.hash) {
6571
history.pushState('', document.title, window.location.pathname + window.location.search);
6672
}
67-
}, 300);
73+
iframeHash = hash;
74+
};
6875

6976
const onMessage = event => {
70-
setHash(event.data.hash);
77+
setHash(event.data.hash, event.data.replace);
7178

7279
if (event.data && event.data.openSettings) {
7380
if (chrome.runtime.openOptionsPage) {
@@ -81,9 +88,11 @@ if (json) {
8188
window.addEventListener('message', onMessage);
8289

8390
const onHashChange = () => {
84-
iframe.contentWindow.postMessage({
85-
hash: window.location.hash
86-
}, '*');
91+
if (iframeHash !== null && iframeHash !== window.location.hash) {
92+
iframe.contentWindow.postMessage({
93+
hash: window.location.hash
94+
}, '*');
95+
}
8796
};
8897

8998
window.addEventListener('hashchange', onHashChange);
@@ -93,26 +102,3 @@ if (json) {
93102
window.removeEventListener('hashchange', onHashChange);
94103
});
95104
}
96-
97-
/**
98-
* Debounce
99-
* @param {Function} func
100-
* @param {number} wait
101-
* @returns {Function}
102-
*/
103-
function debounce(func, wait) {
104-
let timer = null;
105-
106-
return function(...args) {
107-
const onComplete = () => {
108-
func.apply(this, args);
109-
timer = null;
110-
};
111-
112-
if (timer) {
113-
clearTimeout(timer);
114-
}
115-
116-
timer = setTimeout(onComplete, wait);
117-
};
118-
}

0 commit comments

Comments
 (0)