Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
76b3cb8c
Commit
76b3cb8c
authored
Jul 11, 2017
by
Sean Roberts
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding intial pass of heuristics for iframe validity
parent
dc4e7e3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
8 deletions
+86
-8
frame-util.js
src/annotator/util/frame-util.js
+24
-8
frame-util-test.js
src/annotator/util/test/frame-util-test.js
+62
-0
No files found.
src/annotator/util/frame-util.js
View file @
76b3cb8c
...
...
@@ -2,7 +2,7 @@
// Find all iframes within this iframe only
function
findFrames
(
container
)
{
var
frames
=
Array
.
from
(
container
.
getElementsByTagName
(
'iframe'
));
const
frames
=
Array
.
from
(
container
.
getElementsByTagName
(
'iframe'
));
return
frames
.
filter
(
isValid
);
}
...
...
@@ -13,13 +13,13 @@ function hasHypothesis (iframe) {
// Inject embed.js into the iframe
function
injectHypothesis
(
iframe
,
scriptUrl
,
config
)
{
var
configElement
=
document
.
createElement
(
'script'
);
const
configElement
=
document
.
createElement
(
'script'
);
configElement
.
className
=
'js-hypothesis-config'
;
configElement
.
type
=
'application/json'
;
configElement
.
innerText
=
JSON
.
stringify
(
config
);
var
src
=
scriptUrl
;
var
embedElement
=
document
.
createElement
(
'script'
);
const
src
=
scriptUrl
;
const
embedElement
=
document
.
createElement
(
'script'
);
embedElement
.
className
=
'js-hypothesis-embed'
;
embedElement
.
async
=
true
;
embedElement
.
src
=
src
;
...
...
@@ -37,10 +37,27 @@ function isAccessible (iframe) {
}
}
// Check if this is an iframe that we want to inject embed.js into
/**
* Check if the frame elements being considered for injection have the
* basic heuristics for content that a user might want to annotate.
* Rules:
* - avoid our client iframe
* - iframe should be sizeable - to avoid the small advertisement and social plugins
*
* @param {HTMLIFrameElement} iframe the frame being checked
* @returns {boolean} result of our validity checks
*/
function
isValid
(
iframe
)
{
// Currently only checks if it's not the h-sidebar
return
iframe
.
className
!==
'h-sidebar-iframe'
;
const
isNotClientFrame
=
!
iframe
.
classList
.
contains
(
'h-sidebar-iframe'
);
const
frameRect
=
iframe
.
getBoundingClientRect
();
const
MIN_WIDTH
=
150
;
const
MIN_HEIGHT
=
150
;
const
hasSizableContainer
=
frameRect
.
width
>
MIN_WIDTH
&&
frameRect
.
height
>
MIN_HEIGHT
;
return
isNotClientFrame
&&
hasSizableContainer
;
}
function
isDocumentReady
(
iframe
,
callback
)
{
...
...
@@ -68,7 +85,6 @@ module.exports = {
hasHypothesis
:
hasHypothesis
,
injectHypothesis
:
injectHypothesis
,
isAccessible
:
isAccessible
,
isValid
:
isValid
,
isLoaded
:
isLoaded
,
isDocumentReady
:
isDocumentReady
,
};
src/annotator/util/test/frame-util-test.js
0 → 100644
View file @
76b3cb8c
'use strict'
;
const
frameUtil
=
require
(
'../frame-util'
);
describe
(
'frameUtil'
,
function
()
{
describe
(
'findFrames'
,
function
()
{
let
container
;
const
_addFrameToContainer
=
(
options
=
{})
=>
{
const
frame
=
document
.
createElement
(
'iframe'
);
frame
.
className
=
options
.
className
||
''
;
frame
.
style
.
height
=
`
${(
options
.
height
||
150
)}
px`
;
frame
.
style
.
width
=
`
${(
options
.
width
||
150
)}
px`
;
container
.
appendChild
(
frame
);
return
frame
;
};
beforeEach
(
function
()
{
container
=
document
.
createElement
(
'div'
);
document
.
body
.
appendChild
(
container
);
});
afterEach
(
function
()
{
container
.
remove
();
});
it
(
'should find valid frames'
,
function
()
{
let
foundFrames
=
frameUtil
.
findFrames
(
container
);
assert
.
lengthOf
(
foundFrames
,
0
,
'no frames appended so none should be found'
);
const
frame1
=
_addFrameToContainer
();
const
frame2
=
_addFrameToContainer
();
foundFrames
=
frameUtil
.
findFrames
(
container
);
assert
.
deepEqual
(
foundFrames
,
[
frame1
,
frame2
],
'appended frames should be found'
);
});
it
(
'should not find small frames'
,
function
()
{
// add frames that are small in both demensions
_addFrameToContainer
({
width
:
140
});
_addFrameToContainer
({
height
:
140
});
const
foundFrames
=
frameUtil
.
findFrames
(
container
);
assert
.
lengthOf
(
foundFrames
,
0
,
'frames with small demensions should not be found'
);
});
it
(
'should not find hypothesis frames'
,
function
()
{
_addFrameToContainer
({
className
:
'h-sidebar-iframe other-class-too'
});
const
foundFrames
=
frameUtil
.
findFrames
(
container
);
assert
.
lengthOf
(
foundFrames
,
0
,
'frames with hypothesis className should not be found'
);
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment